Mercurial Hosting > luan
annotate src/org/eclipse/jetty/io/JBuffer.java @ 1062:4a50422596b6
fix JBuffer.getByteBuffer()
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Wed, 09 Nov 2016 02:04:02 -0700 |
parents | c880589715c7 |
children | 0157e92670f5 |
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 |
1062
4a50422596b6
fix JBuffer.getByteBuffer()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1061
diff
changeset
|
32 public int limit() { |
4a50422596b6
fix JBuffer.getByteBuffer()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1061
diff
changeset
|
33 return bb.limit(); |
4a50422596b6
fix JBuffer.getByteBuffer()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1061
diff
changeset
|
34 } |
4a50422596b6
fix JBuffer.getByteBuffer()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1061
diff
changeset
|
35 |
1050
5ef954fad97b
remove JBuffer.sliceFrom()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1049
diff
changeset
|
36 public void limit(int i) { |
5ef954fad97b
remove JBuffer.sliceFrom()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1049
diff
changeset
|
37 bb.limit(i); |
5ef954fad97b
remove JBuffer.sliceFrom()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1049
diff
changeset
|
38 } |
5ef954fad97b
remove JBuffer.sliceFrom()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1049
diff
changeset
|
39 |
1046 | 40 public byte[] array() { |
1053 | 41 return bb.array(); |
42 } | |
43 | |
44 public boolean hasArray() { | |
45 return bb.hasArray(); | |
1046 | 46 } |
47 | |
1048 | 48 public JBuffer duplicate() { |
1046 | 49 return new JBuffer(bb.duplicate()); |
50 } | |
51 | |
52 public int remaining() { | |
53 return bb.remaining(); | |
54 } | |
55 | |
56 public boolean isReadOnly() { | |
57 return bb.isReadOnly(); | |
58 } | |
59 | |
60 public boolean hasRemaining() { | |
61 return bb.hasRemaining(); | |
62 } | |
63 | |
64 public byte get() { | |
65 return bb.get(); | |
66 } | |
67 | |
1054
87275900646e
remove JBuffer.toString()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1053
diff
changeset
|
68 public void get(byte[] bytes) { |
87275900646e
remove JBuffer.toString()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1053
diff
changeset
|
69 bb.get(bytes); |
87275900646e
remove JBuffer.toString()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1053
diff
changeset
|
70 } |
87275900646e
remove JBuffer.toString()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1053
diff
changeset
|
71 |
1046 | 72 public void compact() { |
73 int n = bb.remaining(); | |
74 bb.compact(); | |
75 bb.position(0); | |
76 bb.limit(n); | |
77 } | |
78 | |
79 public int capacity() { | |
80 return bb.capacity(); | |
81 } | |
82 | |
83 | |
84 | |
85 public ByteBuffer getByteBuffer() { | |
1062
4a50422596b6
fix JBuffer.getByteBuffer()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1061
diff
changeset
|
86 return bb; |
1046 | 87 } |
88 | |
89 public int getIndex() { | |
90 return bb.position(); | |
91 } | |
92 | |
93 public void clear() { | |
94 bb.position(0); | |
95 bb.limit(0); | |
96 } | |
97 | |
98 public int space() { | |
99 return bb.capacity() - bb.limit(); | |
100 } | |
101 | |
102 | |
1060 | 103 public void get(byte[] b, int offset, int length) { |
1046 | 104 bb.get(b,offset,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 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 | |
1060 | 165 private final byte[] asArray() { |
1046 | 166 byte[] bytes = new byte[remaining()]; |
167 bb.duplicate().get(bytes); | |
168 return bytes; | |
169 } | |
170 | |
171 @Override | |
1054
87275900646e
remove JBuffer.toString()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1053
diff
changeset
|
172 public String toString() { |
87275900646e
remove JBuffer.toString()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1053
diff
changeset
|
173 // return toString("ISO-8859-1"); |
87275900646e
remove JBuffer.toString()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1053
diff
changeset
|
174 // Thread.dumpStack(); |
87275900646e
remove JBuffer.toString()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1053
diff
changeset
|
175 throw new RuntimeException("toString"); |
1046 | 176 } |
177 | |
178 | |
1052
4a2489f1d5fe
remove JBuffer.peek()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1051
diff
changeset
|
179 public byte get(int index) { |
1046 | 180 return bb.get(index); |
181 } | |
182 | |
183 } |