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