Mercurial Hosting > luan
annotate src/org/eclipse/jetty/io/JBuffer.java @ 1050:5ef954fad97b
remove JBuffer.sliceFrom()
| author | Franklin Schmidt <fschmidt@gmail.com> |
|---|---|
| date | Mon, 07 Nov 2016 23:49:41 -0700 |
| parents | 4afdf0f0c5bc |
| children | 1ab2dd0a7db5 |
| 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 | |
| 1048 | 93 public JBuffer get(int length) { |
| 1046 | 94 ByteBuffer dup = bb.duplicate(); |
| 95 int end = bb.position()+length; | |
| 96 dup.limit(end); | |
| 97 bb.position(end); | |
| 98 return new JBuffer(dup); | |
| 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 | |
| 154 public int skip(int n) { | |
| 155 if (remaining() < n) n = remaining(); | |
| 156 bb.position(bb.position() + n); | |
| 157 return n; | |
| 158 } | |
| 159 | |
| 160 public int readFrom(InputStream in,int max) throws IOException { | |
| 161 ByteBuffer dup = bb.duplicate(); | |
| 162 int put = bb.limit(); | |
| 163 dup.limit( Math.min(put+max,bb.capacity()) ); | |
| 164 dup.position(put); | |
| 165 | |
| 166 ReadableByteChannel chan = Channels.newChannel(in); | |
| 167 int n = chan.read(dup); | |
| 168 | |
| 169 if( n > 0 ) | |
| 170 bb.limit(put+n); | |
| 171 return n; | |
| 172 } | |
| 173 | |
| 174 public final byte[] asArray() { | |
| 175 byte[] bytes = new byte[remaining()]; | |
| 176 bb.duplicate().get(bytes); | |
| 177 return bytes; | |
| 178 } | |
| 179 | |
| 180 @Override | |
| 181 public String toString() | |
| 182 { | |
| 183 return toString("ISO-8859-1"); | |
| 184 } | |
| 185 | |
| 186 public final String toString(int index, int length) { | |
| 187 ByteBuffer dup = bb.duplicate(); | |
| 188 dup.limit(index+length); | |
| 189 dup.position(index); | |
| 190 return new JBuffer(dup).toString(); | |
| 191 } | |
| 192 | |
|
1049
4afdf0f0c5bc
remove unused JBuffer methods
Franklin Schmidt <fschmidt@gmail.com>
parents:
1048
diff
changeset
|
193 private final String toString(String charset) |
| 1046 | 194 { |
| 195 byte[] bytes = asArray(); | |
| 196 try | |
| 197 { | |
| 198 return new String(bytes,charset); | |
| 199 } | |
| 200 catch(Exception e) | |
| 201 { | |
| 202 LOG.warn("",e); | |
| 203 return new String(bytes); | |
| 204 } | |
| 205 } | |
| 206 | |
| 207 | |
| 1048 | 208 private JBuffer pokeBuffer(int index) { |
| 209 JBuffer dup = duplicate(); | |
| 1046 | 210 dup.setPutIndex(index); |
| 211 return dup; | |
| 212 } | |
| 213 | |
| 214 public int poke(int index, byte b[], int offset, int length) { | |
| 215 return pokeBuffer(index).put(b,offset,length); | |
| 216 } | |
| 217 | |
| 218 public void poke(int index, byte b) { | |
| 219 pokeBuffer(index).put(b); | |
| 220 } | |
| 221 | |
| 1048 | 222 private JBuffer peekBuffer(int index) { |
| 223 JBuffer dup = duplicate(); | |
| 1046 | 224 dup.setGetIndex(index); |
| 225 dup.setPutIndex(dup.capacity()); | |
| 226 return dup; | |
| 227 } | |
| 228 | |
| 229 public byte peek(int index) { | |
| 230 return bb.get(index); | |
| 231 } | |
| 232 | |
| 233 public byte peek() { | |
| 234 return peek(bb.position()); | |
| 235 } | |
| 236 | |
| 237 } |
