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