diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/BufferUtil.java b/jetty-util/src/main/java/org/eclipse/jetty/util/BufferUtil.java index d319b2bc04f..8ad221812ed 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/BufferUtil.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/BufferUtil.java @@ -649,6 +649,43 @@ public class BufferUtil { return ByteBuffer.wrap(s.getBytes(charset)); } + + /** + * Create a new ByteBuffer using a copy of the provided byte array. + * + * @param array + * the byte array to copy. (not using as-is in underlying implementation) + * @return ByteBuffer with provided byte array, in flush mode + */ + public static ByteBuffer toBuffer(byte array[]) + { + int len = array.length; + ByteBuffer buf = ByteBuffer.allocate(len); + BufferUtil.clearToFill(buf); + buf.put(array,0,len); + BufferUtil.flipToFlush(buf,0); + return buf; + } + + /** + * Create a new ByteBuffer using a copy of the provided byte array. + * + * @param array + * the byte array to copy. (not using as-is in underlying implementation) + * @param offset + * the offset within the byte array to copy from + * @param length + * the length in bytes of the array to copy + * @return ByteBuffer with provided byte array, in flush mode + */ + public static ByteBuffer toBuffer(byte array[], int offset, int length) + { + ByteBuffer buf = ByteBuffer.allocate(length); + BufferUtil.clearToFill(buf); + buf.put(array,offset,length); + BufferUtil.flipToFlush(buf,0); + return buf; + } public static ByteBuffer toBuffer(File file) throws IOException { diff --git a/jetty-util/src/test/java/org/eclipse/jetty/util/BufferUtilTest.java b/jetty-util/src/test/java/org/eclipse/jetty/util/BufferUtilTest.java index ade231f96cf..2d70a698d06 100644 --- a/jetty-util/src/test/java/org/eclipse/jetty/util/BufferUtilTest.java +++ b/jetty-util/src/test/java/org/eclipse/jetty/util/BufferUtilTest.java @@ -17,7 +17,9 @@ package org.eclipse.jetty.util; import static org.junit.Assert.*; import java.nio.ByteBuffer; +import java.util.Arrays; +import org.junit.Assert; import org.junit.Test; public class BufferUtilTest @@ -155,4 +157,42 @@ public class BufferUtilTest assertEquals("1234567890",BufferUtil.toString(to)); } + @Test + public void testToBuffer_Array() + { + byte arr[] = new byte[128]; + Arrays.fill(arr,(byte)0x44); + ByteBuffer buf = BufferUtil.toBuffer(arr); + + int count = 0; + while (buf.remaining() > 0) + { + byte b = buf.get(); + Assert.assertEquals(b,0x44); + count++; + } + + Assert.assertEquals("Count of bytes",arr.length,count); + } + + @Test + public void testToBuffer_ArrayOffsetLength() + { + byte arr[] = new byte[128]; + Arrays.fill(arr,(byte)0xFF); // fill whole thing with FF + int offset = 10; + int length = 100; + Arrays.fill(arr,offset,offset + length,(byte)0x77); // fill partial with 0x77 + ByteBuffer buf = BufferUtil.toBuffer(arr,offset,length); + + int count = 0; + while (buf.remaining() > 0) + { + byte b = buf.get(); + Assert.assertEquals(b,0x77); + count++; + } + + Assert.assertEquals("Count of bytes",length,count); + } }