Mercurial Hosting > luan
annotate src/org/eclipse/jetty/io/Buffer.java @ 1030:80cad9086593
remove Buffer.isVolatile()
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 03 Nov 2016 01:10:09 -0600 |
parents | 4e5e9e3c25b3 |
children | 921c25a05eaa |
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 */ |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
43 public interface Buffer extends Cloneable |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
44 { |
1010 | 45 public final static int |
46 IMMUTABLE=0, // neither indexes or contexts can be changed | |
47 READONLY=1, // indexes may be changed, but not content | |
48 READWRITE=2; // anything can be changed | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
49 |
1010 | 50 /** |
51 * Get the underlying array, if one exists. | |
52 * @return a <code>byte[]</code> backing this buffer or null if none exists. | |
53 */ | |
54 byte[] array(); | |
55 | |
56 /** | |
57 * | |
58 * @return a <code>byte[]</code> value of the bytes from the getIndex to the putIndex. | |
59 */ | |
60 byte[] asArray(); | |
61 | |
62 /** | |
63 * Get the underlying buffer. If this buffer wraps a backing buffer. | |
64 * @return The root backing buffer or this if there is no backing buffer; | |
65 */ | |
66 Buffer buffer(); | |
67 | |
68 /** | |
69 * | |
70 * @return a readonly version of this <code>Buffer</code>. | |
71 */ | |
72 Buffer asReadOnlyBuffer(); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
73 |
1010 | 74 /** |
75 * | |
76 * The capacity of the buffer. This is the maximum putIndex that may be set. | |
77 * @return an <code>int</code> value | |
78 */ | |
79 int capacity(); | |
80 | |
81 /** | |
82 * the space remaining in the buffer. | |
83 * @return capacity - putIndex | |
84 */ | |
85 int space(); | |
86 | |
87 /** | |
88 * Clear the buffer. getIndex=0, putIndex=0. | |
89 */ | |
90 void clear(); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
91 |
1010 | 92 /** |
93 * Compact the buffer by discarding bytes before the postion (or mark if set). | |
94 * Bytes from the getIndex (or mark) to the putIndex are moved to the beginning of | |
95 * the buffer and the values adjusted accordingly. | |
96 */ | |
97 void compact(); | |
98 | |
99 /** | |
100 * Get the byte at the current getIndex and increment it. | |
101 * @return The <code>byte</code> value from the current getIndex. | |
102 */ | |
103 byte get(); | |
104 | |
105 /** | |
106 * Get bytes from the current postion and put them into the passed byte array. | |
107 * The getIndex is incremented by the number of bytes copied into the array. | |
108 * @param b The byte array to fill. | |
109 * @param offset Offset in the array. | |
110 * @param length The max number of bytes to read. | |
111 * @return The number of bytes actually read. | |
112 */ | |
113 int get(byte[] b, int offset, int length); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
114 |
1010 | 115 /** |
116 * | |
117 * @param length an <code>int</code> value | |
118 * @return a <code>Buffer</code> value | |
119 */ | |
120 Buffer get(int length); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
121 |
1010 | 122 /** |
123 * The index within the buffer that will next be read or written. | |
124 * @return an <code>int</code> value >=0 <= putIndex() | |
125 */ | |
126 int getIndex(); | |
127 | |
128 /** | |
129 * @return true of putIndex > getIndex | |
130 */ | |
131 boolean hasContent(); | |
132 | |
133 /** | |
134 * | |
135 * @return a <code>boolean</code> value true if the buffer is immutable and that neither | |
136 * the buffer contents nor the indexes may be changed. | |
137 */ | |
138 boolean isImmutable(); | |
139 | |
140 /** | |
141 * | |
142 * @return a <code>boolean</code> value true if the buffer is readonly. The buffer indexes may | |
143 * be modified, but the buffer contents may not. For example a View onto an immutable Buffer will be | |
144 * read only. | |
145 */ | |
146 boolean isReadOnly(); | |
147 | |
148 /** | |
149 * The number of bytes from the getIndex to the putIndex | |
150 * @return an <code>int</code> == putIndex()-getIndex() | |
151 */ | |
152 int length(); | |
153 | |
154 /** | |
155 * Set the mark to the current getIndex. | |
156 */ | |
157 void mark(); | |
158 | |
159 /** | |
160 * Set the mark relative to the current getIndex | |
161 * @param offset an <code>int</code> value to add to the current getIndex to obtain the mark value. | |
162 */ | |
163 void mark(int offset); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
164 |
1010 | 165 /** |
166 * The current index of the mark. | |
167 * @return an <code>int</code> index in the buffer or -1 if the mark is not set. | |
168 */ | |
169 int markIndex(); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
170 |
1010 | 171 /** |
172 * Get the byte at the current getIndex without incrementing the getIndex. | |
173 * @return The <code>byte</code> value from the current getIndex. | |
174 */ | |
175 byte peek(); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
176 |
1010 | 177 /** |
178 * Get the byte at a specific index in the buffer. | |
179 * @param index an <code>int</code> value | |
180 * @return a <code>byte</code> value | |
181 */ | |
182 byte peek(int index); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
183 |
1010 | 184 /** |
185 * | |
186 * @param index an <code>int</code> value | |
187 * @param length an <code>int</code> value | |
188 * @return The <code>Buffer</code> value from the requested getIndex. | |
189 */ | |
190 Buffer peek(int index, int length); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
191 |
1010 | 192 /** |
193 * | |
194 * @param index an <code>int</code> value | |
195 * @param b The byte array to peek into | |
196 * @param offset The offset into the array to start peeking | |
197 * @param length an <code>int</code> value | |
198 * @return The number of bytes actually peeked | |
199 */ | |
200 int peek(int index, byte[] b, int offset, int length); | |
201 | |
202 /** | |
203 * Put the contents of the buffer at the specific index. | |
204 * @param index an <code>int</code> value | |
205 * @param src a <code>Buffer</code>. If the source buffer is not modified | |
206 | |
207 * @return The number of bytes actually poked | |
208 */ | |
209 int poke(int index, Buffer src); | |
210 | |
211 /** | |
212 * Put a specific byte to a specific getIndex. | |
213 * @param index an <code>int</code> value | |
214 * @param b a <code>byte</code> value | |
215 */ | |
216 void poke(int index, byte b); | |
217 | |
218 /** | |
219 * Put a specific byte to a specific getIndex. | |
220 * @param index an <code>int</code> value | |
221 * @param b a <code>byte array</code> value | |
222 * @return The number of bytes actually poked | |
223 */ | |
224 int poke(int index, byte b[], int offset, int length); | |
225 | |
226 /** | |
227 * Write the bytes from the source buffer to the current getIndex. | |
228 * @param src The source <code>Buffer</code> it is not modified. | |
229 * @return The number of bytes actually poked | |
230 */ | |
231 int put(Buffer src); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
232 |
1010 | 233 /** |
234 * Put a byte to the current getIndex and increment the getIndex. | |
235 * @param b a <code>byte</code> value | |
236 */ | |
237 void put(byte b); | |
238 | |
239 /** | |
240 * Put a byte to the current getIndex and increment the getIndex. | |
241 * @param b a <code>byte</code> value | |
242 * @return The number of bytes actually poked | |
243 */ | |
244 int put(byte[] b,int offset, int length); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
245 |
1010 | 246 /** |
247 * Put a byte to the current getIndex and increment the getIndex. | |
248 * @param b a <code>byte</code> value | |
249 * @return The number of bytes actually poked | |
250 */ | |
251 int put(byte[] b); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
252 |
1010 | 253 /** |
254 * The index of the first element that should not be read. | |
255 * @return an <code>int</code> value >= getIndex() | |
256 */ | |
257 int putIndex(); | |
258 | |
259 /** | |
260 * Reset the current getIndex to the mark | |
261 */ | |
262 void reset(); | |
263 | |
264 /** | |
265 * Set the buffers start getIndex. | |
266 * @param newStart an <code>int</code> value | |
267 */ | |
268 void setGetIndex(int newStart); | |
269 | |
270 /** | |
271 * Set a specific value for the mark. | |
272 * @param newMark an <code>int</code> value | |
273 */ | |
274 void setMarkIndex(int newMark); | |
275 | |
276 /** | |
277 * | |
278 * @param newLimit an <code>int</code> value | |
279 */ | |
280 void setPutIndex(int newLimit); | |
281 | |
282 /** | |
283 * Skip _content. The getIndex is updated by min(remaining(), n) | |
284 * @param n The number of bytes to skip | |
285 * @return the number of bytes skipped. | |
286 */ | |
287 int skip(int n); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
288 |
1010 | 289 /** |
290 * | |
291 * @return a volitile <code>Buffer</code> from the postion to the putIndex. | |
292 */ | |
293 Buffer slice(); | |
294 | |
295 /** | |
296 * | |
297 * | |
298 * @return a volitile <code>Buffer</code> value from the mark to the putIndex | |
299 */ | |
300 Buffer sliceFromMark(); | |
301 | |
302 /** | |
303 * | |
304 * | |
305 * @param length an <code>int</code> value | |
306 * @return a valitile <code>Buffer</code> value from the mark of the length requested. | |
307 */ | |
308 Buffer sliceFromMark(int length); | |
309 | |
310 /** | |
311 * | |
312 * @return a <code>String</code> value describing the state and contents of the buffer. | |
313 */ | |
314 String toDetailString(); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
315 |
1010 | 316 /* ------------------------------------------------------------ */ |
317 /** Read the buffer's contents from the input stream | |
318 * @param in input stream | |
319 * @param max maximum number of bytes that may be read | |
320 * @return actual number of bytes read or -1 for EOF | |
321 */ | |
322 int readFrom(InputStream in, int max) throws IOException; | |
323 | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
324 |
1010 | 325 String toString(String charset); |
326 | |
327 String toString(Charset charset); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
328 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
329 } |