annotate 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
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
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
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
29 public final class BufferUtil
802
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 static final byte SPACE= 0x20;
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
32 static final byte MINUS= '-';
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
33 static final byte[] DIGIT=
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 * Convert buffer to an integer.
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
38 * 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
39 * IllegalArgumentException is thrown
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
40 * @param buffer A buffer containing an integer. The position is not changed.
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
41 * @return an int
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
42 */
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
43 public static int toInt(Buffer buffer)
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
44 {
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
45 int val= 0;
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
46 boolean started= false;
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
47 boolean minus= false;
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
48 for (int i= buffer.getIndex(); i < buffer.putIndex(); i++)
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
49 {
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
50 byte b= buffer.peek(i);
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
51 if (b <= SPACE)
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
52 {
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
53 if (started)
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
54 break;
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
55 }
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
56 else if (b >= '0' && b <= '9')
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
57 {
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
58 val= val * 10 + (b - '0');
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
59 started= true;
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
60 }
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
61 else if (b == MINUS && !started)
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
62 {
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
63 minus= true;
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
64 }
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
65 else
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
66 break;
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
67 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
68
1018
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
69 if (started)
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
70 return minus ? (-val) : val;
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
71 throw new NumberFormatException(buffer.toString());
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
72 }
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
73
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
74 /**
1020
6be43ef1eb96 HttpHeaderValues uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents: 1019
diff changeset
75 * Convert string to an long.
1018
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
76 * 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
77 * IllegalArgumentException is thrown
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
78 */
1020
6be43ef1eb96 HttpHeaderValues uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents: 1019
diff changeset
79 public static long toLong(String s)
1018
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
80 {
1020
6be43ef1eb96 HttpHeaderValues uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents: 1019
diff changeset
81 long val = 0;
1018
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
82 boolean started= false;
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
83 boolean minus= false;
1020
6be43ef1eb96 HttpHeaderValues uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents: 1019
diff changeset
84 for (int i = 0; i < s.length(); i++)
1018
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
85 {
1020
6be43ef1eb96 HttpHeaderValues uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents: 1019
diff changeset
86 char c = s.charAt(i);
6be43ef1eb96 HttpHeaderValues uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents: 1019
diff changeset
87 if (c <= ' ')
1018
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
88 {
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
89 if (started)
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
90 break;
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
91 }
1020
6be43ef1eb96 HttpHeaderValues uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents: 1019
diff changeset
92 else if (c >= '0' && c <= '9')
1018
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
93 {
1020
6be43ef1eb96 HttpHeaderValues uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents: 1019
diff changeset
94 val= val * 10L + (c - '0');
1018
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
95 started= true;
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
96 }
1020
6be43ef1eb96 HttpHeaderValues uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents: 1019
diff changeset
97 else if (c == '-' && !started)
1018
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
98 {
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
99 minus= true;
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
100 }
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
101 else
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
102 break;
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
103 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
104
1018
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
105 if (started)
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
106 return minus ? (-val) : val;
1020
6be43ef1eb96 HttpHeaderValues uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents: 1019
diff changeset
107 throw new NumberFormatException(s);
1018
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
108 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
109
1018
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
110 public static void putHexInt(Buffer buffer, int n)
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
111 {
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
112
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
113 if (n < 0)
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
114 {
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
115 buffer.put((byte)'-');
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
116
1018
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
117 if (n == Integer.MIN_VALUE)
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 buffer.put((byte)(0x7f&'8'));
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
120 buffer.put((byte)(0x7f&'0'));
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
121 buffer.put((byte)(0x7f&'0'));
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
122 buffer.put((byte)(0x7f&'0'));
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
123 buffer.put((byte)(0x7f&'0'));
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
124 buffer.put((byte)(0x7f&'0'));
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
125 buffer.put((byte)(0x7f&'0'));
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
126 buffer.put((byte)(0x7f&'0'));
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
127
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
128 return;
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
129 }
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
130 n= -n;
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
131 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
132
1018
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
133 if (n < 0x10)
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 buffer.put(DIGIT[n]);
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 else
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
138 {
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
139 boolean started= false;
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
140 // This assumes constant time int arithmatic
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
141 for (int i= 0; i < hexDivisors.length; i++)
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
142 {
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
143 if (n < hexDivisors[i])
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
144 {
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
145 if (started)
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
146 buffer.put((byte)'0');
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
147 continue;
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
148 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
149
1018
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
150 started= true;
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
151 int d= n / hexDivisors[i];
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
152 buffer.put(DIGIT[d]);
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
153 n= n - d * hexDivisors[i];
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
154 }
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 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
157
1018
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
158 /* ------------------------------------------------------------ */
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
159 /**
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
160 * Add hex integer BEFORE current getIndex.
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
161 * @param buffer
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
162 * @param n
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
163 */
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
164 public static void prependHexInt(Buffer buffer, int n)
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
165 {
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
166 if (n==0)
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
167 {
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
168 int gi=buffer.getIndex();
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
169 buffer.poke(--gi,(byte)'0');
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
170 buffer.setGetIndex(gi);
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
171 }
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
172 else
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
173 {
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
174 boolean minus=false;
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
175 if (n<0)
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
176 {
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
177 minus=true;
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
178 n=-n;
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
179 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
180
1018
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
181 int gi=buffer.getIndex();
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
182 while(n>0)
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 int d = 0xf&n;
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
185 n=n>>4;
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
186 buffer.poke(--gi,DIGIT[d]);
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
187 }
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
188
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
189 if (minus)
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
190 buffer.poke(--gi,(byte)'-');
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
191 buffer.setGetIndex(gi);
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
192 }
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
193 }
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
194
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
195
1018
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
196 public static void putDecLong(Buffer buffer, long n)
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 if (n < 0)
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
199 {
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
200 buffer.put((byte)'-');
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
201
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
202 if (n == Long.MIN_VALUE)
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
203 {
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
204 buffer.put((byte)'9');
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
205 n= 223372036854775808L;
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
206 }
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
207 else
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
208 n= -n;
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 if (n < 10)
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 buffer.put(DIGIT[(int)n]);
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
214 }
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
215 else
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
216 {
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
217 boolean started= false;
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
218 // This assumes constant time int arithmatic
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
219 for (int i= 0; i < decDivisorsL.length; i++)
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
220 {
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
221 if (n < decDivisorsL[i])
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
222 {
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
223 if (started)
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
224 buffer.put((byte)'0');
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
225 continue;
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
226 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
227
1018
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
228 started= true;
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
229 long d= n / decDivisorsL[i];
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
230 buffer.put(DIGIT[(int)d]);
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
231 n= n - d * decDivisorsL[i];
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
232 }
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
233 }
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
234 }
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
235
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
236 private final static int[] hexDivisors=
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
237 {
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
238 0x10000000,
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
239 0x1000000,
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
240 0x100000,
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
241 0x10000,
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
242 0x1000,
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
243 0x100,
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
244 0x10,
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
245 0x1
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
246 };
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
247
1018
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
248 private final static long[] decDivisorsL=
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
249 {
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
250 1000000000000000000L,
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
251 100000000000000000L,
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
252 10000000000000000L,
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
253 1000000000000000L,
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
254 100000000000000L,
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
255 10000000000000L,
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
256 1000000000000L,
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
257 100000000000L,
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
258 10000000000L,
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
259 1000000000L,
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
260 100000000L,
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
261 10000000L,
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
262 1000000L,
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
263 100000L,
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
264 10000L,
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
265 1000L,
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
266 100L,
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
267 10L,
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
268 1L
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
269 };
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
270
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
271
1018
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
272 public static void putCRLF(Buffer buffer)
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
273 {
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
274 buffer.put((byte)13);
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
275 buffer.put((byte)10);
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
276 }
1020
6be43ef1eb96 HttpHeaderValues uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents: 1019
diff changeset
277 /*
1018
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
278 public static boolean isPrefix(Buffer prefix,Buffer buffer)
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
279 {
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
280 if (prefix.length()>buffer.length())
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
281 return false;
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
282 int bi=buffer.getIndex();
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
283 for (int i=prefix.getIndex(); i<prefix.putIndex();i++)
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
284 if (prefix.peek(i)!=buffer.peek(bi++))
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
285 return false;
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
286 return true;
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
287 }
1020
6be43ef1eb96 HttpHeaderValues uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents: 1019
diff changeset
288 */
1018
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
289 public static String to8859_1_String(Buffer buffer)
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
290 {
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
291 if (buffer instanceof CachedBuffer)
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
292 return buffer.toString();
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
293 return buffer.toString(StringUtil.__ISO_8859_1_CHARSET);
4dc1e1a18661 remove HttpSchemes
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
294 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
295 }