Mercurial Hosting > luan
annotate src/org/eclipse/jetty/io/Buffer.java @ 1041:35e3c864d7a7
make View package local
| author | Franklin Schmidt <fschmidt@gmail.com> |
|---|---|
| date | Fri, 04 Nov 2016 00:47:23 -0600 |
| parents | 3e4949834f3e |
| children | a62193321d6a |
| rev | line source |
|---|---|
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1 // |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
2 // ======================================================================== |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
3 // Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd. |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
4 // ------------------------------------------------------------------------ |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
5 // All rights reserved. This program and the accompanying materials |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
6 // are made available under the terms of the Eclipse Public License v1.0 |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
7 // and Apache License v2.0 which accompanies this distribution. |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
8 // |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
9 // The Eclipse Public License is available at |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
10 // http://www.eclipse.org/legal/epl-v10.html |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
11 // |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
12 // The Apache License v2.0 is available at |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
13 // http://www.opensource.org/licenses/apache2.0.php |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
14 // |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
15 // You may elect to redistribute this code under either of these licenses. |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
16 // ======================================================================== |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
17 // |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
18 |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
19 package org.eclipse.jetty.io; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
20 |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
21 import java.io.IOException; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
22 import java.io.InputStream; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
23 import java.io.OutputStream; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
24 import java.nio.charset.Charset; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
25 |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
26 |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
27 /** |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
28 * Byte Buffer interface. |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
29 * |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
30 * This is a byte buffer that is designed to work like a FIFO for bytes. Puts and Gets operate on different |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
31 * pointers into the buffer and the valid _content of the buffer is always between the getIndex and the putIndex. |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
32 * |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
33 * This buffer interface is designed to be similar, but not dependent on the java.nio buffers, which may |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
34 * be used to back an implementation of this Buffer. The main difference is that NIO buffer after a put have |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
35 * their valid _content before the position and a flip is required to access that data. |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
36 * |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
37 * For this buffer it is always true that: |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
38 * markValue <= getIndex <= putIndex <= capacity |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
39 * |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
40 * |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
41 * @version 1.0 |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
42 */ |
|
1039
a7319f14ba1e
remove Buffer.isImmutable()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1038
diff
changeset
|
43 public interface Buffer |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
44 { |
| 1010 | 45 public final static int |
| 46 READONLY=1, // indexes may be changed, but not content | |
| 47 READWRITE=2; // anything can be changed | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
48 |
| 1010 | 49 /** |
| 50 * Get the underlying array, if one exists. | |
| 51 * @return a <code>byte[]</code> backing this buffer or null if none exists. | |
| 52 */ | |
| 53 byte[] array(); | |
| 54 | |
| 55 /** | |
| 56 * Get the underlying buffer. If this buffer wraps a backing buffer. | |
| 57 * @return The root backing buffer or this if there is no backing buffer; | |
| 58 */ | |
| 59 Buffer buffer(); | |
| 60 | |
| 61 /** | |
| 62 * | |
| 63 * The capacity of the buffer. This is the maximum putIndex that may be set. | |
| 64 * @return an <code>int</code> value | |
| 65 */ | |
| 66 int capacity(); | |
| 67 | |
| 68 /** | |
| 69 * the space remaining in the buffer. | |
| 70 * @return capacity - putIndex | |
| 71 */ | |
| 72 int space(); | |
| 73 | |
| 74 /** | |
| 75 * Clear the buffer. getIndex=0, putIndex=0. | |
| 76 */ | |
| 77 void clear(); | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
78 |
| 1010 | 79 /** |
| 80 * Compact the buffer by discarding bytes before the postion (or mark if set). | |
| 81 * Bytes from the getIndex (or mark) to the putIndex are moved to the beginning of | |
| 82 * the buffer and the values adjusted accordingly. | |
| 83 */ | |
| 84 void compact(); | |
| 85 | |
| 86 /** | |
| 87 * Get the byte at the current getIndex and increment it. | |
| 88 * @return The <code>byte</code> value from the current getIndex. | |
| 89 */ | |
| 90 byte get(); | |
| 91 | |
| 92 /** | |
| 93 * Get bytes from the current postion and put them into the passed byte array. | |
| 94 * The getIndex is incremented by the number of bytes copied into the array. | |
| 95 * @param b The byte array to fill. | |
| 96 * @param offset Offset in the array. | |
| 97 * @param length The max number of bytes to read. | |
| 98 * @return The number of bytes actually read. | |
| 99 */ | |
| 100 int get(byte[] b, int offset, int length); | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
101 |
| 1010 | 102 /** |
| 103 * | |
| 104 * @param length an <code>int</code> value | |
| 105 * @return a <code>Buffer</code> value | |
| 106 */ | |
| 107 Buffer get(int length); | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
108 |
| 1010 | 109 /** |
| 110 * The index within the buffer that will next be read or written. | |
| 111 * @return an <code>int</code> value >=0 <= putIndex() | |
| 112 */ | |
| 113 int getIndex(); | |
| 114 | |
| 115 /** | |
| 116 * @return true of putIndex > getIndex | |
| 117 */ | |
|
1037
3c4c7cc7904f
rename Buffer.hasContent() to hasRemaining()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1032
diff
changeset
|
118 boolean hasRemaining(); |
| 1010 | 119 |
| 120 /** | |
| 121 * | |
| 122 * @return a <code>boolean</code> value true if the buffer is readonly. The buffer indexes may | |
| 123 * be modified, but the buffer contents may not. For example a View onto an immutable Buffer will be | |
| 124 * read only. | |
| 125 */ | |
| 126 boolean isReadOnly(); | |
| 127 | |
| 128 /** | |
| 129 * The number of bytes from the getIndex to the putIndex | |
| 130 * @return an <code>int</code> == putIndex()-getIndex() | |
| 131 */ | |
|
1038
b71ad168fe34
rename Buffer.length() to remaining()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1037
diff
changeset
|
132 int remaining(); |
| 1010 | 133 |
| 134 /** | |
| 135 * Set the mark to the current getIndex. | |
| 136 */ | |
| 137 void mark(); | |
| 138 | |
| 139 /** | |
| 140 * Set the mark relative to the current getIndex | |
| 141 * @param offset an <code>int</code> value to add to the current getIndex to obtain the mark value. | |
| 142 */ | |
| 143 void mark(int offset); | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
144 |
| 1010 | 145 /** |
| 146 * The current index of the mark. | |
| 147 * @return an <code>int</code> index in the buffer or -1 if the mark is not set. | |
| 148 */ | |
| 149 int markIndex(); | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
150 |
| 1010 | 151 /** |
| 152 * Get the byte at the current getIndex without incrementing the getIndex. | |
| 153 * @return The <code>byte</code> value from the current getIndex. | |
| 154 */ | |
| 155 byte peek(); | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
156 |
| 1010 | 157 /** |
| 158 * Get the byte at a specific index in the buffer. | |
| 159 * @param index an <code>int</code> value | |
| 160 * @return a <code>byte</code> value | |
| 161 */ | |
| 162 byte peek(int index); | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
163 |
| 1032 | 164 String toString(int index, int length); |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
165 |
| 1010 | 166 /** |
| 167 * | |
| 168 * @param index an <code>int</code> value | |
| 169 * @param b The byte array to peek into | |
| 170 * @param offset The offset into the array to start peeking | |
| 171 * @param length an <code>int</code> value | |
| 172 * @return The number of bytes actually peeked | |
| 173 */ | |
| 174 int peek(int index, byte[] b, int offset, int length); | |
| 175 | |
| 176 /** | |
| 177 * Put the contents of the buffer at the specific index. | |
| 178 * @param index an <code>int</code> value | |
| 179 * @param src a <code>Buffer</code>. If the source buffer is not modified | |
| 180 | |
| 181 * @return The number of bytes actually poked | |
| 182 */ | |
| 183 int poke(int index, Buffer src); | |
| 184 | |
| 185 /** | |
| 186 * Put a specific byte to a specific getIndex. | |
| 187 * @param index an <code>int</code> value | |
| 188 * @param b a <code>byte</code> value | |
| 189 */ | |
| 190 void poke(int index, byte b); | |
| 191 | |
| 192 /** | |
| 193 * Put a specific byte to a specific getIndex. | |
| 194 * @param index an <code>int</code> value | |
| 195 * @param b a <code>byte array</code> value | |
| 196 * @return The number of bytes actually poked | |
| 197 */ | |
| 198 int poke(int index, byte b[], int offset, int length); | |
| 199 | |
| 200 /** | |
| 201 * Write the bytes from the source buffer to the current getIndex. | |
| 202 * @param src The source <code>Buffer</code> it is not modified. | |
| 203 * @return The number of bytes actually poked | |
| 204 */ | |
| 205 int put(Buffer src); | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
206 |
| 1010 | 207 /** |
| 208 * Put a byte to the current getIndex and increment the getIndex. | |
| 209 * @param b a <code>byte</code> value | |
| 210 */ | |
| 211 void put(byte b); | |
| 212 | |
| 213 /** | |
| 214 * Put a byte to the current getIndex and increment the getIndex. | |
| 215 * @param b a <code>byte</code> value | |
| 216 * @return The number of bytes actually poked | |
| 217 */ | |
| 218 int put(byte[] b,int offset, int length); | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
219 |
| 1010 | 220 /** |
| 221 * Put a byte to the current getIndex and increment the getIndex. | |
| 222 * @param b a <code>byte</code> value | |
| 223 * @return The number of bytes actually poked | |
| 224 */ | |
| 225 int put(byte[] b); | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
226 |
| 1010 | 227 /** |
| 228 * The index of the first element that should not be read. | |
| 229 * @return an <code>int</code> value >= getIndex() | |
| 230 */ | |
| 231 int putIndex(); | |
| 232 | |
| 233 /** | |
| 234 * Reset the current getIndex to the mark | |
| 235 */ | |
| 236 void reset(); | |
| 237 | |
| 238 /** | |
| 239 * Set the buffers start getIndex. | |
| 240 * @param newStart an <code>int</code> value | |
| 241 */ | |
| 242 void setGetIndex(int newStart); | |
| 243 | |
| 244 /** | |
| 245 * Set a specific value for the mark. | |
| 246 * @param newMark an <code>int</code> value | |
| 247 */ | |
| 248 void setMarkIndex(int newMark); | |
| 249 | |
| 250 /** | |
| 251 * | |
| 252 * @param newLimit an <code>int</code> value | |
| 253 */ | |
| 254 void setPutIndex(int newLimit); | |
| 255 | |
| 256 /** | |
| 257 * Skip _content. The getIndex is updated by min(remaining(), n) | |
| 258 * @param n The number of bytes to skip | |
| 259 * @return the number of bytes skipped. | |
| 260 */ | |
| 261 int skip(int n); | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
262 |
| 1010 | 263 /** |
| 264 * | |
| 265 * @return a volitile <code>Buffer</code> from the postion to the putIndex. | |
| 266 */ | |
| 267 Buffer slice(); | |
| 268 | |
| 269 /** | |
| 270 * | |
| 271 * | |
| 272 * @return a volitile <code>Buffer</code> value from the mark to the putIndex | |
| 273 */ | |
| 274 Buffer sliceFromMark(); | |
| 275 | |
| 276 /** | |
| 277 * | |
| 278 * | |
| 279 * @param length an <code>int</code> value | |
| 280 * @return a valitile <code>Buffer</code> value from the mark of the length requested. | |
| 281 */ | |
| 282 Buffer sliceFromMark(int length); | |
| 283 | |
| 284 /** | |
| 285 * | |
| 286 * @return a <code>String</code> value describing the state and contents of the buffer. | |
| 287 */ | |
| 288 String toDetailString(); | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
289 |
| 1010 | 290 /* ------------------------------------------------------------ */ |
| 291 /** Read the buffer's contents from the input stream | |
| 292 * @param in input stream | |
| 293 * @param max maximum number of bytes that may be read | |
| 294 * @return actual number of bytes read or -1 for EOF | |
| 295 */ | |
| 296 int readFrom(InputStream in, int max) throws IOException; | |
| 297 | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
298 |
| 1010 | 299 String toString(String charset); |
| 300 | |
| 301 String toString(Charset charset); | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
302 |
|
1041
35e3c864d7a7
make View package local
Franklin Schmidt <fschmidt@gmail.com>
parents:
1040
diff
changeset
|
303 |
|
35e3c864d7a7
make View package local
Franklin Schmidt <fschmidt@gmail.com>
parents:
1040
diff
changeset
|
304 // like ByteBuffer |
|
35e3c864d7a7
make View package local
Franklin Schmidt <fschmidt@gmail.com>
parents:
1040
diff
changeset
|
305 Buffer duplicate(); |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
306 } |
