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