comparison 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
comparison
equal deleted inserted replaced
1019:f126d30e04a4 1020:6be43ef1eb96
70 return minus ? (-val) : val; 70 return minus ? (-val) : val;
71 throw new NumberFormatException(buffer.toString()); 71 throw new NumberFormatException(buffer.toString());
72 } 72 }
73 73
74 /** 74 /**
75 * Convert buffer to an long. 75 * Convert string to an long.
76 * Parses up to the first non-numeric character. If no number is found an 76 * Parses up to the first non-numeric character. If no number is found an
77 * IllegalArgumentException is thrown 77 * IllegalArgumentException is thrown
78 * @param buffer A buffer containing an integer. The position is not changed.
79 * @return an int
80 */ 78 */
81 public static long toLong(Buffer buffer) 79 public static long toLong(String s)
82 { 80 {
83 long val= 0; 81 long val = 0;
84 boolean started= false; 82 boolean started= false;
85 boolean minus= false; 83 boolean minus= false;
86 for (int i= buffer.getIndex(); i < buffer.putIndex(); i++) 84 for (int i = 0; i < s.length(); i++)
87 { 85 {
88 byte b= buffer.peek(i); 86 char c = s.charAt(i);
89 if (b <= SPACE) 87 if (c <= ' ')
90 { 88 {
91 if (started) 89 if (started)
92 break; 90 break;
93 } 91 }
94 else if (b >= '0' && b <= '9') 92 else if (c >= '0' && c <= '9')
95 { 93 {
96 val= val * 10L + (b - '0'); 94 val= val * 10L + (c - '0');
97 started= true; 95 started= true;
98 } 96 }
99 else if (b == MINUS && !started) 97 else if (c == '-' && !started)
100 { 98 {
101 minus= true; 99 minus= true;
102 } 100 }
103 else 101 else
104 break; 102 break;
105 } 103 }
106 104
107 if (started) 105 if (started)
108 return minus ? (-val) : val; 106 return minus ? (-val) : val;
109 throw new NumberFormatException(buffer.toString()); 107 throw new NumberFormatException(s);
110 } 108 }
111 109
112 public static void putHexInt(Buffer buffer, int n) 110 public static void putHexInt(Buffer buffer, int n)
113 { 111 {
114 112
233 n= n - d * decDivisorsL[i]; 231 n= n - d * decDivisorsL[i];
234 } 232 }
235 } 233 }
236 } 234 }
237 235
238 public static Buffer toBuffer(long value)
239 {
240 ByteArrayBuffer buf = new ByteArrayBuffer(32);
241 putDecLong(buf, value);
242 return buf;
243 }
244
245 private final static int[] hexDivisors= 236 private final static int[] hexDivisors=
246 { 237 {
247 0x10000000, 238 0x10000000,
248 0x1000000, 239 0x1000000,
249 0x100000, 240 0x100000,
281 public static void putCRLF(Buffer buffer) 272 public static void putCRLF(Buffer buffer)
282 { 273 {
283 buffer.put((byte)13); 274 buffer.put((byte)13);
284 buffer.put((byte)10); 275 buffer.put((byte)10);
285 } 276 }
286 277 /*
287 public static boolean isPrefix(Buffer prefix,Buffer buffer) 278 public static boolean isPrefix(Buffer prefix,Buffer buffer)
288 { 279 {
289 if (prefix.length()>buffer.length()) 280 if (prefix.length()>buffer.length())
290 return false; 281 return false;
291 int bi=buffer.getIndex(); 282 int bi=buffer.getIndex();
292 for (int i=prefix.getIndex(); i<prefix.putIndex();i++) 283 for (int i=prefix.getIndex(); i<prefix.putIndex();i++)
293 if (prefix.peek(i)!=buffer.peek(bi++)) 284 if (prefix.peek(i)!=buffer.peek(bi++))
294 return false; 285 return false;
295 return true; 286 return true;
296 } 287 }
297 288 */
298 public static String to8859_1_String(Buffer buffer) 289 public static String to8859_1_String(Buffer buffer)
299 { 290 {
300 if (buffer instanceof CachedBuffer) 291 if (buffer instanceof CachedBuffer)
301 return buffer.toString(); 292 return buffer.toString();
302 return buffer.toString(StringUtil.__ISO_8859_1_CHARSET); 293 return buffer.toString(StringUtil.__ISO_8859_1_CHARSET);