Mercurial Hosting > luan
annotate src/org/eclipse/jetty/io/JBuffer.java @ 1053:7e4b41247544
fix JBuffer.array()
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 08 Nov 2016 00:32:02 -0700 |
parents | 4a2489f1d5fe |
children | 87275900646e |
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() { |
1053 | 37 return bb.array(); |
38 } | |
39 | |
40 public boolean hasArray() { | |
41 return bb.hasArray(); | |
1046 | 42 } |
43 | |
1048 | 44 public JBuffer duplicate() { |
1046 | 45 return new JBuffer(bb.duplicate()); |
46 } | |
47 | |
48 public int remaining() { | |
49 return bb.remaining(); | |
50 } | |
51 | |
52 public boolean isReadOnly() { | |
53 return bb.isReadOnly(); | |
54 } | |
55 | |
56 public boolean hasRemaining() { | |
57 return bb.hasRemaining(); | |
58 } | |
59 | |
60 public byte get() { | |
61 return bb.get(); | |
62 } | |
63 | |
64 public void compact() { | |
65 int n = bb.remaining(); | |
66 bb.compact(); | |
67 bb.position(0); | |
68 bb.limit(n); | |
69 } | |
70 | |
71 public int capacity() { | |
72 return bb.capacity(); | |
73 } | |
74 | |
75 | |
76 | |
77 public ByteBuffer getByteBuffer() { | |
78 ByteBuffer dup = bb.duplicate(); | |
79 dup.limit(dup.capacity()); | |
80 return dup; | |
81 } | |
82 | |
83 public int getIndex() { | |
84 return bb.position(); | |
85 } | |
86 | |
87 public void clear() { | |
88 bb.position(0); | |
89 bb.limit(0); | |
90 } | |
91 | |
92 public int space() { | |
93 return bb.capacity() - bb.limit(); | |
94 } | |
95 | |
96 | |
97 public int get(byte[] b, int offset, int length) { | |
98 int remaining = bb.remaining(); | |
99 if( remaining == 0 ) | |
100 return -1; | |
101 if( length > remaining ) | |
102 length = remaining; | |
103 bb.get(b,offset,length); | |
104 return length; | |
105 } | |
106 | |
107 | |
1048 | 108 public int put(JBuffer src) { |
1046 | 109 return put(src.asArray()); |
110 } | |
111 | |
112 public void put(byte b) | |
113 { | |
114 ByteBuffer dup = bb.duplicate(); | |
115 dup.position(bb.limit()); | |
116 dup.limit(bb.capacity()); | |
117 dup.put(b); | |
118 bb.limit(bb.limit()+1); | |
119 } | |
120 | |
1049
4afdf0f0c5bc
remove unused JBuffer methods
Franklin Schmidt <fschmidt@gmail.com>
parents:
1048
diff
changeset
|
121 private int put(byte[] b, int offset, int length) { |
1046 | 122 ByteBuffer dup = bb.duplicate(); |
123 int put = bb.limit(); | |
124 int capacity = bb.capacity(); | |
125 dup.position(put); | |
126 dup.limit(capacity); | |
127 if( length > capacity - put ) | |
128 length = capacity - put; | |
129 dup.put(b,offset,length); | |
130 bb.limit(put+length); | |
131 return length; | |
132 } | |
133 | |
134 public int put(byte[] b) { | |
135 return put(b,0,b.length); | |
136 } | |
137 | |
138 public final int putIndex() { | |
139 return bb.limit(); | |
140 } | |
141 | |
142 public void setGetIndex(int getIndex) { | |
143 bb.position(getIndex); | |
144 } | |
145 | |
146 public void setPutIndex(int putIndex) { | |
147 bb.limit(putIndex); | |
148 } | |
149 | |
1051
1ab2dd0a7db5
remove JBuffer.get(int)
Franklin Schmidt <fschmidt@gmail.com>
parents:
1050
diff
changeset
|
150 public void skip(int n) { |
1046 | 151 if (remaining() < n) n = remaining(); |
152 bb.position(bb.position() + n); | |
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 | |
217 | |
1052
4a2489f1d5fe
remove JBuffer.peek()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1051
diff
changeset
|
218 public byte get(int index) { |
1046 | 219 return bb.get(index); |
220 } | |
221 | |
222 } |