View Javadoc

1   /*
2    * Copyright 2009 Red Hat, Inc.
3    *
4    * Red Hat licenses this file to you under the Apache License, version 2.0
5    * (the "License"); you may not use this file except in compliance with the
6    * 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   * @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
29   * @author <a href="http://gleamynode.net/">Trustin Lee</a>
30   *
31   * @version $Rev: 2080 $, $Date: 2010-01-26 18:04:19 +0900 (Tue, 26 Jan 2010) $
32   *
33   * @apiviz.uses org.jboss.netty.buffer.ChannelBuffer
34   */
35  public interface ChannelBufferIndexFinder {
36  
37      /**
38       * Returns {@code true} if and only if the data is found at the specified
39       * {@code guessedIndex} of the specified {@code buffer}.
40       * <p>
41       * The implementation should not perform an operation which raises an
42       * exception such as {@link IndexOutOfBoundsException} nor perform
43       * an operation which modifies the content of the buffer.
44       */
45      boolean find(ChannelBuffer buffer, int guessedIndex);
46  
47      /**
48       * Index finder which locates a {@code NUL (0x00)} byte.
49       */
50      static ChannelBufferIndexFinder NUL = new ChannelBufferIndexFinder() {
51          public boolean find(ChannelBuffer buffer, int guessedIndex) {
52              return buffer.getByte(guessedIndex) == 0;
53          }
54      };
55  
56      /**
57       * Index finder which locates a non-{@code NUL (0x00)} byte.
58       */
59      static ChannelBufferIndexFinder NOT_NUL = new ChannelBufferIndexFinder() {
60          public boolean find(ChannelBuffer buffer, int guessedIndex) {
61              return buffer.getByte(guessedIndex) != 0;
62          }
63      };
64  
65      /**
66       * Index finder which locates a {@code CR ('\r')} byte.
67       */
68      static ChannelBufferIndexFinder CR = new ChannelBufferIndexFinder() {
69          public boolean find(ChannelBuffer buffer, int guessedIndex) {
70              return buffer.getByte(guessedIndex) == '\r';
71          }
72      };
73  
74      /**
75       * Index finder which locates a non-{@code CR ('\r')} byte.
76       */
77      static ChannelBufferIndexFinder NOT_CR = new ChannelBufferIndexFinder() {
78          public boolean find(ChannelBuffer buffer, int guessedIndex) {
79              return buffer.getByte(guessedIndex) != '\r';
80          }
81      };
82  
83      /**
84       * Index finder which locates a {@code LF ('\n')} byte.
85       */
86      static ChannelBufferIndexFinder LF = new ChannelBufferIndexFinder() {
87          public boolean find(ChannelBuffer buffer, int guessedIndex) {
88              return buffer.getByte(guessedIndex) == '\n';
89          }
90      };
91  
92      /**
93       * Index finder which locates a non-{@code LF ('\n')} byte.
94       */
95      static ChannelBufferIndexFinder NOT_LF = new ChannelBufferIndexFinder() {
96          public boolean find(ChannelBuffer buffer, int guessedIndex) {
97              return buffer.getByte(guessedIndex) != '\n';
98          }
99      };
100 
101     /**
102      * Index finder which locates a {@code CR ('\r')} or {@code LF ('\n')}.
103      */
104     static ChannelBufferIndexFinder CRLF = new ChannelBufferIndexFinder() {
105         public boolean find(ChannelBuffer buffer, int guessedIndex) {
106             byte b = buffer.getByte(guessedIndex);
107             return b == '\r' || b == '\n';
108         }
109     };
110 
111     /**
112      * Index finder which locates a byte which is neither a {@code CR ('\r')}
113      * nor a {@code LF ('\n')}.
114      */
115     static ChannelBufferIndexFinder NOT_CRLF = new ChannelBufferIndexFinder() {
116         public boolean find(ChannelBuffer buffer, int guessedIndex) {
117             byte b = buffer.getByte(guessedIndex);
118             return b != '\r' && b != '\n';
119         }
120     };
121 
122     /**
123      * Index finder which locates a linear whitespace
124      * ({@code ' '} and {@code '\t'}).
125      */
126     static ChannelBufferIndexFinder LINEAR_WHITESPACE = new ChannelBufferIndexFinder() {
127         public boolean find(ChannelBuffer buffer, int guessedIndex) {
128             byte b = buffer.getByte(guessedIndex);
129             return b == ' ' || b == '\t';
130         }
131     };
132 
133     /**
134      * Index finder which locates a byte which is not a linear whitespace
135      * (neither {@code ' '} nor {@code '\t'}).
136      */
137     static ChannelBufferIndexFinder NOT_LINEAR_WHITESPACE = new ChannelBufferIndexFinder() {
138         public boolean find(ChannelBuffer buffer, int guessedIndex) {
139             byte b = buffer.getByte(guessedIndex);
140             return b != ' ' && b != '\t';
141         }
142     };
143 }