Skip to content

Commit

Permalink
Merge pull request #684 from fredoboulo/fix/682
Browse files Browse the repository at this point in the history
Problem: Msg.getBytes does not return the bytes of the message.
  • Loading branch information
c-rack committed Mar 8, 2019
2 parents 1cfbadb + 96ae542 commit 184b6b7
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/main/java/zmq/Msg.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ enum Type
// the file descriptor where this message originated, needs to be 64bit due to alignment
private SocketChannel fileDesc;

private int size;
private final int size;
private byte[] data;
private final ByteBuffer buf;
// keep track of relative write position
Expand Down Expand Up @@ -330,7 +330,7 @@ public int getBytes(int index, byte[] dst, int off, int len)
if (data == null) {
ByteBuffer dup = buf.duplicate();
dup.position(index);
dup.put(dst, off, count);
dup.get(dst, off, count);
}
else {
System.arraycopy(data, index, dst, off, count);
Expand Down
73 changes: 73 additions & 0 deletions src/test/java/zmq/TestMsg.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
package zmq;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

import java.nio.ByteBuffer;
import java.util.function.Function;

import org.junit.Test;

public class TestMsg
{
private final Function<Integer, ByteBuffer> allocator;

public TestMsg()
{
this(ByteBuffer::allocateDirect);
}

protected TestMsg(Function<Integer, ByteBuffer> allocator)
{
this.allocator = allocator;
}

@Test(expected = IllegalArgumentException.class)
public void shouldThrowForNullByteBuffer()
{
Expand All @@ -22,4 +38,61 @@ public void shouldWorkForFlippedBuffers()
buffer.flip();
new Msg(buffer);
}

@Test
public void testGetBytes()
{
final Msg msg = initMsg();

final byte[] dst = new byte[3];
msg.getBytes(0, dst, 0, 3);
assertThat(dst, is(new byte[] { 0, 1, 2 }));
}

@Test
public void testGetBytesIndex()
{
final Msg msg = initMsg();

final byte[] dst = new byte[4];
msg.getBytes(1, dst, 0, 4);
assertThat(dst, is(new byte[] { 1, 2, 3, 4 }));
}

@Test
public void testGetBytesLength()
{
final Msg msg = initMsg();

final byte[] dst = new byte[5];
msg.getBytes(2, dst, 0, 2);
assertThat(dst, is(new byte[] { 2, 3, 0, 0, 0 }));
}

@Test
public void testGetBytesOffset()
{
final Msg msg = initMsg();

final byte[] dst = new byte[6];
msg.getBytes(3, dst, 1, 2);
assertThat(dst, is(new byte[] { 0, 3, 4, 0, 0, 0 }));
}

protected Msg initMsg()
{
return initDirectMsg(allocator);
}

Msg initDirectMsg(Function<Integer, ByteBuffer> allocator)
{
int size = 30;
final ByteBuffer buffer = allocator.apply(size);
for (int idx = 0; idx < size; ++idx) {
buffer.put((byte) idx);
}
buffer.position(0);
final Msg msg = new Msg(buffer);
return msg;
}
}
11 changes: 11 additions & 0 deletions src/test/java/zmq/TestMsgDirect.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package zmq;

import java.nio.ByteBuffer;

public class TestMsgDirect extends TestMsg
{
public TestMsgDirect()
{
super(ByteBuffer::allocate);
}
}

0 comments on commit 184b6b7

Please sign in to comment.