Mercurial Hosting > luan
annotate src/org/eclipse/jetty/io/BufferUtil.java @ 1066:bbbda7c6e8ec
fix use of HttpGenerator._header
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Wed, 09 Nov 2016 05:48:10 -0700 |
parents | 158d1e6ac17f |
children | 9d357b9e4bcb |
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 { | |
1066
bbbda7c6e8ec
fix use of HttpGenerator._header
Franklin Schmidt <fschmidt@gmail.com>
parents:
1065
diff
changeset
|
78 buffer.putQ((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 { | |
1066
bbbda7c6e8ec
fix use of HttpGenerator._header
Franklin Schmidt <fschmidt@gmail.com>
parents:
1065
diff
changeset
|
82 buffer.putQ((byte)(0x7f&'8')); |
bbbda7c6e8ec
fix use of HttpGenerator._header
Franklin Schmidt <fschmidt@gmail.com>
parents:
1065
diff
changeset
|
83 buffer.putQ((byte)(0x7f&'0')); |
bbbda7c6e8ec
fix use of HttpGenerator._header
Franklin Schmidt <fschmidt@gmail.com>
parents:
1065
diff
changeset
|
84 buffer.putQ((byte)(0x7f&'0')); |
bbbda7c6e8ec
fix use of HttpGenerator._header
Franklin Schmidt <fschmidt@gmail.com>
parents:
1065
diff
changeset
|
85 buffer.putQ((byte)(0x7f&'0')); |
bbbda7c6e8ec
fix use of HttpGenerator._header
Franklin Schmidt <fschmidt@gmail.com>
parents:
1065
diff
changeset
|
86 buffer.putQ((byte)(0x7f&'0')); |
bbbda7c6e8ec
fix use of HttpGenerator._header
Franklin Schmidt <fschmidt@gmail.com>
parents:
1065
diff
changeset
|
87 buffer.putQ((byte)(0x7f&'0')); |
bbbda7c6e8ec
fix use of HttpGenerator._header
Franklin Schmidt <fschmidt@gmail.com>
parents:
1065
diff
changeset
|
88 buffer.putQ((byte)(0x7f&'0')); |
bbbda7c6e8ec
fix use of HttpGenerator._header
Franklin Schmidt <fschmidt@gmail.com>
parents:
1065
diff
changeset
|
89 buffer.putQ((byte)(0x7f&'0')); |
1018 | 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 { | |
1066
bbbda7c6e8ec
fix use of HttpGenerator._header
Franklin Schmidt <fschmidt@gmail.com>
parents:
1065
diff
changeset
|
98 buffer.putQ(DIGIT[n]); |
1018 | 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) | |
1066
bbbda7c6e8ec
fix use of HttpGenerator._header
Franklin Schmidt <fschmidt@gmail.com>
parents:
1065
diff
changeset
|
109 buffer.putQ((byte)'0'); |
1018 | 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]; |
1066
bbbda7c6e8ec
fix use of HttpGenerator._header
Franklin Schmidt <fschmidt@gmail.com>
parents:
1065
diff
changeset
|
115 buffer.putQ(DIGIT[d]); |
1018 | 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 |
1048 | 121 public static void putDecLong(JBuffer buffer, long n) |
1018 | 122 { |
123 if (n < 0) | |
124 { | |
1066
bbbda7c6e8ec
fix use of HttpGenerator._header
Franklin Schmidt <fschmidt@gmail.com>
parents:
1065
diff
changeset
|
125 buffer.putQ((byte)'-'); |
1018 | 126 |
127 if (n == Long.MIN_VALUE) | |
128 { | |
1066
bbbda7c6e8ec
fix use of HttpGenerator._header
Franklin Schmidt <fschmidt@gmail.com>
parents:
1065
diff
changeset
|
129 buffer.putQ((byte)'9'); |
1056 | 130 n = 223372036854775808L; |
1018 | 131 } |
132 else | |
1056 | 133 n = -n; |
1018 | 134 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
135 |
1018 | 136 if (n < 10) |
137 { | |
1066
bbbda7c6e8ec
fix use of HttpGenerator._header
Franklin Schmidt <fschmidt@gmail.com>
parents:
1065
diff
changeset
|
138 buffer.putQ(DIGIT[(int)n]); |
1018 | 139 } |
140 else | |
141 { | |
142 boolean started= false; | |
143 // This assumes constant time int arithmatic | |
144 for (int i= 0; i < decDivisorsL.length; i++) | |
145 { | |
146 if (n < decDivisorsL[i]) | |
147 { | |
148 if (started) | |
1066
bbbda7c6e8ec
fix use of HttpGenerator._header
Franklin Schmidt <fschmidt@gmail.com>
parents:
1065
diff
changeset
|
149 buffer.putQ((byte)'0'); |
1018 | 150 continue; |
151 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
152 |
1018 | 153 started= true; |
154 long d= n / decDivisorsL[i]; | |
1066
bbbda7c6e8ec
fix use of HttpGenerator._header
Franklin Schmidt <fschmidt@gmail.com>
parents:
1065
diff
changeset
|
155 buffer.putQ(DIGIT[(int)d]); |
1018 | 156 n= n - d * decDivisorsL[i]; |
157 } | |
158 } | |
159 } | |
160 | |
161 private final static int[] hexDivisors= | |
162 { | |
163 0x10000000, | |
164 0x1000000, | |
165 0x100000, | |
166 0x10000, | |
167 0x1000, | |
168 0x100, | |
169 0x10, | |
170 0x1 | |
171 }; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
172 |
1018 | 173 private final static long[] decDivisorsL= |
174 { | |
175 1000000000000000000L, | |
176 100000000000000000L, | |
177 10000000000000000L, | |
178 1000000000000000L, | |
179 100000000000000L, | |
180 10000000000000L, | |
181 1000000000000L, | |
182 100000000000L, | |
183 10000000000L, | |
184 1000000000L, | |
185 100000000L, | |
186 10000000L, | |
187 1000000L, | |
188 100000L, | |
189 10000L, | |
190 1000L, | |
191 100L, | |
192 10L, | |
193 1L | |
194 }; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
195 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
196 |
1046 | 197 |
1048 | 198 public static final JBuffer EMPTY_BUFFER = new JBuffer(ByteBuffer.allocate(0)); |
1046 | 199 |
1048 | 200 public static JBuffer wrap(byte[] array,int offset,int length) { |
1046 | 201 return new JBuffer(ByteBuffer.wrap(array,offset,length)); |
202 } | |
203 | |
1048 | 204 public static JBuffer wrap(byte[] array) { |
1046 | 205 return new JBuffer(ByteBuffer.wrap(array)); |
206 } | |
207 | |
1048 | 208 public static JBuffer wrap(String s) { |
1046 | 209 byte[] bytes = StringUtil.getBytes(s); |
210 ByteBuffer bb = ByteBuffer.wrap(bytes).asReadOnlyBuffer(); | |
211 return new JBuffer(bb); | |
212 } | |
213 | |
1048 | 214 public static JBuffer newBuffer(int size) { |
1046 | 215 ByteBuffer bb = ByteBuffer.allocate(size); |
216 bb.limit(0); | |
217 return new JBuffer(bb); | |
218 } | |
219 | |
1048 | 220 public static JBuffer newDirectBuffer(int size) { |
1046 | 221 ByteBuffer bb = ByteBuffer.allocateDirect(size); |
222 bb.limit(0); | |
223 return new JBuffer(bb); | |
224 } | |
225 | |
1054
87275900646e
remove JBuffer.toString()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1052
diff
changeset
|
226 |
87275900646e
remove JBuffer.toString()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1052
diff
changeset
|
227 |
87275900646e
remove JBuffer.toString()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1052
diff
changeset
|
228 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
|
229 |
87275900646e
remove JBuffer.toString()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1052
diff
changeset
|
230 public static String getString(JBuffer buffer) { |
87275900646e
remove JBuffer.toString()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1052
diff
changeset
|
231 byte[] bytes = new byte[buffer.remaining()]; |
87275900646e
remove JBuffer.toString()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1052
diff
changeset
|
232 buffer.get(bytes); |
87275900646e
remove JBuffer.toString()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1052
diff
changeset
|
233 return new String(bytes,ISO_8859_1); |
87275900646e
remove JBuffer.toString()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1052
diff
changeset
|
234 } |
1065
158d1e6ac17f
fix JBuffer.compact()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1059
diff
changeset
|
235 |
158d1e6ac17f
fix JBuffer.compact()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1059
diff
changeset
|
236 public static void compact(JBuffer buffer) { |
158d1e6ac17f
fix JBuffer.compact()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1059
diff
changeset
|
237 buffer.compact(); |
158d1e6ac17f
fix JBuffer.compact()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1059
diff
changeset
|
238 buffer.limit(buffer.position()); |
158d1e6ac17f
fix JBuffer.compact()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1059
diff
changeset
|
239 buffer.position(0); |
158d1e6ac17f
fix JBuffer.compact()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1059
diff
changeset
|
240 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
241 } |