comparison src/org/eclipse/jetty/io/BufferUtil.java @ 1056:7d872cc72ec2

minor
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 08 Nov 2016 01:19:36 -0700
parents e6ca3cb52837
children 013939bfc9e8
comparison
equal deleted inserted replaced
1055:e6ca3cb52837 1056:7d872cc72ec2
28 * 28 *
29 * 29 *
30 */ 30 */
31 public final class BufferUtil 31 public final class BufferUtil
32 { 32 {
33 static final byte SPACE= 0x20; 33 private static final byte[] DIGIT=
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'}; 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'};
37 35
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(JBuffer 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.get(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 }
70
71 if (started)
72 return minus ? (-val) : val;
73 throw new NumberFormatException(getString(buffer.duplicate()));
74 }
75 36
76 /** 37 /**
77 * Convert string to an long. 38 * Convert string to an long.
78 * Parses up to the first non-numeric character. If no number is found an 39 * Parses up to the first non-numeric character. If no number is found an
79 * IllegalArgumentException is thrown 40 * IllegalArgumentException is thrown
148 buffer.put((byte)'0'); 109 buffer.put((byte)'0');
149 continue; 110 continue;
150 } 111 }
151 112
152 started= true; 113 started= true;
153 int d= n / hexDivisors[i]; 114 int d = n / hexDivisors[i];
154 buffer.put(DIGIT[d]); 115 buffer.put(DIGIT[d]);
155 n= n - d * hexDivisors[i]; 116 n= n - d * hexDivisors[i];
156 } 117 }
157 } 118 }
158 } 119 }
202 buffer.put((byte)'-'); 163 buffer.put((byte)'-');
203 164
204 if (n == Long.MIN_VALUE) 165 if (n == Long.MIN_VALUE)
205 { 166 {
206 buffer.put((byte)'9'); 167 buffer.put((byte)'9');
207 n= 223372036854775808L; 168 n = 223372036854775808L;
208 } 169 }
209 else 170 else
210 n= -n; 171 n = -n;
211 } 172 }
212 173
213 if (n < 10) 174 if (n < 10)
214 { 175 {
215 buffer.put(DIGIT[(int)n]); 176 buffer.put(DIGIT[(int)n]);