From 91494d9081286e3313ac813d98d4210f1f93bea8 Mon Sep 17 00:00:00 2001 From: Joel Dice Date: Fri, 28 Mar 2008 18:08:08 -0600 Subject: [PATCH] add constructor to ByteBuffer --- classpath/java/nio/ByteBuffer.java | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/classpath/java/nio/ByteBuffer.java b/classpath/java/nio/ByteBuffer.java index 858c0eb23d..d06b239735 100644 --- a/classpath/java/nio/ByteBuffer.java +++ b/classpath/java/nio/ByteBuffer.java @@ -16,18 +16,22 @@ public class ByteBuffer extends Buffer implements Comparable { private final boolean readOnly; public static ByteBuffer allocate(int capacity) { - return new ByteBuffer(new byte[capacity], false); + return new ByteBuffer(new byte[capacity], 0, capacity, false); } public static ByteBuffer wrap(byte[] array) { - return new ByteBuffer(array, false); + return wrap(array, 0, array.length); } - private ByteBuffer(byte[] array, boolean readOnly) { + public static ByteBuffer wrap(byte[] array, int offset, int length) { + return new ByteBuffer(array, offset, length, false); + } + + private ByteBuffer(byte[] array, int offset, int length, boolean readOnly) { this.array = array; this.readOnly = readOnly; - arrayOffset = 0; - capacity = array.length; + arrayOffset = offset; + capacity = length; limit = capacity; position = 0; } @@ -53,12 +57,7 @@ public class ByteBuffer extends Buffer implements Comparable { } public ByteBuffer slice() { - ByteBuffer buf = new ByteBuffer(array, true); - buf.arrayOffset = arrayOffset + position; - buf.position = 0; - buf.capacity = remaining(); - buf.limit = buf.capacity; - return buf; + return new ByteBuffer(array, arrayOffset + position, remaining(), true); } public int arrayOffset() {