comparison src/org/eclipse/jetty/io/JBuffer.java @ 1046:a8c92b0a08ed

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