annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
1046
a8c92b0a08ed add JBuffer
Franklin Schmidt <fschmidt@gmail.com>
parents: 1021
diff changeset
21 import java.nio.ByteBuffer;
1054
87275900646e remove JBuffer.toString()
Franklin Schmidt <fschmidt@gmail.com>
parents: 1052
diff changeset
22 import java.nio.charset.Charset;
1046
a8c92b0a08ed add JBuffer
Franklin Schmidt <fschmidt@gmail.com>
parents: 1021
diff changeset
23 import org.eclipse.jetty.util.StringUtil;
a8c92b0a08ed add JBuffer
Franklin Schmidt <fschmidt@gmail.com>
parents: 1021
diff changeset
24
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25
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 /** Buffer utility methods.
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 *
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 *
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 */
1018
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
31 public final class BufferUtil
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 {
1056
Franklin Schmidt <fschmidt@gmail.com>
parents: 1055
diff changeset
33 private static final byte[] DIGIT=
1018
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
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
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
36
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
37 /**
1020
6be43ef1eb96 HttpHeaderValues uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents: 1019
diff changeset
38 * Convert string to an long.
1018
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
39 * Parses up to the first non-numeric character. If no number is found an
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
40 * IllegalArgumentException is thrown
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
41 */
1020
6be43ef1eb96 HttpHeaderValues uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents: 1019
diff changeset
42 public static long toLong(String s)
1018
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
43 {
1020
6be43ef1eb96 HttpHeaderValues uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents: 1019
diff changeset
44 long val = 0;
1018
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
45 boolean started= false;
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
46 boolean minus= false;
1020
6be43ef1eb96 HttpHeaderValues uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents: 1019
diff changeset
47 for (int i = 0; i < s.length(); i++)
1018
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
48 {
1020
6be43ef1eb96 HttpHeaderValues uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents: 1019
diff changeset
49 char c = s.charAt(i);
6be43ef1eb96 HttpHeaderValues uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents: 1019
diff changeset
50 if (c <= ' ')
1018
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
51 {
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
52 if (started)
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
53 break;
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
54 }
1020
6be43ef1eb96 HttpHeaderValues uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents: 1019
diff changeset
55 else if (c >= '0' && c <= '9')
1018
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
56 {
1020
6be43ef1eb96 HttpHeaderValues uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents: 1019
diff changeset
57 val= val * 10L + (c - '0');
1018
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
58 started= true;
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
59 }
1020
6be43ef1eb96 HttpHeaderValues uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents: 1019
diff changeset
60 else if (c == '-' && !started)
1018
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
61 {
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
62 minus= true;
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
63 }
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
64 else
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
65 break;
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
66 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
67
1018
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
68 if (started)
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
69 return minus ? (-val) : val;
1020
6be43ef1eb96 HttpHeaderValues uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents: 1019
diff changeset
70 throw new NumberFormatException(s);
1018
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
71 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
72
1048
2b769da7f67d remove Buffer
Franklin Schmidt <fschmidt@gmail.com>
parents: 1046
diff changeset
73 public static void putHexInt(JBuffer buffer, int n)
1018
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
74 {
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
75
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
76 if (n < 0)
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
77 {
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
78 buffer.put((byte)'-');
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
79
1018
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
80 if (n == Integer.MIN_VALUE)
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
81 {
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
82 buffer.put((byte)(0x7f&'8'));
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
83 buffer.put((byte)(0x7f&'0'));
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
84 buffer.put((byte)(0x7f&'0'));
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
85 buffer.put((byte)(0x7f&'0'));
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
86 buffer.put((byte)(0x7f&'0'));
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
87 buffer.put((byte)(0x7f&'0'));
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
88 buffer.put((byte)(0x7f&'0'));
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
89 buffer.put((byte)(0x7f&'0'));
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
90
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
91 return;
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
92 }
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
93 n= -n;
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
94 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
95
1018
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
96 if (n < 0x10)
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
97 {
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
98 buffer.put(DIGIT[n]);
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
99 }
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
100 else
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
101 {
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
102 boolean started= false;
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
103 // This assumes constant time int arithmatic
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
104 for (int i= 0; i < hexDivisors.length; i++)
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
105 {
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
106 if (n < hexDivisors[i])
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
107 {
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
108 if (started)
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
109 buffer.put((byte)'0');
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
110 continue;
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
111 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
112
1018
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
113 started= true;
1056
Franklin Schmidt <fschmidt@gmail.com>
parents: 1055
diff changeset
114 int d = n / hexDivisors[i];
1018
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
115 buffer.put(DIGIT[d]);
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
116 n= n - d * hexDivisors[i];
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
117 }
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
118 }
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
119 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
120
1018
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
121 /* ------------------------------------------------------------ */
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
122 /**
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
123 * Add hex integer BEFORE current getIndex.
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
124 * @param buffer
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
125 * @param n
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
126 */
1048
2b769da7f67d remove Buffer
Franklin Schmidt <fschmidt@gmail.com>
parents: 1046
diff changeset
127 public static void prependHexInt(JBuffer buffer, int n)
1018
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
128 {
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
129 if (n==0)
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
130 {
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
131 int gi=buffer.getIndex();
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
132 buffer.poke(--gi,(byte)'0');
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
133 buffer.setGetIndex(gi);
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
134 }
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
135 else
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
136 {
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
137 boolean minus=false;
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
138 if (n<0)
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
139 {
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
140 minus=true;
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
141 n=-n;
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
142 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
143
1018
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
144 int gi=buffer.getIndex();
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
145 while(n>0)
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
146 {
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
147 int d = 0xf&n;
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
148 n=n>>4;
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
149 buffer.poke(--gi,DIGIT[d]);
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
150 }
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
151
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
152 if (minus)
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
153 buffer.poke(--gi,(byte)'-');
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
154 buffer.setGetIndex(gi);
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
155 }
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
156 }
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
157
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
158
1048
2b769da7f67d remove Buffer
Franklin Schmidt <fschmidt@gmail.com>
parents: 1046
diff changeset
159 public static void putDecLong(JBuffer buffer, long n)
1018
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
160 {
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
161 if (n < 0)
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
162 {
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
163 buffer.put((byte)'-');
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
164
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
165 if (n == Long.MIN_VALUE)
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
166 {
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
167 buffer.put((byte)'9');
1056
Franklin Schmidt <fschmidt@gmail.com>
parents: 1055
diff changeset
168 n = 223372036854775808L;
1018
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
169 }
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
170 else
1056
Franklin Schmidt <fschmidt@gmail.com>
parents: 1055
diff changeset
171 n = -n;
1018
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
172 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
173
1018
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
174 if (n < 10)
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
175 {
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
176 buffer.put(DIGIT[(int)n]);
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
177 }
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
178 else
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
179 {
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
180 boolean started= false;
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
181 // This assumes constant time int arithmatic
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
182 for (int i= 0; i < decDivisorsL.length; i++)
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
183 {
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
184 if (n < decDivisorsL[i])
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
185 {
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
186 if (started)
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
187 buffer.put((byte)'0');
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
188 continue;
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
189 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
190
1018
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
191 started= true;
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
192 long d= n / decDivisorsL[i];
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
193 buffer.put(DIGIT[(int)d]);
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
194 n= n - d * decDivisorsL[i];
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
195 }
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
196 }
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
197 }
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
198
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
199 private final static int[] hexDivisors=
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
200 {
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
201 0x10000000,
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
202 0x1000000,
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
203 0x100000,
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
204 0x10000,
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
205 0x1000,
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
206 0x100,
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
207 0x10,
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
208 0x1
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
209 };
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
210
1018
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
211 private final static long[] decDivisorsL=
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
212 {
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
213 1000000000000000000L,
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
214 100000000000000000L,
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
215 10000000000000000L,
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
216 1000000000000000L,
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
217 100000000000000L,
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
218 10000000000000L,
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
219 1000000000000L,
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
220 100000000000L,
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
221 10000000000L,
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
222 1000000000L,
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
223 100000000L,
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
224 10000000L,
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
225 1000000L,
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
226 100000L,
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
227 10000L,
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
228 1000L,
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
229 100L,
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
230 10L,
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
231 1L
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
232 };
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
233
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
234
1048
2b769da7f67d remove Buffer
Franklin Schmidt <fschmidt@gmail.com>
parents: 1046
diff changeset
235 public static void putCRLF(JBuffer buffer)
1018
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
236 {
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
237 buffer.put((byte)13);
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
238 buffer.put((byte)10);
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
239 }
1021
Franklin Schmidt <fschmidt@gmail.com>
parents: 1020
diff changeset
240
1046
a8c92b0a08ed add JBuffer
Franklin Schmidt <fschmidt@gmail.com>
parents: 1021
diff changeset
241
1048
2b769da7f67d remove Buffer
Franklin Schmidt <fschmidt@gmail.com>
parents: 1046
diff changeset
242 public static final JBuffer EMPTY_BUFFER = new JBuffer(ByteBuffer.allocate(0));
1046
a8c92b0a08ed add JBuffer
Franklin Schmidt <fschmidt@gmail.com>
parents: 1021
diff changeset
243
1048
2b769da7f67d remove Buffer
Franklin Schmidt <fschmidt@gmail.com>
parents: 1046
diff changeset
244 public static JBuffer wrap(byte[] array,int offset,int length) {
1046
a8c92b0a08ed add JBuffer
Franklin Schmidt <fschmidt@gmail.com>
parents: 1021
diff changeset
245 return new JBuffer(ByteBuffer.wrap(array,offset,length));
a8c92b0a08ed add JBuffer
Franklin Schmidt <fschmidt@gmail.com>
parents: 1021
diff changeset
246 }
a8c92b0a08ed add JBuffer
Franklin Schmidt <fschmidt@gmail.com>
parents: 1021
diff changeset
247
1048
2b769da7f67d remove Buffer
Franklin Schmidt <fschmidt@gmail.com>
parents: 1046
diff changeset
248 public static JBuffer wrap(byte[] array) {
1046
a8c92b0a08ed add JBuffer
Franklin Schmidt <fschmidt@gmail.com>
parents: 1021
diff changeset
249 return new JBuffer(ByteBuffer.wrap(array));
a8c92b0a08ed add JBuffer
Franklin Schmidt <fschmidt@gmail.com>
parents: 1021
diff changeset
250 }
a8c92b0a08ed add JBuffer
Franklin Schmidt <fschmidt@gmail.com>
parents: 1021
diff changeset
251
1048
2b769da7f67d remove Buffer
Franklin Schmidt <fschmidt@gmail.com>
parents: 1046
diff changeset
252 public static JBuffer wrap(String s) {
1046
a8c92b0a08ed add JBuffer
Franklin Schmidt <fschmidt@gmail.com>
parents: 1021
diff changeset
253 byte[] bytes = StringUtil.getBytes(s);
a8c92b0a08ed add JBuffer
Franklin Schmidt <fschmidt@gmail.com>
parents: 1021
diff changeset
254 ByteBuffer bb = ByteBuffer.wrap(bytes).asReadOnlyBuffer();
a8c92b0a08ed add JBuffer
Franklin Schmidt <fschmidt@gmail.com>
parents: 1021
diff changeset
255 return new JBuffer(bb);
a8c92b0a08ed add JBuffer
Franklin Schmidt <fschmidt@gmail.com>
parents: 1021
diff changeset
256 }
a8c92b0a08ed add JBuffer
Franklin Schmidt <fschmidt@gmail.com>
parents: 1021
diff changeset
257
1048
2b769da7f67d remove Buffer
Franklin Schmidt <fschmidt@gmail.com>
parents: 1046
diff changeset
258 public static JBuffer newBuffer(int size) {
1046
a8c92b0a08ed add JBuffer
Franklin Schmidt <fschmidt@gmail.com>
parents: 1021
diff changeset
259 ByteBuffer bb = ByteBuffer.allocate(size);
a8c92b0a08ed add JBuffer
Franklin Schmidt <fschmidt@gmail.com>
parents: 1021
diff changeset
260 bb.limit(0);
a8c92b0a08ed add JBuffer
Franklin Schmidt <fschmidt@gmail.com>
parents: 1021
diff changeset
261 return new JBuffer(bb);
a8c92b0a08ed add JBuffer
Franklin Schmidt <fschmidt@gmail.com>
parents: 1021
diff changeset
262 }
a8c92b0a08ed add JBuffer
Franklin Schmidt <fschmidt@gmail.com>
parents: 1021
diff changeset
263
1048
2b769da7f67d remove Buffer
Franklin Schmidt <fschmidt@gmail.com>
parents: 1046
diff changeset
264 public static JBuffer newDirectBuffer(int size) {
1046
a8c92b0a08ed add JBuffer
Franklin Schmidt <fschmidt@gmail.com>
parents: 1021
diff changeset
265 ByteBuffer bb = ByteBuffer.allocateDirect(size);
a8c92b0a08ed add JBuffer
Franklin Schmidt <fschmidt@gmail.com>
parents: 1021
diff changeset
266 bb.limit(0);
a8c92b0a08ed add JBuffer
Franklin Schmidt <fschmidt@gmail.com>
parents: 1021
diff changeset
267 return new JBuffer(bb);
a8c92b0a08ed add JBuffer
Franklin Schmidt <fschmidt@gmail.com>
parents: 1021
diff changeset
268 }
a8c92b0a08ed add JBuffer
Franklin Schmidt <fschmidt@gmail.com>
parents: 1021
diff changeset
269
1054
87275900646e remove JBuffer.toString()
Franklin Schmidt <fschmidt@gmail.com>
parents: 1052
diff changeset
270
87275900646e remove JBuffer.toString()
Franklin Schmidt <fschmidt@gmail.com>
parents: 1052
diff changeset
271
87275900646e remove JBuffer.toString()
Franklin Schmidt <fschmidt@gmail.com>
parents: 1052
diff changeset
272 private static final Charset ISO_8859_1 = Charset.forName("ISO-8859-1");
87275900646e remove JBuffer.toString()
Franklin Schmidt <fschmidt@gmail.com>
parents: 1052
diff changeset
273
87275900646e remove JBuffer.toString()
Franklin Schmidt <fschmidt@gmail.com>
parents: 1052
diff changeset
274 public static String getString(JBuffer buffer) {
87275900646e remove JBuffer.toString()
Franklin Schmidt <fschmidt@gmail.com>
parents: 1052
diff changeset
275 byte[] bytes = new byte[buffer.remaining()];
87275900646e remove JBuffer.toString()
Franklin Schmidt <fschmidt@gmail.com>
parents: 1052
diff changeset
276 buffer.get(bytes);
87275900646e remove JBuffer.toString()
Franklin Schmidt <fschmidt@gmail.com>
parents: 1052
diff changeset
277 return new String(bytes,ISO_8859_1);
87275900646e remove JBuffer.toString()
Franklin Schmidt <fschmidt@gmail.com>
parents: 1052
diff changeset
278 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
279 }