Mercurial Hosting > luan
annotate src/org/eclipse/jetty/io/JBuffer.java @ 1063:0157e92670f5
remove JBuffer.getIndex()
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Wed, 09 Nov 2016 02:08:33 -0700 |
parents | 4a50422596b6 |
children | a0abb16cf6e7 |
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 void clear() { | |
90 bb.position(0); | |
91 bb.limit(0); | |
92 } | |
93 | |
94 public int space() { | |
95 return bb.capacity() - bb.limit(); | |
96 } | |
97 | |
98 | |
1060 | 99 public void get(byte[] b, int offset, int length) { |
1046 | 100 bb.get(b,offset,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 setPutIndex(int putIndex) { | |
139 bb.limit(putIndex); | |
140 } | |
141 | |
1051
1ab2dd0a7db5
remove JBuffer.get(int)
Franklin Schmidt <fschmidt@gmail.com>
parents:
1050
diff
changeset
|
142 public void skip(int n) { |
1046 | 143 if (remaining() < n) n = remaining(); |
144 bb.position(bb.position() + n); | |
145 } | |
146 | |
147 public int readFrom(InputStream in,int max) throws IOException { | |
148 ByteBuffer dup = bb.duplicate(); | |
149 int put = bb.limit(); | |
150 dup.limit( Math.min(put+max,bb.capacity()) ); | |
151 dup.position(put); | |
152 | |
153 ReadableByteChannel chan = Channels.newChannel(in); | |
154 int n = chan.read(dup); | |
155 | |
156 if( n > 0 ) | |
157 bb.limit(put+n); | |
158 return n; | |
159 } | |
160 | |
1060 | 161 private final byte[] asArray() { |
1046 | 162 byte[] bytes = new byte[remaining()]; |
163 bb.duplicate().get(bytes); | |
164 return bytes; | |
165 } | |
166 | |
167 @Override | |
1054
87275900646e
remove JBuffer.toString()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1053
diff
changeset
|
168 public String toString() { |
87275900646e
remove JBuffer.toString()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1053
diff
changeset
|
169 // return toString("ISO-8859-1"); |
87275900646e
remove JBuffer.toString()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1053
diff
changeset
|
170 // Thread.dumpStack(); |
87275900646e
remove JBuffer.toString()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1053
diff
changeset
|
171 throw new RuntimeException("toString"); |
1046 | 172 } |
173 | |
174 | |
1052
4a2489f1d5fe
remove JBuffer.peek()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1051
diff
changeset
|
175 public byte get(int index) { |
1046 | 176 return bb.get(index); |
177 } | |
178 | |
179 } |