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