Mercurial Hosting > luan
annotate src/org/eclipse/jetty/io/JBuffer.java @ 1065:158d1e6ac17f
fix JBuffer.compact()
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Wed, 09 Nov 2016 04:36:05 -0700 |
parents | a0abb16cf6e7 |
children | bbbda7c6e8ec |
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 org.slf4j.Logger; | |
9 import org.slf4j.LoggerFactory; | |
10 import org.eclipse.jetty.util.TypeUtil; | |
11 | |
12 | |
1048 | 13 public final class JBuffer { |
1046 | 14 private static final Logger LOG = LoggerFactory.getLogger(JBuffer.class); |
15 | |
16 private final ByteBuffer bb; | |
17 | |
18 public JBuffer(ByteBuffer bb) { | |
19 this.bb = bb; | |
20 } | |
21 | |
1050
5ef954fad97b
remove JBuffer.sliceFrom()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1049
diff
changeset
|
22 public int position() { |
5ef954fad97b
remove JBuffer.sliceFrom()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1049
diff
changeset
|
23 return bb.position(); |
5ef954fad97b
remove JBuffer.sliceFrom()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1049
diff
changeset
|
24 } |
5ef954fad97b
remove JBuffer.sliceFrom()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1049
diff
changeset
|
25 |
5ef954fad97b
remove JBuffer.sliceFrom()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1049
diff
changeset
|
26 public void position(int i) { |
5ef954fad97b
remove JBuffer.sliceFrom()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1049
diff
changeset
|
27 bb.position(i); |
5ef954fad97b
remove JBuffer.sliceFrom()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1049
diff
changeset
|
28 } |
5ef954fad97b
remove JBuffer.sliceFrom()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1049
diff
changeset
|
29 |
1062
4a50422596b6
fix JBuffer.getByteBuffer()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1061
diff
changeset
|
30 public int limit() { |
4a50422596b6
fix JBuffer.getByteBuffer()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1061
diff
changeset
|
31 return bb.limit(); |
4a50422596b6
fix JBuffer.getByteBuffer()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1061
diff
changeset
|
32 } |
4a50422596b6
fix JBuffer.getByteBuffer()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1061
diff
changeset
|
33 |
1050
5ef954fad97b
remove JBuffer.sliceFrom()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1049
diff
changeset
|
34 public void limit(int i) { |
5ef954fad97b
remove JBuffer.sliceFrom()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1049
diff
changeset
|
35 bb.limit(i); |
5ef954fad97b
remove JBuffer.sliceFrom()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1049
diff
changeset
|
36 } |
5ef954fad97b
remove JBuffer.sliceFrom()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1049
diff
changeset
|
37 |
1046 | 38 public byte[] array() { |
1053 | 39 return bb.array(); |
40 } | |
41 | |
42 public boolean hasArray() { | |
43 return bb.hasArray(); | |
1046 | 44 } |
45 | |
1048 | 46 public JBuffer duplicate() { |
1046 | 47 return new JBuffer(bb.duplicate()); |
48 } | |
49 | |
50 public int remaining() { | |
51 return bb.remaining(); | |
52 } | |
53 | |
54 public boolean isReadOnly() { | |
55 return bb.isReadOnly(); | |
56 } | |
57 | |
58 public boolean hasRemaining() { | |
59 return bb.hasRemaining(); | |
60 } | |
61 | |
62 public byte get() { | |
63 return bb.get(); | |
64 } | |
65 | |
1054
87275900646e
remove JBuffer.toString()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1053
diff
changeset
|
66 public void get(byte[] bytes) { |
87275900646e
remove JBuffer.toString()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1053
diff
changeset
|
67 bb.get(bytes); |
87275900646e
remove JBuffer.toString()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1053
diff
changeset
|
68 } |
87275900646e
remove JBuffer.toString()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1053
diff
changeset
|
69 |
1046 | 70 public void compact() { |
71 bb.compact(); | |
72 } | |
73 | |
74 public int capacity() { | |
75 return bb.capacity(); | |
76 } | |
77 | |
78 | |
79 | |
80 public ByteBuffer getByteBuffer() { | |
1062
4a50422596b6
fix JBuffer.getByteBuffer()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1061
diff
changeset
|
81 return bb; |
1046 | 82 } |
83 | |
1065
158d1e6ac17f
fix JBuffer.compact()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1064
diff
changeset
|
84 public void clearJ() { |
1046 | 85 bb.position(0); |
86 bb.limit(0); | |
87 } | |
88 | |
89 public int space() { | |
90 return bb.capacity() - bb.limit(); | |
91 } | |
92 | |
93 | |
1060 | 94 public void get(byte[] b, int offset, int length) { |
1046 | 95 bb.get(b,offset,length); |
96 } | |
97 | |
98 | |
1048 | 99 public int put(JBuffer src) { |
1046 | 100 return put(src.asArray()); |
101 } | |
102 | |
103 public void put(byte b) | |
104 { | |
105 ByteBuffer dup = bb.duplicate(); | |
106 dup.position(bb.limit()); | |
107 dup.limit(bb.capacity()); | |
108 dup.put(b); | |
109 bb.limit(bb.limit()+1); | |
110 } | |
111 | |
1049
4afdf0f0c5bc
remove unused JBuffer methods
Franklin Schmidt <fschmidt@gmail.com>
parents:
1048
diff
changeset
|
112 private int put(byte[] b, int offset, int length) { |
1046 | 113 ByteBuffer dup = bb.duplicate(); |
114 int put = bb.limit(); | |
115 int capacity = bb.capacity(); | |
116 dup.position(put); | |
117 dup.limit(capacity); | |
118 if( length > capacity - put ) | |
119 length = capacity - put; | |
120 dup.put(b,offset,length); | |
121 bb.limit(put+length); | |
122 return length; | |
123 } | |
124 | |
125 public int put(byte[] b) { | |
126 return put(b,0,b.length); | |
127 } | |
128 | |
129 public final int putIndex() { | |
130 return bb.limit(); | |
131 } | |
132 | |
133 public void setPutIndex(int putIndex) { | |
134 bb.limit(putIndex); | |
135 } | |
136 | |
1051
1ab2dd0a7db5
remove JBuffer.get(int)
Franklin Schmidt <fschmidt@gmail.com>
parents:
1050
diff
changeset
|
137 public void skip(int n) { |
1046 | 138 if (remaining() < n) n = remaining(); |
139 bb.position(bb.position() + n); | |
140 } | |
141 | |
1060 | 142 private final byte[] asArray() { |
1046 | 143 byte[] bytes = new byte[remaining()]; |
144 bb.duplicate().get(bytes); | |
145 return bytes; | |
146 } | |
1064
a0abb16cf6e7
remove JBuffer.readFrom()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1063
diff
changeset
|
147 /* |
1046 | 148 @Override |
1054
87275900646e
remove JBuffer.toString()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1053
diff
changeset
|
149 public String toString() { |
87275900646e
remove JBuffer.toString()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1053
diff
changeset
|
150 // return toString("ISO-8859-1"); |
87275900646e
remove JBuffer.toString()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1053
diff
changeset
|
151 // Thread.dumpStack(); |
87275900646e
remove JBuffer.toString()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1053
diff
changeset
|
152 throw new RuntimeException("toString"); |
1046 | 153 } |
1064
a0abb16cf6e7
remove JBuffer.readFrom()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1063
diff
changeset
|
154 */ |
1046 | 155 |
1052
4a2489f1d5fe
remove JBuffer.peek()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1051
diff
changeset
|
156 public byte get(int index) { |
1046 | 157 return bb.get(index); |
158 } | |
159 | |
160 } |