Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private NIOConverter() {
@Converter(order = 1)
public static byte[] toByteArray(ByteBuffer buffer) {
byte[] bArray = new byte[buffer.limit()];
buffer.get(bArray);
buffer.get(0, bArray);
return bArray;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@
import org.apache.camel.Exchange;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

public class NIOConverterTest extends ContextTestSupport {
class NIOConverterTest extends ContextTestSupport {
private static final String TEST_FILE_NAME = "hello" + UUID.randomUUID() + ".txt";

@Test
Expand All @@ -53,6 +54,79 @@ public void testToByteArrayBigBuffer() {
assertEquals(5, out.length);
}

@Test
void testToByteArrayFullyConsumedBuffer() {
ByteBuffer bb = ByteBuffer.wrap("Hello".getBytes());
while (bb.hasRemaining()) {
bb.get();
}
assertThat(bb.position()).isEqualTo(bb.limit());

byte[] out = NIOConverter.toByteArray(bb);

assertThat(out).containsExactly("Hello".getBytes());
assertThat(bb.position()).isEqualTo(bb.limit());
}

@Test
void testToByteArrayPartiallyConsumedBuffer() {
ByteBuffer bb = ByteBuffer.allocate(100);
bb.put("Hello".getBytes());
bb.flip();
bb.get();
assertThat(bb.position()).isEqualTo(1);

byte[] out = NIOConverter.toByteArray(bb);

assertThat(out).containsExactly("Hello".getBytes());
assertThat(bb.position()).isEqualTo(1);
}

@Test
void testToByteArrayEmptyBuffer() {
ByteBuffer bb = ByteBuffer.allocate(0);

byte[] out = NIOConverter.toByteArray(bb);

assertThat(out).isEmpty();
}

@Test
void testToByteArrayReadOnlyFullyConsumedBuffer() {
ByteBuffer bb = ByteBuffer.wrap("Hello".getBytes()).asReadOnlyBuffer();
while (bb.hasRemaining()) {
bb.get();
}

byte[] out = NIOConverter.toByteArray(bb);

assertThat(out).containsExactly("Hello".getBytes());
}

@Test
void testToStringFullyConsumedBuffer() throws Exception {
ByteBuffer bb = ByteBuffer.wrap("Hello".getBytes());
while (bb.hasRemaining()) {
bb.get();
}

String out = NIOConverter.toString(bb, null);

assertThat(out).isEqualTo("Hello");
}

@Test
void testToInputStreamFullyConsumedBuffer() throws Exception {
ByteBuffer bb = ByteBuffer.wrap("Hello".getBytes());
while (bb.hasRemaining()) {
bb.get();
}

InputStream is = NIOConverter.toInputStream(bb);

assertThat(IOConverter.toString(is, null)).isEqualTo("Hello");
}

@Test
public void testToString() throws Exception {
ByteBuffer bb = ByteBuffer.wrap("Hello".getBytes());
Expand Down