View Javadoc

1   /**
2    * This file is part of Waarp Project.
3    * 
4    * Copyright 2009, Frederic Bregier, and individual contributors by the @author tags. See the
5    * COPYRIGHT.txt in the distribution for a full listing of individual contributors.
6    * 
7    * All Waarp Project is free software: you can redistribute it and/or modify it under the terms of
8    * the GNU General Public License as published by the Free Software Foundation, either version 3 of
9    * the License, or (at your option) any later version.
10   * 
11   * Waarp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
12   * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13   * Public License for more details.
14   * 
15   * You should have received a copy of the GNU General Public License along with Waarp . If not, see
16   * <http://www.gnu.org/licenses/>.
17   */
18  package org.waarp.ftp.core.data.handler;
19  
20  import io.netty.buffer.ByteBuf;
21  
22  /**
23   * SeekAheadData Class used to optimize access to the incoming buffer
24   * 
25   * @author Frederic Bregier
26   * 
27   */
28  class FtpSeekAheadData {
29      /**
30       * Exception when NO Backend Array is found
31       */
32      static class SeekAheadNoBackArrayException extends Exception {
33          private static final long serialVersionUID = -630418804938699495L;
34      }
35  
36      byte[] bytes;
37  
38      int readerIndex;
39  
40      int pos;
41  
42      int limit;
43  
44      ByteBuf buffer;
45  
46      /**
47       * @param buffer
48       */
49      FtpSeekAheadData(ByteBuf buffer) throws SeekAheadNoBackArrayException {
50          if (!buffer.hasArray()) {
51              throw new SeekAheadNoBackArrayException();
52          }
53          this.buffer = buffer;
54          this.bytes = buffer.array();
55          this.pos = this.readerIndex = buffer.arrayOffset() + buffer.readerIndex();
56          this.limit = buffer.arrayOffset() + buffer.writerIndex();
57      }
58  
59      /**
60       * 
61       * @param minus
62       *            this value will be used as (currentPos - minus) to set the current readerIndex in
63       *            the buffer.
64       */
65      void setReadPosition(int minus) {
66          pos -= minus;
67          readerIndex = pos;
68          buffer.readerIndex(readerIndex);
69      }
70  
71      void clear() {
72          this.buffer = null;
73          this.bytes = null;
74          this.limit = 0;
75          this.pos = 0;
76          this.readerIndex = 0;
77      }
78  }