Mercurial Hosting > luan
annotate src/org/eclipse/jetty/io/JBuffer.java @ 1054:87275900646e
remove JBuffer.toString()
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 08 Nov 2016 01:03:02 -0700 |
parents | 7e4b41247544 |
children | 013939bfc9e8 |
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 | |
1054
87275900646e
remove JBuffer.toString()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1053
diff
changeset
|
64 public void get(byte[] bytes) { |
87275900646e
remove JBuffer.toString()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1053
diff
changeset
|
65 bb.get(bytes); |
87275900646e
remove JBuffer.toString()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1053
diff
changeset
|
66 } |
87275900646e
remove JBuffer.toString()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1053
diff
changeset
|
67 |
1046 | 68 public void compact() { |
69 int n = bb.remaining(); | |
70 bb.compact(); | |
71 bb.position(0); | |
72 bb.limit(n); | |
73 } | |
74 | |
75 public int capacity() { | |
76 return bb.capacity(); | |
77 } | |
78 | |
79 | |
80 | |
81 public ByteBuffer getByteBuffer() { | |
82 ByteBuffer dup = bb.duplicate(); | |
83 dup.limit(dup.capacity()); | |
84 return dup; | |
85 } | |
86 | |
87 public int getIndex() { | |
88 return bb.position(); | |
89 } | |
90 | |
91 public void clear() { | |
92 bb.position(0); | |
93 bb.limit(0); | |
94 } | |
95 | |
96 public int space() { | |
97 return bb.capacity() - bb.limit(); | |
98 } | |
99 | |
100 | |
101 public int get(byte[] b, int offset, int length) { | |
102 int remaining = bb.remaining(); | |
103 if( remaining == 0 ) | |
104 return -1; | |
105 if( length > remaining ) | |
106 length = remaining; | |
107 bb.get(b,offset,length); | |
108 return length; | |
109 } | |
110 | |
111 | |
1048 | 112 public int put(JBuffer src) { |
1046 | 113 return put(src.asArray()); |
114 } | |
115 | |
116 public void put(byte b) | |
117 { | |
118 ByteBuffer dup = bb.duplicate(); | |
119 dup.position(bb.limit()); | |
120 dup.limit(bb.capacity()); | |
121 dup.put(b); | |
122 bb.limit(bb.limit()+1); | |
123 } | |
124 | |
1049
4afdf0f0c5bc
remove unused JBuffer methods
Franklin Schmidt <fschmidt@gmail.com>
parents:
1048
diff
changeset
|
125 private int put(byte[] b, int offset, int length) { |
1046 | 126 ByteBuffer dup = bb.duplicate(); |
127 int put = bb.limit(); | |
128 int capacity = bb.capacity(); | |
129 dup.position(put); | |
130 dup.limit(capacity); | |
131 if( length > capacity - put ) | |
132 length = capacity - put; | |
133 dup.put(b,offset,length); | |
134 bb.limit(put+length); | |
135 return length; | |
136 } | |
137 | |
138 public int put(byte[] b) { | |
139 return put(b,0,b.length); | |
140 } | |
141 | |
142 public final int putIndex() { | |
143 return bb.limit(); | |
144 } | |
145 | |
146 public void setGetIndex(int getIndex) { | |
147 bb.position(getIndex); | |
148 } | |
149 | |
150 public void setPutIndex(int putIndex) { | |
151 bb.limit(putIndex); | |
152 } | |
153 | |
1051
1ab2dd0a7db5
remove JBuffer.get(int)
Franklin Schmidt <fschmidt@gmail.com>
parents:
1050
diff
changeset
|
154 public void skip(int n) { |
1046 | 155 if (remaining() < n) n = remaining(); |
156 bb.position(bb.position() + n); | |
157 } | |
158 | |
159 public int readFrom(InputStream in,int max) throws IOException { | |
160 ByteBuffer dup = bb.duplicate(); | |
161 int put = bb.limit(); | |
162 dup.limit( Math.min(put+max,bb.capacity()) ); | |
163 dup.position(put); | |
164 | |
165 ReadableByteChannel chan = Channels.newChannel(in); | |
166 int n = chan.read(dup); | |
167 | |
168 if( n > 0 ) | |
169 bb.limit(put+n); | |
170 return n; | |
171 } | |
172 | |
173 public final byte[] asArray() { | |
174 byte[] bytes = new byte[remaining()]; | |
175 bb.duplicate().get(bytes); | |
176 return bytes; | |
177 } | |
178 | |
179 @Override | |
1054
87275900646e
remove JBuffer.toString()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1053
diff
changeset
|
180 public String toString() { |
87275900646e
remove JBuffer.toString()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1053
diff
changeset
|
181 // return toString("ISO-8859-1"); |
87275900646e
remove JBuffer.toString()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1053
diff
changeset
|
182 // Thread.dumpStack(); |
87275900646e
remove JBuffer.toString()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1053
diff
changeset
|
183 throw new RuntimeException("toString"); |
1046 | 184 } |
185 | |
186 | |
1048 | 187 private JBuffer pokeBuffer(int index) { |
188 JBuffer dup = duplicate(); | |
1046 | 189 dup.setPutIndex(index); |
190 return dup; | |
191 } | |
192 | |
193 public int poke(int index, byte b[], int offset, int length) { | |
194 return pokeBuffer(index).put(b,offset,length); | |
195 } | |
196 | |
197 public void poke(int index, byte b) { | |
198 pokeBuffer(index).put(b); | |
199 } | |
200 | |
201 | |
1052
4a2489f1d5fe
remove JBuffer.peek()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1051
diff
changeset
|
202 public byte get(int index) { |
1046 | 203 return bb.get(index); |
204 } | |
205 | |
206 } |