View Javadoc
1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package org.jboss.netty.buffer;
17  
18  
19  /**
20   * Locates an index of data in a {@link ChannelBuffer}.
21   * <p>
22   * This interface enables the sequential search for the data which meets more
23   * complex and dynamic condition than just a simple value matching.  Please
24   * refer to {@link ChannelBuffer#indexOf(int, int, ChannelBufferIndexFinder)} and
25   * {@link ChannelBuffer#bytesBefore(int, int, ChannelBufferIndexFinder)}
26   * for more explanation.
27   *
28   * @apiviz.uses org.jboss.netty.buffer.ChannelBuffer
29   */
30  public interface ChannelBufferIndexFinder {
31  
32      /**
33       * Returns {@code true} if and only if the data is found at the specified
34       * {@code guessedIndex} of the specified {@code buffer}.
35       * <p>
36       * The implementation should not perform an operation which raises an
37       * exception such as {@link IndexOutOfBoundsException} nor perform
38       * an operation which modifies the content of the buffer.
39       */
40      boolean find(ChannelBuffer buffer, int guessedIndex);
41  
42      /**
43       * Index finder which locates a {@code NUL (0x00)} byte.
44       */
45      ChannelBufferIndexFinder NUL = new ChannelBufferIndexFinder() {
46          public boolean find(ChannelBuffer buffer, int guessedIndex) {
47              return buffer.getByte(guessedIndex) == 0;
48          }
49      };
50  
51      /**
52       * Index finder which locates a non-{@code NUL (0x00)} byte.
53       */
54      ChannelBufferIndexFinder NOT_NUL = new ChannelBufferIndexFinder() {
55          public boolean find(ChannelBuffer buffer, int guessedIndex) {
56              return buffer.getByte(guessedIndex) != 0;
57          }
58      };
59  
60      /**
61       * Index finder which locates a {@code CR ('\r')} byte.
62       */
63      ChannelBufferIndexFinder CR = new ChannelBufferIndexFinder() {
64          public boolean find(ChannelBuffer buffer, int guessedIndex) {
65              return buffer.getByte(guessedIndex) == '\r';
66          }
67      };
68  
69      /**
70       * Index finder which locates a non-{@code CR ('\r')} byte.
71       */
72      ChannelBufferIndexFinder NOT_CR = new ChannelBufferIndexFinder() {
73          public boolean find(ChannelBuffer buffer, int guessedIndex) {
74              return buffer.getByte(guessedIndex) != '\r';
75          }
76      };
77  
78      /**
79       * Index finder which locates a {@code LF ('\n')} byte.
80       */
81      ChannelBufferIndexFinder LF = new ChannelBufferIndexFinder() {
82          public boolean find(ChannelBuffer buffer, int guessedIndex) {
83              return buffer.getByte(guessedIndex) == '\n';
84          }
85      };
86  
87      /**
88       * Index finder which locates a non-{@code LF ('\n')} byte.
89       */
90      ChannelBufferIndexFinder NOT_LF = new ChannelBufferIndexFinder() {
91          public boolean find(ChannelBuffer buffer, int guessedIndex) {
92              return buffer.getByte(guessedIndex) != '\n';
93          }
94      };
95  
96      /**
97       * Index finder which locates a {@code CR ('\r')} or {@code LF ('\n')}.
98       */
99      ChannelBufferIndexFinder CRLF = new ChannelBufferIndexFinder() {
100         public boolean find(ChannelBuffer buffer, int guessedIndex) {
101             byte b = buffer.getByte(guessedIndex);
102             return b == '\r' || b == '\n';
103         }
104     };
105 
106     /**
107      * Index finder which locates a byte which is neither a {@code CR ('\r')}
108      * nor a {@code LF ('\n')}.
109      */
110     ChannelBufferIndexFinder NOT_CRLF = new ChannelBufferIndexFinder() {
111         public boolean find(ChannelBuffer buffer, int guessedIndex) {
112             byte b = buffer.getByte(guessedIndex);
113             return b != '\r' && b != '\n';
114         }
115     };
116 
117     /**
118      * Index finder which locates a linear whitespace
119      * ({@code ' '} and {@code '\t'}).
120      */
121     ChannelBufferIndexFinder LINEAR_WHITESPACE = new ChannelBufferIndexFinder() {
122         public boolean find(ChannelBuffer buffer, int guessedIndex) {
123             byte b = buffer.getByte(guessedIndex);
124             return b == ' ' || b == '\t';
125         }
126     };
127 
128     /**
129      * Index finder which locates a byte which is not a linear whitespace
130      * (neither {@code ' '} nor {@code '\t'}).
131      */
132     ChannelBufferIndexFinder NOT_LINEAR_WHITESPACE = new ChannelBufferIndexFinder() {
133         public boolean find(ChannelBuffer buffer, int guessedIndex) {
134             byte b = buffer.getByte(guessedIndex);
135             return b != ' ' && b != '\t';
136         }
137     };
138 }