Mercurial Hosting > luan
annotate src/org/eclipse/jetty/io/BufferUtil.java @ 1018:4dc1e1a18661
remove HttpSchemes
| author | Franklin Schmidt <fschmidt@gmail.com> |
|---|---|
| date | Mon, 24 Oct 2016 05:37:24 -0600 |
| parents | 3428c60d7cfc |
| children | f126d30e04a4 |
| 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 /** | |
| 75 * Convert buffer to an long. | |
| 76 * Parses up to the first non-numeric character. If no number is found an | |
| 77 * IllegalArgumentException is thrown | |
| 78 * @param buffer A buffer containing an integer. The position is not changed. | |
| 79 * @return an int | |
| 80 */ | |
| 81 public static long toLong(Buffer buffer) | |
| 82 { | |
| 83 long val= 0; | |
| 84 boolean started= false; | |
| 85 boolean minus= false; | |
| 86 for (int i= buffer.getIndex(); i < buffer.putIndex(); i++) | |
| 87 { | |
| 88 byte b= buffer.peek(i); | |
| 89 if (b <= SPACE) | |
| 90 { | |
| 91 if (started) | |
| 92 break; | |
| 93 } | |
| 94 else if (b >= '0' && b <= '9') | |
| 95 { | |
| 96 val= val * 10L + (b - '0'); | |
| 97 started= true; | |
| 98 } | |
| 99 else if (b == MINUS && !started) | |
| 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; | |
| 109 throw new NumberFormatException(buffer.toString()); | |
| 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 public static Buffer toBuffer(long value) | |
| 239 { | |
| 240 ByteArrayBuffer buf=new ByteArrayBuffer(32); | |
| 241 putDecLong(buf, value); | |
| 242 return buf; | |
| 243 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
244 |
| 1018 | 245 private final static int[] hexDivisors= |
| 246 { | |
| 247 0x10000000, | |
| 248 0x1000000, | |
| 249 0x100000, | |
| 250 0x10000, | |
| 251 0x1000, | |
| 252 0x100, | |
| 253 0x10, | |
| 254 0x1 | |
| 255 }; | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
256 |
| 1018 | 257 private final static long[] decDivisorsL= |
| 258 { | |
| 259 1000000000000000000L, | |
| 260 100000000000000000L, | |
| 261 10000000000000000L, | |
| 262 1000000000000000L, | |
| 263 100000000000000L, | |
| 264 10000000000000L, | |
| 265 1000000000000L, | |
| 266 100000000000L, | |
| 267 10000000000L, | |
| 268 1000000000L, | |
| 269 100000000L, | |
| 270 10000000L, | |
| 271 1000000L, | |
| 272 100000L, | |
| 273 10000L, | |
| 274 1000L, | |
| 275 100L, | |
| 276 10L, | |
| 277 1L | |
| 278 }; | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
279 |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
280 |
| 1018 | 281 public static void putCRLF(Buffer buffer) |
| 282 { | |
| 283 buffer.put((byte)13); | |
| 284 buffer.put((byte)10); | |
| 285 } | |
| 286 | |
| 287 public static boolean isPrefix(Buffer prefix,Buffer buffer) | |
| 288 { | |
| 289 if (prefix.length()>buffer.length()) | |
| 290 return false; | |
| 291 int bi=buffer.getIndex(); | |
| 292 for (int i=prefix.getIndex(); i<prefix.putIndex();i++) | |
| 293 if (prefix.peek(i)!=buffer.peek(bi++)) | |
| 294 return false; | |
| 295 return true; | |
| 296 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
297 |
| 1018 | 298 public static String to8859_1_String(Buffer buffer) |
| 299 { | |
| 300 if (buffer instanceof CachedBuffer) | |
| 301 return buffer.toString(); | |
| 302 return buffer.toString(StringUtil.__ISO_8859_1_CHARSET); | |
| 303 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
304 } |
