use of org.jgroups.util.ByteArrayDataInputStream in project JGroups by belaban.
the class MessageTest method testReadFromSkipPayload.
public void testReadFromSkipPayload() throws Exception {
Message msg = new Message(Util.createRandomAddress("A"), "bela".getBytes()).src(Util.createRandomAddress("B"));
addHeaders(msg);
byte[] buf = Util.streamableToByteBuffer(msg);
// ExposedByteArrayInputStream input=new ExposedByteArrayInputStream(buf);
// DataInput in=new DataInputStream(input);
ByteArrayDataInputStream in = new ByteArrayDataInputStream(buf);
Message msg2 = new Message(false);
int payload_position = msg2.readFromSkipPayload(in);
msg2.setBuffer(buf, payload_position, buf.length - payload_position);
assert msg2.getOffset() == payload_position;
assert msg2.getLength() == msg.getLength();
assert msg2.size() == msg.size();
Message copy = msg2.copy();
assert copy.getOffset() == payload_position;
assert copy.getLength() == msg.getLength();
assert copy.size() == msg2.size();
}
use of org.jgroups.util.ByteArrayDataInputStream in project JGroups by belaban.
the class BuffersTest method readCookieVersionAndAddress.
protected void readCookieVersionAndAddress(final Buffers bufs, final byte[] cookie, final IpAddress addr) throws Exception {
// cookie
ByteBuffer cookie_buf = bufs.get(0);
byte[] cookie2 = new byte[cookie_buf.position()];
cookie_buf.flip();
cookie_buf.get(cookie2, 0, cookie2.length);
assert Arrays.equals(cookie, cookie2);
// version
ByteBuffer version_buf = bufs.get(1);
short ver = version_buf.getShort(0);
assert Version.version == ver;
// address
ByteBuffer addr_buf = bufs.get(2);
addr_buf.flip();
ByteArrayDataInputStream in = new ByteArrayDataInputStream(addr_buf);
IpAddress address = new IpAddress();
address.readFrom(in);
assert addr.equals(address);
}
use of org.jgroups.util.ByteArrayDataInputStream in project JGroups by belaban.
the class ByteArrayDataInputOutputStreamTest method testReadBeyondLimit.
public void testReadBeyondLimit() {
byte[] buf = new byte[100];
buf[10] = 'B';
buf[11] = 'e';
buf[12] = 'l';
buf[13] = 'a';
ByteArrayDataInputStream in = new ByteArrayDataInputStream(buf, 10, 4);
assert in.position() == 10;
assert in.limit() == 14;
assert in.capacity() == buf.length;
byte[] tmp = new byte[5];
try {
in.readFully(tmp, 0, tmp.length);
assert false : " should have gotten an exception";
} catch (Exception ex) {
System.out.println("caught exception as expected: " + ex);
assert ex instanceof EOFException;
}
in.position(10);
for (int i = 0; i < 4; i++) in.read();
assert in.read() == -1;
}
use of org.jgroups.util.ByteArrayDataInputStream in project JGroups by belaban.
the class ByteArrayDataInputOutputStreamTest method testCompressedLong.
public void testCompressedLong() throws IOException {
ByteArrayDataOutputStream out = new ByteArrayDataOutputStream(1024);
long[] numbers = { Long.MIN_VALUE, -500, 0, 1, 100, Long.MAX_VALUE };
for (long i : numbers) Bits.writeLong(i, out);
ByteArrayDataInputStream in = new ByteArrayDataInputStream(out.buffer());
for (long i : numbers) {
long num = Bits.readLong(in);
assert num == i;
}
}
use of org.jgroups.util.ByteArrayDataInputStream in project JGroups by belaban.
the class ByteArrayDataInputOutputStreamTest method testUnsignedByte.
public void testUnsignedByte() throws IOException {
ByteArrayDataOutputStream out = new ByteArrayDataOutputStream(20);
byte[] bytes = { -100, -50, 0, 1, 100, 127 };
for (byte b : bytes) out.writeByte(b);
ByteArrayDataInputStream in = new ByteArrayDataInputStream(out.buffer());
for (byte b : bytes) {
int tmp = in.readUnsignedByte();
assert tmp == (b & 0xff);
}
}
Aggregations