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