Adding BufferUtil.toBuffer(byte[]) and .toBuffer(byte[], int, int) with tests
This commit is contained in:
parent
f1d4d4699f
commit
21ef1570c7
|
@ -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
|
||||
{
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue