@@ -56,8 +56,13 @@ public ByteBuffer() {
56
56
* of given size.
57
57
*/
58
58
public ByteBuffer (int initialSize ) {
59
- elems = new byte [initialSize ];
60
- length = 0 ;
59
+ this (new byte [initialSize ]);
60
+ }
61
+
62
+ /** Create a new byte buffer using the given array for storage.
63
+ */
64
+ public ByteBuffer (byte [] elems ) {
65
+ this .elems = elems ;
61
66
}
62
67
63
68
/** Append byte to this buffer.
@@ -147,30 +152,28 @@ public void appendName(Name name) {
147
152
appendBytes (name .getByteArray (), name .getByteOffset (), name .getByteLength ());
148
153
}
149
154
150
- /** Append the content of a given input stream, and then close the stream.
155
+ /** Append the content of the given input stream.
151
156
*/
152
- public void appendStream (InputStream is ) throws IOException {
153
- try (InputStream input = is ) {
154
- while (true ) {
155
-
156
- // Read another chunk of data, using size hint from available().
157
- // If available() is accurate, the array size should be just right.
158
- int amountToRead = Math .max (input .available (), 64 );
159
- elems = ArrayUtils .ensureCapacity (elems , length + amountToRead );
160
- int amountRead = input .read (elems , length , amountToRead );
161
- if (amountRead == -1 )
157
+ public void appendStream (InputStream input ) throws IOException {
158
+ while (true ) {
159
+
160
+ // Read another chunk of data, using size hint from available().
161
+ // If available() is accurate, the array size should be just right.
162
+ int amountToRead = Math .max (input .available (), 64 );
163
+ elems = ArrayUtils .ensureCapacity (elems , length + amountToRead );
164
+ int amountRead = input .read (elems , length , amountToRead );
165
+ if (amountRead == -1 )
166
+ break ;
167
+ length += amountRead ;
168
+
169
+ // Check for the common case where input.available() returned the
170
+ // entire remaining input; in that case, avoid an extra array extension.
171
+ // Note we are guaranteed that elems.length >= length + 1 at this point.
172
+ if (amountRead == amountToRead ) {
173
+ int byt = input .read ();
174
+ if (byt == -1 )
162
175
break ;
163
- length += amountRead ;
164
-
165
- // Check for the common case where input.available() returned the
166
- // entire remaining input; in that case, avoid an extra array extension.
167
- // Note we are guaranteed that elems.length >= length + 1 at this point.
168
- if (amountRead == amountToRead ) {
169
- int byt = input .read ();
170
- if (byt == -1 )
171
- break ;
172
- elems [length ++] = (byte )byt ;
173
- }
176
+ elems [length ++] = (byte )byt ;
174
177
}
175
178
}
176
179
}
@@ -292,6 +295,15 @@ public void verifyRange(int off, int len) throws UnderflowException {
292
295
throw new UnderflowException (length );
293
296
}
294
297
298
+ /** Create a {@link java.nio.ByteBuffer} view of this instance.
299
+ *
300
+ * <p>
301
+ * If this instance is modified, the returned buffer may no longer reflect it.
302
+ */
303
+ public java .nio .ByteBuffer asByteBuffer () {
304
+ return java .nio .ByteBuffer .wrap (elems , 0 , length );
305
+ }
306
+
295
307
// UnderflowException
296
308
297
309
/** Thrown when trying to read past the end of the buffer.
0 commit comments