Mercurial Hosting > luan
annotate src/org/eclipse/jetty/io/BufferUtil.java @ 1048:2b769da7f67d
remove Buffer
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Mon, 07 Nov 2016 23:15:42 -0700 |
parents | a8c92b0a08ed |
children | 4a2489f1d5fe |
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 |
1046 | 21 import java.nio.ByteBuffer; |
22 import org.eclipse.jetty.util.StringUtil; | |
23 | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
24 |
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 /** Buffer utility methods. |
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 * |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
29 */ |
1018 | 30 public final class BufferUtil |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
31 { |
1018 | 32 static final byte SPACE= 0x20; |
33 static final byte MINUS= '-'; | |
34 static final byte[] DIGIT= | |
35 {(byte)'0',(byte)'1',(byte)'2',(byte)'3',(byte)'4',(byte)'5',(byte)'6',(byte)'7',(byte)'8',(byte)'9',(byte)'A',(byte)'B',(byte)'C',(byte)'D',(byte)'E',(byte)'F'}; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
36 |
1018 | 37 /** |
38 * Convert buffer to an integer. | |
39 * Parses up to the first non-numeric character. If no number is found an | |
40 * IllegalArgumentException is thrown | |
41 * @param buffer A buffer containing an integer. The position is not changed. | |
42 * @return an int | |
43 */ | |
1048 | 44 public static int toInt(JBuffer buffer) |
1018 | 45 { |
46 int val= 0; | |
47 boolean started= false; | |
48 boolean minus= false; | |
49 for (int i= buffer.getIndex(); i < buffer.putIndex(); i++) | |
50 { | |
51 byte b= buffer.peek(i); | |
52 if (b <= SPACE) | |
53 { | |
54 if (started) | |
55 break; | |
56 } | |
57 else if (b >= '0' && b <= '9') | |
58 { | |
59 val= val * 10 + (b - '0'); | |
60 started= true; | |
61 } | |
62 else if (b == MINUS && !started) | |
63 { | |
64 minus= true; | |
65 } | |
66 else | |
67 break; | |
68 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
69 |
1018 | 70 if (started) |
71 return minus ? (-val) : val; | |
72 throw new NumberFormatException(buffer.toString()); | |
73 } | |
74 | |
75 /** | |
1020
6be43ef1eb96
HttpHeaderValues uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1019
diff
changeset
|
76 * Convert string to an long. |
1018 | 77 * Parses up to the first non-numeric character. If no number is found an |
78 * IllegalArgumentException is thrown | |
79 */ | |
1020
6be43ef1eb96
HttpHeaderValues uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1019
diff
changeset
|
80 public static long toLong(String s) |
1018 | 81 { |
1020
6be43ef1eb96
HttpHeaderValues uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1019
diff
changeset
|
82 long val = 0; |
1018 | 83 boolean started= false; |
84 boolean minus= false; | |
1020
6be43ef1eb96
HttpHeaderValues uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1019
diff
changeset
|
85 for (int i = 0; i < s.length(); i++) |
1018 | 86 { |
1020
6be43ef1eb96
HttpHeaderValues uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1019
diff
changeset
|
87 char c = s.charAt(i); |
6be43ef1eb96
HttpHeaderValues uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1019
diff
changeset
|
88 if (c <= ' ') |
1018 | 89 { |
90 if (started) | |
91 break; | |
92 } | |
1020
6be43ef1eb96
HttpHeaderValues uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1019
diff
changeset
|
93 else if (c >= '0' && c <= '9') |
1018 | 94 { |
1020
6be43ef1eb96
HttpHeaderValues uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1019
diff
changeset
|
95 val= val * 10L + (c - '0'); |
1018 | 96 started= true; |
97 } | |
1020
6be43ef1eb96
HttpHeaderValues uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1019
diff
changeset
|
98 else if (c == '-' && !started) |
1018 | 99 { |
100 minus= true; | |
101 } | |
102 else | |
103 break; | |
104 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
105 |
1018 | 106 if (started) |
107 return minus ? (-val) : val; | |
1020
6be43ef1eb96
HttpHeaderValues uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1019
diff
changeset
|
108 throw new NumberFormatException(s); |
1018 | 109 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
110 |
1048 | 111 public static void putHexInt(JBuffer buffer, int n) |
1018 | 112 { |
113 | |
114 if (n < 0) | |
115 { | |
116 buffer.put((byte)'-'); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
117 |
1018 | 118 if (n == Integer.MIN_VALUE) |
119 { | |
120 buffer.put((byte)(0x7f&'8')); | |
121 buffer.put((byte)(0x7f&'0')); | |
122 buffer.put((byte)(0x7f&'0')); | |
123 buffer.put((byte)(0x7f&'0')); | |
124 buffer.put((byte)(0x7f&'0')); | |
125 buffer.put((byte)(0x7f&'0')); | |
126 buffer.put((byte)(0x7f&'0')); | |
127 buffer.put((byte)(0x7f&'0')); | |
128 | |
129 return; | |
130 } | |
131 n= -n; | |
132 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
133 |
1018 | 134 if (n < 0x10) |
135 { | |
136 buffer.put(DIGIT[n]); | |
137 } | |
138 else | |
139 { | |
140 boolean started= false; | |
141 // This assumes constant time int arithmatic | |
142 for (int i= 0; i < hexDivisors.length; i++) | |
143 { | |
144 if (n < hexDivisors[i]) | |
145 { | |
146 if (started) | |
147 buffer.put((byte)'0'); | |
148 continue; | |
149 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
150 |
1018 | 151 started= true; |
152 int d= n / hexDivisors[i]; | |
153 buffer.put(DIGIT[d]); | |
154 n= n - d * hexDivisors[i]; | |
155 } | |
156 } | |
157 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
158 |
1018 | 159 /* ------------------------------------------------------------ */ |
160 /** | |
161 * Add hex integer BEFORE current getIndex. | |
162 * @param buffer | |
163 * @param n | |
164 */ | |
1048 | 165 public static void prependHexInt(JBuffer buffer, int n) |
1018 | 166 { |
167 if (n==0) | |
168 { | |
169 int gi=buffer.getIndex(); | |
170 buffer.poke(--gi,(byte)'0'); | |
171 buffer.setGetIndex(gi); | |
172 } | |
173 else | |
174 { | |
175 boolean minus=false; | |
176 if (n<0) | |
177 { | |
178 minus=true; | |
179 n=-n; | |
180 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
181 |
1018 | 182 int gi=buffer.getIndex(); |
183 while(n>0) | |
184 { | |
185 int d = 0xf&n; | |
186 n=n>>4; | |
187 buffer.poke(--gi,DIGIT[d]); | |
188 } | |
189 | |
190 if (minus) | |
191 buffer.poke(--gi,(byte)'-'); | |
192 buffer.setGetIndex(gi); | |
193 } | |
194 } | |
195 | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
196 |
1048 | 197 public static void putDecLong(JBuffer buffer, long n) |
1018 | 198 { |
199 if (n < 0) | |
200 { | |
201 buffer.put((byte)'-'); | |
202 | |
203 if (n == Long.MIN_VALUE) | |
204 { | |
205 buffer.put((byte)'9'); | |
206 n= 223372036854775808L; | |
207 } | |
208 else | |
209 n= -n; | |
210 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
211 |
1018 | 212 if (n < 10) |
213 { | |
214 buffer.put(DIGIT[(int)n]); | |
215 } | |
216 else | |
217 { | |
218 boolean started= false; | |
219 // This assumes constant time int arithmatic | |
220 for (int i= 0; i < decDivisorsL.length; i++) | |
221 { | |
222 if (n < decDivisorsL[i]) | |
223 { | |
224 if (started) | |
225 buffer.put((byte)'0'); | |
226 continue; | |
227 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
228 |
1018 | 229 started= true; |
230 long d= n / decDivisorsL[i]; | |
231 buffer.put(DIGIT[(int)d]); | |
232 n= n - d * decDivisorsL[i]; | |
233 } | |
234 } | |
235 } | |
236 | |
237 private final static int[] hexDivisors= | |
238 { | |
239 0x10000000, | |
240 0x1000000, | |
241 0x100000, | |
242 0x10000, | |
243 0x1000, | |
244 0x100, | |
245 0x10, | |
246 0x1 | |
247 }; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
248 |
1018 | 249 private final static long[] decDivisorsL= |
250 { | |
251 1000000000000000000L, | |
252 100000000000000000L, | |
253 10000000000000000L, | |
254 1000000000000000L, | |
255 100000000000000L, | |
256 10000000000000L, | |
257 1000000000000L, | |
258 100000000000L, | |
259 10000000000L, | |
260 1000000000L, | |
261 100000000L, | |
262 10000000L, | |
263 1000000L, | |
264 100000L, | |
265 10000L, | |
266 1000L, | |
267 100L, | |
268 10L, | |
269 1L | |
270 }; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
271 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
272 |
1048 | 273 public static void putCRLF(JBuffer buffer) |
1018 | 274 { |
275 buffer.put((byte)13); | |
276 buffer.put((byte)10); | |
277 } | |
1021 | 278 |
1046 | 279 |
1048 | 280 public static final JBuffer EMPTY_BUFFER = new JBuffer(ByteBuffer.allocate(0)); |
1046 | 281 |
1048 | 282 public static JBuffer wrap(byte[] array,int offset,int length) { |
1046 | 283 return new JBuffer(ByteBuffer.wrap(array,offset,length)); |
284 } | |
285 | |
1048 | 286 public static JBuffer wrap(byte[] array) { |
1046 | 287 return new JBuffer(ByteBuffer.wrap(array)); |
288 } | |
289 | |
1048 | 290 public static JBuffer wrap(String s) { |
1046 | 291 byte[] bytes = StringUtil.getBytes(s); |
292 ByteBuffer bb = ByteBuffer.wrap(bytes).asReadOnlyBuffer(); | |
293 return new JBuffer(bb); | |
294 } | |
295 | |
1048 | 296 public static JBuffer newBuffer(int size) { |
1046 | 297 ByteBuffer bb = ByteBuffer.allocate(size); |
298 bb.limit(0); | |
299 return new JBuffer(bb); | |
300 } | |
301 | |
1048 | 302 public static JBuffer newDirectBuffer(int size) { |
1046 | 303 ByteBuffer bb = ByteBuffer.allocateDirect(size); |
304 bb.limit(0); | |
305 return new JBuffer(bb); | |
306 } | |
307 | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
308 } |