Mercurial Hosting > luan
annotate src/org/eclipse/jetty/io/BufferUtil.java @ 1061:c880589715c7
remove JBuffer.setGetIndex()
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 08 Nov 2016 05:54:53 -0700 |
parents | 013939bfc9e8 |
children | 158d1e6ac17f |
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 |
1048 | 121 public static void putDecLong(JBuffer buffer, long n) |
1018 | 122 { |
123 if (n < 0) | |
124 { | |
125 buffer.put((byte)'-'); | |
126 | |
127 if (n == Long.MIN_VALUE) | |
128 { | |
129 buffer.put((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 { | |
138 buffer.put(DIGIT[(int)n]); | |
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) | |
149 buffer.put((byte)'0'); | |
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]; | |
155 buffer.put(DIGIT[(int)d]); | |
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 |
1048 | 197 public static void putCRLF(JBuffer buffer) |
1018 | 198 { |
199 buffer.put((byte)13); | |
200 buffer.put((byte)10); | |
201 } | |
1021 | 202 |
1046 | 203 |
1048 | 204 public static final JBuffer EMPTY_BUFFER = new JBuffer(ByteBuffer.allocate(0)); |
1046 | 205 |
1048 | 206 public static JBuffer wrap(byte[] array,int offset,int length) { |
1046 | 207 return new JBuffer(ByteBuffer.wrap(array,offset,length)); |
208 } | |
209 | |
1048 | 210 public static JBuffer wrap(byte[] array) { |
1046 | 211 return new JBuffer(ByteBuffer.wrap(array)); |
212 } | |
213 | |
1048 | 214 public static JBuffer wrap(String s) { |
1046 | 215 byte[] bytes = StringUtil.getBytes(s); |
216 ByteBuffer bb = ByteBuffer.wrap(bytes).asReadOnlyBuffer(); | |
217 return new JBuffer(bb); | |
218 } | |
219 | |
1048 | 220 public static JBuffer newBuffer(int size) { |
1046 | 221 ByteBuffer bb = ByteBuffer.allocate(size); |
222 bb.limit(0); | |
223 return new JBuffer(bb); | |
224 } | |
225 | |
1048 | 226 public static JBuffer newDirectBuffer(int size) { |
1046 | 227 ByteBuffer bb = ByteBuffer.allocateDirect(size); |
228 bb.limit(0); | |
229 return new JBuffer(bb); | |
230 } | |
231 | |
1054
87275900646e
remove JBuffer.toString()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1052
diff
changeset
|
232 |
87275900646e
remove JBuffer.toString()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1052
diff
changeset
|
233 |
87275900646e
remove JBuffer.toString()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1052
diff
changeset
|
234 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
|
235 |
87275900646e
remove JBuffer.toString()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1052
diff
changeset
|
236 public static String getString(JBuffer buffer) { |
87275900646e
remove JBuffer.toString()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1052
diff
changeset
|
237 byte[] bytes = new byte[buffer.remaining()]; |
87275900646e
remove JBuffer.toString()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1052
diff
changeset
|
238 buffer.get(bytes); |
87275900646e
remove JBuffer.toString()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1052
diff
changeset
|
239 return new String(bytes,ISO_8859_1); |
87275900646e
remove JBuffer.toString()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1052
diff
changeset
|
240 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
241 } |