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