comparison src/org/eclipse/jetty/io/BufferUtil.java @ 1018:4dc1e1a18661

remove HttpSchemes
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 24 Oct 2016 05:37:24 -0600
parents 3428c60d7cfc
children f126d30e04a4
comparison
equal deleted inserted replaced
1017:d2c3ff33387c 1018:4dc1e1a18661
24 /* ------------------------------------------------------------------------------- */ 24 /* ------------------------------------------------------------------------------- */
25 /** Buffer utility methods. 25 /** Buffer utility methods.
26 * 26 *
27 * 27 *
28 */ 28 */
29 public class BufferUtil 29 public final class BufferUtil
30 { 30 {
31 static final byte SPACE= 0x20; 31 static final byte SPACE= 0x20;
32 static final byte MINUS= '-'; 32 static final byte MINUS= '-';
33 static final byte[] DIGIT= 33 static final byte[] DIGIT=
34 {(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'}; 34 {(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'};
35 35
36 /** 36 /**
37 * Convert buffer to an integer. 37 * Convert buffer to an integer.
38 * Parses up to the first non-numeric character. If no number is found an 38 * Parses up to the first non-numeric character. If no number is found an
39 * IllegalArgumentException is thrown 39 * IllegalArgumentException is thrown
40 * @param buffer A buffer containing an integer. The position is not changed. 40 * @param buffer A buffer containing an integer. The position is not changed.
41 * @return an int 41 * @return an int
42 */ 42 */
43 public static int toInt(Buffer buffer) 43 public static int toInt(Buffer buffer)
44 { 44 {
45 int val= 0; 45 int val= 0;
46 boolean started= false; 46 boolean started= false;
47 boolean minus= false; 47 boolean minus= false;
48 for (int i= buffer.getIndex(); i < buffer.putIndex(); i++) 48 for (int i= buffer.getIndex(); i < buffer.putIndex(); i++)
49 { 49 {
50 byte b= buffer.peek(i); 50 byte b= buffer.peek(i);
51 if (b <= SPACE) 51 if (b <= SPACE)
52 { 52 {
53 if (started) 53 if (started)
54 break; 54 break;
55 } 55 }
56 else if (b >= '0' && b <= '9') 56 else if (b >= '0' && b <= '9')
57 { 57 {
58 val= val * 10 + (b - '0'); 58 val= val * 10 + (b - '0');
59 started= true; 59 started= true;
60 } 60 }
61 else if (b == MINUS && !started) 61 else if (b == MINUS && !started)
62 { 62 {
63 minus= true; 63 minus= true;
64 } 64 }
65 else 65 else
66 break; 66 break;
67 } 67 }
68 68
69 if (started) 69 if (started)
70 return minus ? (-val) : val; 70 return minus ? (-val) : val;
71 throw new NumberFormatException(buffer.toString()); 71 throw new NumberFormatException(buffer.toString());
72 } 72 }
73 73
74 /** 74 /**
75 * Convert buffer to an long. 75 * Convert buffer to an long.
76 * Parses up to the first non-numeric character. If no number is found an 76 * Parses up to the first non-numeric character. If no number is found an
77 * IllegalArgumentException is thrown 77 * IllegalArgumentException is thrown
78 * @param buffer A buffer containing an integer. The position is not changed. 78 * @param buffer A buffer containing an integer. The position is not changed.
79 * @return an int 79 * @return an int
80 */ 80 */
81 public static long toLong(Buffer buffer) 81 public static long toLong(Buffer buffer)
82 { 82 {
83 long val= 0; 83 long val= 0;
84 boolean started= false; 84 boolean started= false;
85 boolean minus= false; 85 boolean minus= false;
86 for (int i= buffer.getIndex(); i < buffer.putIndex(); i++) 86 for (int i= buffer.getIndex(); i < buffer.putIndex(); i++)
87 { 87 {
88 byte b= buffer.peek(i); 88 byte b= buffer.peek(i);
89 if (b <= SPACE) 89 if (b <= SPACE)
90 { 90 {
91 if (started) 91 if (started)
92 break; 92 break;
93 } 93 }
94 else if (b >= '0' && b <= '9') 94 else if (b >= '0' && b <= '9')
95 { 95 {
96 val= val * 10L + (b - '0'); 96 val= val * 10L + (b - '0');
97 started= true; 97 started= true;
98 } 98 }
99 else if (b == MINUS && !started) 99 else if (b == MINUS && !started)
100 { 100 {
101 minus= true; 101 minus= true;
102 } 102 }
103 else 103 else
104 break; 104 break;
105 } 105 }
106 106
107 if (started) 107 if (started)
108 return minus ? (-val) : val; 108 return minus ? (-val) : val;
109 throw new NumberFormatException(buffer.toString()); 109 throw new NumberFormatException(buffer.toString());
110 } 110 }
111 111
112 public static void putHexInt(Buffer buffer, int n) 112 public static void putHexInt(Buffer buffer, int n)
113 { 113 {
114 114
115 if (n < 0) 115 if (n < 0)
116 { 116 {
117 buffer.put((byte)'-'); 117 buffer.put((byte)'-');
118 118
119 if (n == Integer.MIN_VALUE) 119 if (n == Integer.MIN_VALUE)
120 { 120 {
121 buffer.put((byte)(0x7f&'8')); 121 buffer.put((byte)(0x7f&'8'));
122 buffer.put((byte)(0x7f&'0')); 122 buffer.put((byte)(0x7f&'0'));
123 buffer.put((byte)(0x7f&'0')); 123 buffer.put((byte)(0x7f&'0'));
124 buffer.put((byte)(0x7f&'0')); 124 buffer.put((byte)(0x7f&'0'));
125 buffer.put((byte)(0x7f&'0')); 125 buffer.put((byte)(0x7f&'0'));
126 buffer.put((byte)(0x7f&'0')); 126 buffer.put((byte)(0x7f&'0'));
127 buffer.put((byte)(0x7f&'0')); 127 buffer.put((byte)(0x7f&'0'));
128 buffer.put((byte)(0x7f&'0')); 128 buffer.put((byte)(0x7f&'0'));
129 129
130 return; 130 return;
131 } 131 }
132 n= -n; 132 n= -n;
133 } 133 }
134 134
135 if (n < 0x10) 135 if (n < 0x10)
136 { 136 {
137 buffer.put(DIGIT[n]); 137 buffer.put(DIGIT[n]);
138 } 138 }
139 else 139 else
140 { 140 {
141 boolean started= false; 141 boolean started= false;
142 // This assumes constant time int arithmatic 142 // This assumes constant time int arithmatic
143 for (int i= 0; i < hexDivisors.length; i++) 143 for (int i= 0; i < hexDivisors.length; i++)
144 { 144 {
145 if (n < hexDivisors[i]) 145 if (n < hexDivisors[i])
146 { 146 {
147 if (started) 147 if (started)
148 buffer.put((byte)'0'); 148 buffer.put((byte)'0');
149 continue; 149 continue;
150 } 150 }
151 151
152 started= true; 152 started= true;
153 int d= n / hexDivisors[i]; 153 int d= n / hexDivisors[i];
154 buffer.put(DIGIT[d]); 154 buffer.put(DIGIT[d]);
155 n= n - d * hexDivisors[i]; 155 n= n - d * hexDivisors[i];
156 } 156 }
157 } 157 }
158 } 158 }
159 159
160 /* ------------------------------------------------------------ */ 160 /* ------------------------------------------------------------ */
161 /** 161 /**
162 * Add hex integer BEFORE current getIndex. 162 * Add hex integer BEFORE current getIndex.
163 * @param buffer 163 * @param buffer
164 * @param n 164 * @param n
165 */ 165 */
166 public static void prependHexInt(Buffer buffer, int n) 166 public static void prependHexInt(Buffer buffer, int n)
167 { 167 {
168 if (n==0) 168 if (n==0)
169 { 169 {
170 int gi=buffer.getIndex(); 170 int gi=buffer.getIndex();
171 buffer.poke(--gi,(byte)'0'); 171 buffer.poke(--gi,(byte)'0');
172 buffer.setGetIndex(gi); 172 buffer.setGetIndex(gi);
173 } 173 }
174 else 174 else
175 { 175 {
176 boolean minus=false; 176 boolean minus=false;
177 if (n<0) 177 if (n<0)
178 { 178 {
179 minus=true; 179 minus=true;
180 n=-n; 180 n=-n;
181 } 181 }
182 182
183 int gi=buffer.getIndex(); 183 int gi=buffer.getIndex();
184 while(n>0) 184 while(n>0)
185 { 185 {
186 int d = 0xf&n; 186 int d = 0xf&n;
187 n=n>>4; 187 n=n>>4;
188 buffer.poke(--gi,DIGIT[d]); 188 buffer.poke(--gi,DIGIT[d]);
189 } 189 }
190 190
191 if (minus) 191 if (minus)
192 buffer.poke(--gi,(byte)'-'); 192 buffer.poke(--gi,(byte)'-');
193 buffer.setGetIndex(gi); 193 buffer.setGetIndex(gi);
194 } 194 }
195 } 195 }
196 196
197 197
198 /* ------------------------------------------------------------ */ 198 public static void putDecLong(Buffer buffer, long n)
199 public static void putDecInt(Buffer buffer, int n) 199 {
200 { 200 if (n < 0)
201 if (n < 0) 201 {
202 { 202 buffer.put((byte)'-');
203 buffer.put((byte)'-'); 203
204 204 if (n == Long.MIN_VALUE)
205 if (n == Integer.MIN_VALUE) 205 {
206 { 206 buffer.put((byte)'9');
207 buffer.put((byte)'2'); 207 n= 223372036854775808L;
208 n= 147483648; 208 }
209 } 209 else
210 else 210 n= -n;
211 n= -n; 211 }
212 } 212
213 213 if (n < 10)
214 if (n < 10) 214 {
215 { 215 buffer.put(DIGIT[(int)n]);
216 buffer.put(DIGIT[n]); 216 }
217 } 217 else
218 else 218 {
219 { 219 boolean started= false;
220 boolean started= false; 220 // This assumes constant time int arithmatic
221 // This assumes constant time int arithmatic 221 for (int i= 0; i < decDivisorsL.length; i++)
222 for (int i= 0; i < decDivisors.length; i++) 222 {
223 { 223 if (n < decDivisorsL[i])
224 if (n < decDivisors[i]) 224 {
225 { 225 if (started)
226 if (started) 226 buffer.put((byte)'0');
227 buffer.put((byte)'0'); 227 continue;
228 continue; 228 }
229 } 229
230 230 started= true;
231 started= true; 231 long d= n / decDivisorsL[i];
232 int d= n / decDivisors[i]; 232 buffer.put(DIGIT[(int)d]);
233 buffer.put(DIGIT[d]); 233 n= n - d * decDivisorsL[i];
234 n= n - d * decDivisors[i]; 234 }
235 } 235 }
236 } 236 }
237 } 237
238 238 public static Buffer toBuffer(long value)
239 public static void putDecLong(Buffer buffer, long n) 239 {
240 { 240 ByteArrayBuffer buf=new ByteArrayBuffer(32);
241 if (n < 0) 241 putDecLong(buf, value);
242 { 242 return buf;
243 buffer.put((byte)'-'); 243 }
244 244
245 if (n == Long.MIN_VALUE) 245 private final static int[] hexDivisors=
246 { 246 {
247 buffer.put((byte)'9'); 247 0x10000000,
248 n= 223372036854775808L; 248 0x1000000,
249 } 249 0x100000,
250 else 250 0x10000,
251 n= -n; 251 0x1000,
252 } 252 0x100,
253 253 0x10,
254 if (n < 10) 254 0x1
255 { 255 };
256 buffer.put(DIGIT[(int)n]); 256
257 } 257 private final static long[] decDivisorsL=
258 else 258 {
259 { 259 1000000000000000000L,
260 boolean started= false; 260 100000000000000000L,
261 // This assumes constant time int arithmatic 261 10000000000000000L,
262 for (int i= 0; i < decDivisorsL.length; i++) 262 1000000000000000L,
263 { 263 100000000000000L,
264 if (n < decDivisorsL[i]) 264 10000000000000L,
265 { 265 1000000000000L,
266 if (started) 266 100000000000L,
267 buffer.put((byte)'0'); 267 10000000000L,
268 continue; 268 1000000000L,
269 } 269 100000000L,
270 270 10000000L,
271 started= true; 271 1000000L,
272 long d= n / decDivisorsL[i]; 272 100000L,
273 buffer.put(DIGIT[(int)d]); 273 10000L,
274 n= n - d * decDivisorsL[i]; 274 1000L,
275 } 275 100L,
276 } 276 10L,
277 } 277 1L
278 278 };
279 public static Buffer toBuffer(long value) 279
280 { 280
281 ByteArrayBuffer buf=new ByteArrayBuffer(32); 281 public static void putCRLF(Buffer buffer)
282 putDecLong(buf, value); 282 {
283 return buf; 283 buffer.put((byte)13);
284 } 284 buffer.put((byte)10);
285 285 }
286 private final static int[] decDivisors= 286
287 { 287 public static boolean isPrefix(Buffer prefix,Buffer buffer)
288 1000000000, 288 {
289 100000000, 289 if (prefix.length()>buffer.length())
290 10000000, 290 return false;
291 1000000, 291 int bi=buffer.getIndex();
292 100000, 292 for (int i=prefix.getIndex(); i<prefix.putIndex();i++)
293 10000, 293 if (prefix.peek(i)!=buffer.peek(bi++))
294 1000, 294 return false;
295 100, 295 return true;
296 10, 296 }
297 1 297
298 }; 298 public static String to8859_1_String(Buffer buffer)
299 299 {
300 private final static int[] hexDivisors= 300 if (buffer instanceof CachedBuffer)
301 { 301 return buffer.toString();
302 0x10000000, 302 return buffer.toString(StringUtil.__ISO_8859_1_CHARSET);
303 0x1000000, 303 }
304 0x100000,
305 0x10000,
306 0x1000,
307 0x100,
308 0x10,
309 0x1
310 };
311
312 private final static long[] decDivisorsL=
313 {
314 1000000000000000000L,
315 100000000000000000L,
316 10000000000000000L,
317 1000000000000000L,
318 100000000000000L,
319 10000000000000L,
320 1000000000000L,
321 100000000000L,
322 10000000000L,
323 1000000000L,
324 100000000L,
325 10000000L,
326 1000000L,
327 100000L,
328 10000L,
329 1000L,
330 100L,
331 10L,
332 1L
333 };
334
335
336 public static void putCRLF(Buffer buffer)
337 {
338 buffer.put((byte)13);
339 buffer.put((byte)10);
340 }
341
342 public static boolean isPrefix(Buffer prefix,Buffer buffer)
343 {
344 if (prefix.length()>buffer.length())
345 return false;
346 int bi=buffer.getIndex();
347 for (int i=prefix.getIndex(); i<prefix.putIndex();i++)
348 if (prefix.peek(i)!=buffer.peek(bi++))
349 return false;
350 return true;
351 }
352
353 public static String to8859_1_String(Buffer buffer)
354 {
355 if (buffer instanceof CachedBuffer)
356 return buffer.toString();
357 return buffer.toString(StringUtil.__ISO_8859_1_CHARSET);
358 }
359 } 304 }