1046
|
1 // tmp class to implement Buffer until I can get rid of it
|
|
2
|
|
3 package org.eclipse.jetty.io;
|
|
4
|
|
5 import java.io.InputStream;
|
|
6 import java.io.IOException;
|
|
7 import java.nio.ByteBuffer;
|
|
8 import java.nio.channels.Channels;
|
|
9 import java.nio.channels.ReadableByteChannel;
|
|
10 import org.slf4j.Logger;
|
|
11 import org.slf4j.LoggerFactory;
|
|
12 import org.eclipse.jetty.util.TypeUtil;
|
|
13
|
|
14
|
1048
|
15 public final class JBuffer {
|
1046
|
16 private static final Logger LOG = LoggerFactory.getLogger(JBuffer.class);
|
|
17
|
|
18 private final ByteBuffer bb;
|
|
19
|
|
20 public JBuffer(ByteBuffer bb) {
|
|
21 this.bb = bb;
|
|
22 }
|
|
23
|
|
24 public byte[] array() {
|
|
25 return bb.hasArray() ? bb.array() : null;
|
|
26 }
|
|
27
|
1048
|
28 public JBuffer duplicate() {
|
1046
|
29 return new JBuffer(bb.duplicate());
|
|
30 }
|
|
31
|
|
32 public int remaining() {
|
|
33 return bb.remaining();
|
|
34 }
|
|
35
|
|
36 public boolean isReadOnly() {
|
|
37 return bb.isReadOnly();
|
|
38 }
|
|
39
|
|
40 public boolean hasRemaining() {
|
|
41 return bb.hasRemaining();
|
|
42 }
|
|
43
|
|
44 public byte get() {
|
|
45 return bb.get();
|
|
46 }
|
|
47
|
|
48 public void compact() {
|
|
49 int n = bb.remaining();
|
|
50 bb.compact();
|
|
51 bb.position(0);
|
|
52 bb.limit(n);
|
|
53 }
|
|
54
|
|
55 public int capacity() {
|
|
56 return bb.capacity();
|
|
57 }
|
|
58
|
|
59 public boolean isDirect() {
|
|
60 return bb.isDirect();
|
|
61 }
|
|
62
|
|
63
|
|
64
|
|
65 public ByteBuffer getByteBuffer() {
|
|
66 ByteBuffer dup = bb.duplicate();
|
|
67 dup.limit(dup.capacity());
|
|
68 return dup;
|
|
69 }
|
|
70
|
|
71 public int getIndex() {
|
|
72 return bb.position();
|
|
73 }
|
|
74
|
|
75 public void clear() {
|
|
76 bb.position(0);
|
|
77 bb.limit(0);
|
|
78 }
|
|
79
|
|
80 public int space() {
|
|
81 return bb.capacity() - bb.limit();
|
|
82 }
|
|
83
|
1048
|
84 public JBuffer buffer() {
|
1046
|
85 return this;
|
|
86 }
|
|
87
|
|
88
|
1048
|
89 public JBuffer get(int length) {
|
1046
|
90 ByteBuffer dup = bb.duplicate();
|
|
91 int end = bb.position()+length;
|
|
92 dup.limit(end);
|
|
93 bb.position(end);
|
|
94 return new JBuffer(dup);
|
|
95 }
|
|
96
|
|
97 public int get(byte[] b, int offset, int length) {
|
|
98 int remaining = bb.remaining();
|
|
99 if( remaining == 0 )
|
|
100 return -1;
|
|
101 if( length > remaining )
|
|
102 length = remaining;
|
|
103 bb.get(b,offset,length);
|
|
104 return length;
|
|
105 }
|
|
106
|
|
107
|
1048
|
108 public int put(JBuffer src) {
|
1046
|
109 return put(src.asArray());
|
|
110 }
|
|
111
|
|
112 public void put(byte b)
|
|
113 {
|
|
114 ByteBuffer dup = bb.duplicate();
|
|
115 dup.position(bb.limit());
|
|
116 dup.limit(bb.capacity());
|
|
117 dup.put(b);
|
|
118 bb.limit(bb.limit()+1);
|
|
119 }
|
|
120
|
|
121 public int put(byte[] b, int offset, int length) {
|
|
122 ByteBuffer dup = bb.duplicate();
|
|
123 int put = bb.limit();
|
|
124 int capacity = bb.capacity();
|
|
125 dup.position(put);
|
|
126 dup.limit(capacity);
|
|
127 if( length > capacity - put )
|
|
128 length = capacity - put;
|
|
129 dup.put(b,offset,length);
|
|
130 bb.limit(put+length);
|
|
131 return length;
|
|
132 }
|
|
133
|
|
134 public int put(byte[] b) {
|
|
135 return put(b,0,b.length);
|
|
136 }
|
|
137
|
|
138 public final int putIndex() {
|
|
139 return bb.limit();
|
|
140 }
|
|
141
|
|
142 public void setGetIndex(int getIndex) {
|
|
143 bb.position(getIndex);
|
|
144 }
|
|
145
|
|
146 public void setPutIndex(int putIndex) {
|
|
147 bb.limit(putIndex);
|
|
148 }
|
|
149
|
|
150 public int skip(int n) {
|
|
151 if (remaining() < n) n = remaining();
|
|
152 bb.position(bb.position() + n);
|
|
153 return n;
|
|
154 }
|
|
155
|
1048
|
156 public JBuffer slice() {
|
1046
|
157 return duplicate();
|
|
158 }
|
|
159
|
1048
|
160 public final JBuffer sliceFrom(int index) {
|
1046
|
161 ByteBuffer dup = bb.duplicate();
|
|
162 dup.position(index);
|
|
163 dup.limit(bb.position()-1);
|
|
164 return new JBuffer(dup);
|
|
165 }
|
|
166
|
|
167 public int readFrom(InputStream in,int max) throws IOException {
|
|
168 ByteBuffer dup = bb.duplicate();
|
|
169 int put = bb.limit();
|
|
170 dup.limit( Math.min(put+max,bb.capacity()) );
|
|
171 dup.position(put);
|
|
172
|
|
173 ReadableByteChannel chan = Channels.newChannel(in);
|
|
174 int n = chan.read(dup);
|
|
175
|
|
176 if( n > 0 )
|
|
177 bb.limit(put+n);
|
|
178 return n;
|
|
179 }
|
|
180
|
|
181 public final byte[] asArray() {
|
|
182 byte[] bytes = new byte[remaining()];
|
|
183 bb.duplicate().get(bytes);
|
|
184 return bytes;
|
|
185 }
|
|
186
|
|
187 @Override
|
|
188 public String toString()
|
|
189 {
|
|
190 return toString("ISO-8859-1");
|
|
191 }
|
|
192
|
|
193 public final String toString(int index, int length) {
|
|
194 ByteBuffer dup = bb.duplicate();
|
|
195 dup.limit(index+length);
|
|
196 dup.position(index);
|
|
197 return new JBuffer(dup).toString();
|
|
198 }
|
|
199
|
|
200 public final String toString(String charset)
|
|
201 {
|
|
202 byte[] bytes = asArray();
|
|
203 try
|
|
204 {
|
|
205 return new String(bytes,charset);
|
|
206 }
|
|
207 catch(Exception e)
|
|
208 {
|
|
209 LOG.warn("",e);
|
|
210 return new String(bytes);
|
|
211 }
|
|
212 }
|
|
213
|
|
214 public String toDetailString()
|
|
215 {
|
|
216 StringBuilder buf = new StringBuilder();
|
|
217 buf.append("[");
|
|
218 buf.append(super.hashCode());
|
|
219 buf.append(",");
|
|
220 buf.append(this.buffer().hashCode());
|
|
221 buf.append(",g=");
|
|
222 buf.append(getIndex());
|
|
223 buf.append(",p=");
|
|
224 buf.append(putIndex());
|
|
225 buf.append(",c=");
|
|
226 buf.append(capacity());
|
|
227 buf.append("]={");
|
|
228 int count = 0;
|
|
229 for (int i = getIndex(); i < putIndex(); i++)
|
|
230 {
|
|
231 byte b = peek(i);
|
|
232 TypeUtil.toHex(b,buf);
|
|
233 if (count++ == 50)
|
|
234 {
|
|
235 if (putIndex() - i > 20)
|
|
236 {
|
|
237 buf.append(" ... ");
|
|
238 i = putIndex() - 20;
|
|
239 }
|
|
240 }
|
|
241 }
|
|
242 buf.append('}');
|
|
243 return buf.toString();
|
|
244 }
|
|
245
|
|
246
|
|
247
|
1048
|
248 private JBuffer pokeBuffer(int index) {
|
|
249 JBuffer dup = duplicate();
|
1046
|
250 dup.setPutIndex(index);
|
|
251 return dup;
|
|
252 }
|
|
253
|
|
254 public int poke(int index, byte b[], int offset, int length) {
|
|
255 return pokeBuffer(index).put(b,offset,length);
|
|
256 }
|
|
257
|
|
258 public void poke(int index, byte b) {
|
|
259 pokeBuffer(index).put(b);
|
|
260 }
|
|
261
|
1048
|
262 public int poke(int index, JBuffer src) {
|
1046
|
263 return pokeBuffer(index).put(src);
|
|
264 }
|
|
265
|
1048
|
266 private JBuffer peekBuffer(int index) {
|
|
267 JBuffer dup = duplicate();
|
1046
|
268 dup.setGetIndex(index);
|
|
269 dup.setPutIndex(dup.capacity());
|
|
270 return dup;
|
|
271 }
|
|
272
|
|
273 public int peek(int index, byte[] b, int offset, int length) {
|
|
274 return peekBuffer(index).get(b,offset,length);
|
|
275 }
|
|
276
|
|
277 public byte peek(int index) {
|
|
278 return bb.get(index);
|
|
279 }
|
|
280
|
|
281 public byte peek() {
|
|
282 return peek(bb.position());
|
|
283 }
|
|
284
|
|
285 }
|