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