1
// ========================================================================
2
// Copyright 2004-2005 Mort Bay Consulting Pty. Ltd.
3
// ------------------------------------------------------------------------
4
// Licensed under the Apache License, Version 2.0 (the "License");
5
// you may not use this file except in compliance with the License.
6
// You may obtain a copy of the License at
7
// http://www.apache.org/licenses/LICENSE-2.0
8
// Unless required by applicable law or agreed to in writing, software
9
// distributed under the License is distributed on an "AS IS" BASIS,
10
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
// See the License for the specific language governing permissions and
12
// limitations under the License.
13
// ========================================================================
15
package org.mortbay.io;
17
import java.io.UnsupportedEncodingException;
19
import org.mortbay.util.StringUtil;
21
/* ------------------------------------------------------------------------------- */
22
/** Buffer utility methods.
26
public class BufferUtil
28
static final byte SPACE= 0x20;
29
static final byte MINUS= '-';
30
static final byte[] DIGIT=
31
{(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
* Convert buffer to an integer.
35
* Parses up to the first non-numeric character. If no number is found an
36
* IllegalArgumentException is thrown
37
* @param buffer A buffer containing an integer. The position is not changed.
40
public static int toInt(Buffer buffer)
43
boolean started= false;
45
for (int i= buffer.getIndex(); i < buffer.putIndex(); i++)
47
byte b= buffer.peek(i);
53
else if (b >= '0' && b <= '9')
55
val= val * 10 + (b - '0');
58
else if (b == MINUS && !started)
67
return minus ? (-val) : val;
68
throw new NumberFormatException(buffer.toString());
72
* Convert buffer to an long.
73
* Parses up to the first non-numeric character. If no number is found an
74
* IllegalArgumentException is thrown
75
* @param buffer A buffer containing an integer. The position is not changed.
78
public static long toLong(Buffer buffer)
81
boolean started= false;
83
for (int i= buffer.getIndex(); i < buffer.putIndex(); i++)
85
byte b= buffer.peek(i);
91
else if (b >= '0' && b <= '9')
93
val= val * 10L + (b - '0');
96
else if (b == MINUS && !started)
105
return minus ? (-val) : val;
106
throw new NumberFormatException(buffer.toString());
109
public static void putHexInt(Buffer buffer, int n)
114
buffer.put((byte)'-');
116
if (n == Integer.MIN_VALUE)
118
buffer.put((byte)(0x7f&'8'));
119
buffer.put((byte)(0x7f&'0'));
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'));
134
buffer.put(DIGIT[n]);
138
boolean started= false;
139
// This assumes constant time int arithmatic
140
for (int i= 0; i < hexDivisors.length; i++)
142
if (n < hexDivisors[i])
145
buffer.put((byte)'0');
150
int d= n / hexDivisors[i];
151
buffer.put(DIGIT[d]);
152
n= n - d * hexDivisors[i];
157
/* ------------------------------------------------------------ */
159
* Add hex integer BEFORE current getIndex.
163
public static void prependHexInt(Buffer buffer, int n)
167
int gi=buffer.getIndex();
168
buffer.poke(--gi,(byte)'0');
169
buffer.setGetIndex(gi);
180
int gi=buffer.getIndex();
185
buffer.poke(--gi,DIGIT[d]);
189
buffer.poke(--gi,(byte)'-');
190
buffer.setGetIndex(gi);
195
/* ------------------------------------------------------------ */
196
public static void putDecInt(Buffer buffer, int n)
200
buffer.put((byte)'-');
202
if (n == Integer.MIN_VALUE)
204
buffer.put((byte)'2');
213
buffer.put(DIGIT[n]);
217
boolean started= false;
218
// This assumes constant time int arithmatic
219
for (int i= 0; i < decDivisors.length; i++)
221
if (n < decDivisors[i])
224
buffer.put((byte)'0');
229
int d= n / decDivisors[i];
230
buffer.put(DIGIT[d]);
231
n= n - d * decDivisors[i];
236
public static void putDecLong(Buffer buffer, long n)
240
buffer.put((byte)'-');
242
if (n == Long.MIN_VALUE)
244
buffer.put((byte)'9');
245
n= 223372036854775808L;
253
buffer.put(DIGIT[(int)n]);
257
boolean started= false;
258
// This assumes constant time int arithmatic
259
for (int i= 0; i < decDivisors.length; i++)
261
if (n < decDivisors[i])
264
buffer.put((byte)'0');
269
long d= n / decDivisors[i];
270
buffer.put(DIGIT[(int)d]);
271
n= n - d * decDivisors[i];
276
public static Buffer toBuffer(long value)
278
ByteArrayBuffer buf=new ByteArrayBuffer(16);
279
putDecLong(buf, value);
283
private static int[] decDivisors=
284
{ 1000000000, 100000000, 10000000, 1000000, 100000, 10000, 1000, 100, 10, 1 };
286
private static int[] hexDivisors=
287
{ 0x10000000, 0x1000000, 0x100000, 0x10000, 0x1000, 0x100, 0x10, 1 };
290
public static void putCRLF(Buffer buffer)
292
buffer.put((byte)13);
293
buffer.put((byte)10);
296
public static boolean isPrefix(Buffer prefix,Buffer buffer)
298
if (prefix.length()>buffer.length())
300
int bi=buffer.getIndex();
301
for (int i=prefix.getIndex(); i<prefix.putIndex();i++)
302
if (prefix.peek(i)!=buffer.peek(bi++))
307
public static String to8859_1_String(Buffer buffer)
309
if (buffer.isImmutable())
310
return buffer.toString();
314
byte[] bytes=buffer.array();
316
return new String(bytes,buffer.getIndex(),buffer.length(),StringUtil.__ISO_8859_1);
318
StringBuffer b = new StringBuffer(buffer.length());
319
for (int i=buffer.getIndex(),c=0;c<buffer.length();i++,c++)
320
b.append((char)(0x7f&buffer.peek(i)));
323
catch(UnsupportedEncodingException e)
326
return buffer.toString();