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