Mercurial Hosting > luan
annotate src/org/eclipse/jetty/io/JBuffer.java @ 1061:c880589715c7
remove JBuffer.setGetIndex()
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 08 Nov 2016 05:54:53 -0700 |
parents | 957f758dcffc |
children | 4a50422596b6 |
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 | |
1060 | 101 public void get(byte[] b, int offset, int length) { |
1046 | 102 bb.get(b,offset,length); |
103 } | |
104 | |
105 | |
1048 | 106 public int put(JBuffer src) { |
1046 | 107 return put(src.asArray()); |
108 } | |
109 | |
110 public void put(byte b) | |
111 { | |
112 ByteBuffer dup = bb.duplicate(); | |
113 dup.position(bb.limit()); | |
114 dup.limit(bb.capacity()); | |
115 dup.put(b); | |
116 bb.limit(bb.limit()+1); | |
117 } | |
118 | |
1049
4afdf0f0c5bc
remove unused JBuffer methods
Franklin Schmidt <fschmidt@gmail.com>
parents:
1048
diff
changeset
|
119 private int put(byte[] b, int offset, int length) { |
1046 | 120 ByteBuffer dup = bb.duplicate(); |
121 int put = bb.limit(); | |
122 int capacity = bb.capacity(); | |
123 dup.position(put); | |
124 dup.limit(capacity); | |
125 if( length > capacity - put ) | |
126 length = capacity - put; | |
127 dup.put(b,offset,length); | |
128 bb.limit(put+length); | |
129 return length; | |
130 } | |
131 | |
132 public int put(byte[] b) { | |
133 return put(b,0,b.length); | |
134 } | |
135 | |
136 public final int putIndex() { | |
137 return bb.limit(); | |
138 } | |
139 | |
140 public void setPutIndex(int putIndex) { | |
141 bb.limit(putIndex); | |
142 } | |
143 | |
1051
1ab2dd0a7db5
remove JBuffer.get(int)
Franklin Schmidt <fschmidt@gmail.com>
parents:
1050
diff
changeset
|
144 public void skip(int n) { |
1046 | 145 if (remaining() < n) n = remaining(); |
146 bb.position(bb.position() + n); | |
147 } | |
148 | |
149 public int readFrom(InputStream in,int max) throws IOException { | |
150 ByteBuffer dup = bb.duplicate(); | |
151 int put = bb.limit(); | |
152 dup.limit( Math.min(put+max,bb.capacity()) ); | |
153 dup.position(put); | |
154 | |
155 ReadableByteChannel chan = Channels.newChannel(in); | |
156 int n = chan.read(dup); | |
157 | |
158 if( n > 0 ) | |
159 bb.limit(put+n); | |
160 return n; | |
161 } | |
162 | |
1060 | 163 private final byte[] asArray() { |
1046 | 164 byte[] bytes = new byte[remaining()]; |
165 bb.duplicate().get(bytes); | |
166 return bytes; | |
167 } | |
168 | |
169 @Override | |
1054
87275900646e
remove JBuffer.toString()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1053
diff
changeset
|
170 public String toString() { |
87275900646e
remove JBuffer.toString()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1053
diff
changeset
|
171 // return toString("ISO-8859-1"); |
87275900646e
remove JBuffer.toString()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1053
diff
changeset
|
172 // Thread.dumpStack(); |
87275900646e
remove JBuffer.toString()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1053
diff
changeset
|
173 throw new RuntimeException("toString"); |
1046 | 174 } |
175 | |
176 | |
1052
4a2489f1d5fe
remove JBuffer.peek()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1051
diff
changeset
|
177 public byte get(int index) { |
1046 | 178 return bb.get(index); |
179 } | |
180 | |
181 } |