Mercurial Hosting > luan
annotate src/org/eclipse/jetty/io/JBuffer.java @ 1049:4afdf0f0c5bc
remove unused JBuffer methods
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Mon, 07 Nov 2016 23:34:48 -0700 |
parents | 2b769da7f67d |
children | 5ef954fad97b |
rev | line source |
---|---|
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 | |
60 | |
61 public ByteBuffer getByteBuffer() { | |
62 ByteBuffer dup = bb.duplicate(); | |
63 dup.limit(dup.capacity()); | |
64 return dup; | |
65 } | |
66 | |
67 public int getIndex() { | |
68 return bb.position(); | |
69 } | |
70 | |
71 public void clear() { | |
72 bb.position(0); | |
73 bb.limit(0); | |
74 } | |
75 | |
76 public int space() { | |
77 return bb.capacity() - bb.limit(); | |
78 } | |
79 | |
80 | |
1048 | 81 public JBuffer get(int length) { |
1046 | 82 ByteBuffer dup = bb.duplicate(); |
83 int end = bb.position()+length; | |
84 dup.limit(end); | |
85 bb.position(end); | |
86 return new JBuffer(dup); | |
87 } | |
88 | |
89 public int get(byte[] b, int offset, int length) { | |
90 int remaining = bb.remaining(); | |
91 if( remaining == 0 ) | |
92 return -1; | |
93 if( length > remaining ) | |
94 length = remaining; | |
95 bb.get(b,offset,length); | |
96 return length; | |
97 } | |
98 | |
99 | |
1048 | 100 public int put(JBuffer src) { |
1046 | 101 return put(src.asArray()); |
102 } | |
103 | |
104 public void put(byte b) | |
105 { | |
106 ByteBuffer dup = bb.duplicate(); | |
107 dup.position(bb.limit()); | |
108 dup.limit(bb.capacity()); | |
109 dup.put(b); | |
110 bb.limit(bb.limit()+1); | |
111 } | |
112 | |
1049
4afdf0f0c5bc
remove unused JBuffer methods
Franklin Schmidt <fschmidt@gmail.com>
parents:
1048
diff
changeset
|
113 private int put(byte[] b, int offset, int length) { |
1046 | 114 ByteBuffer dup = bb.duplicate(); |
115 int put = bb.limit(); | |
116 int capacity = bb.capacity(); | |
117 dup.position(put); | |
118 dup.limit(capacity); | |
119 if( length > capacity - put ) | |
120 length = capacity - put; | |
121 dup.put(b,offset,length); | |
122 bb.limit(put+length); | |
123 return length; | |
124 } | |
125 | |
126 public int put(byte[] b) { | |
127 return put(b,0,b.length); | |
128 } | |
129 | |
130 public final int putIndex() { | |
131 return bb.limit(); | |
132 } | |
133 | |
134 public void setGetIndex(int getIndex) { | |
135 bb.position(getIndex); | |
136 } | |
137 | |
138 public void setPutIndex(int putIndex) { | |
139 bb.limit(putIndex); | |
140 } | |
141 | |
142 public int skip(int n) { | |
143 if (remaining() < n) n = remaining(); | |
144 bb.position(bb.position() + n); | |
145 return n; | |
146 } | |
147 | |
1048 | 148 public final JBuffer sliceFrom(int index) { |
1046 | 149 ByteBuffer dup = bb.duplicate(); |
150 dup.position(index); | |
151 dup.limit(bb.position()-1); | |
152 return new JBuffer(dup); | |
153 } | |
154 | |
155 public int readFrom(InputStream in,int max) throws IOException { | |
156 ByteBuffer dup = bb.duplicate(); | |
157 int put = bb.limit(); | |
158 dup.limit( Math.min(put+max,bb.capacity()) ); | |
159 dup.position(put); | |
160 | |
161 ReadableByteChannel chan = Channels.newChannel(in); | |
162 int n = chan.read(dup); | |
163 | |
164 if( n > 0 ) | |
165 bb.limit(put+n); | |
166 return n; | |
167 } | |
168 | |
169 public final byte[] asArray() { | |
170 byte[] bytes = new byte[remaining()]; | |
171 bb.duplicate().get(bytes); | |
172 return bytes; | |
173 } | |
174 | |
175 @Override | |
176 public String toString() | |
177 { | |
178 return toString("ISO-8859-1"); | |
179 } | |
180 | |
181 public final String toString(int index, int length) { | |
182 ByteBuffer dup = bb.duplicate(); | |
183 dup.limit(index+length); | |
184 dup.position(index); | |
185 return new JBuffer(dup).toString(); | |
186 } | |
187 | |
1049
4afdf0f0c5bc
remove unused JBuffer methods
Franklin Schmidt <fschmidt@gmail.com>
parents:
1048
diff
changeset
|
188 private final String toString(String charset) |
1046 | 189 { |
190 byte[] bytes = asArray(); | |
191 try | |
192 { | |
193 return new String(bytes,charset); | |
194 } | |
195 catch(Exception e) | |
196 { | |
197 LOG.warn("",e); | |
198 return new String(bytes); | |
199 } | |
200 } | |
201 | |
202 | |
1048 | 203 private JBuffer pokeBuffer(int index) { |
204 JBuffer dup = duplicate(); | |
1046 | 205 dup.setPutIndex(index); |
206 return dup; | |
207 } | |
208 | |
209 public int poke(int index, byte b[], int offset, int length) { | |
210 return pokeBuffer(index).put(b,offset,length); | |
211 } | |
212 | |
213 public void poke(int index, byte b) { | |
214 pokeBuffer(index).put(b); | |
215 } | |
216 | |
1048 | 217 private JBuffer peekBuffer(int index) { |
218 JBuffer dup = duplicate(); | |
1046 | 219 dup.setGetIndex(index); |
220 dup.setPutIndex(dup.capacity()); | |
221 return dup; | |
222 } | |
223 | |
224 public byte peek(int index) { | |
225 return bb.get(index); | |
226 } | |
227 | |
228 public byte peek() { | |
229 return peek(bb.position()); | |
230 } | |
231 | |
232 } |