View Javadoc

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