Search in sources :

Example 1 with ByteArrayDataInputStream

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();
}
Also used : Message(org.jgroups.Message) ByteArrayDataInputStream(org.jgroups.util.ByteArrayDataInputStream)

Example 2 with ByteArrayDataInputStream

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);
}
Also used : IpAddress(org.jgroups.stack.IpAddress) ByteArrayDataInputStream(org.jgroups.util.ByteArrayDataInputStream) ByteBuffer(java.nio.ByteBuffer)

Example 3 with ByteArrayDataInputStream

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;
}
Also used : EOFException(java.io.EOFException) ByteArrayDataInputStream(org.jgroups.util.ByteArrayDataInputStream) IOException(java.io.IOException) EOFException(java.io.EOFException)

Example 4 with ByteArrayDataInputStream

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;
    }
}
Also used : ByteArrayDataInputStream(org.jgroups.util.ByteArrayDataInputStream) ByteArrayDataOutputStream(org.jgroups.util.ByteArrayDataOutputStream)

Example 5 with ByteArrayDataInputStream

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);
    }
}
Also used : ByteArrayDataInputStream(org.jgroups.util.ByteArrayDataInputStream) ByteArrayDataOutputStream(org.jgroups.util.ByteArrayDataOutputStream)

Aggregations

ByteArrayDataInputStream (org.jgroups.util.ByteArrayDataInputStream)32 ByteArrayDataOutputStream (org.jgroups.util.ByteArrayDataOutputStream)19 EOFException (java.io.EOFException)3 IOException (java.io.IOException)3 ByteBuffer (java.nio.ByteBuffer)3 Message (org.jgroups.Message)3 IpAddress (org.jgroups.stack.IpAddress)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 DataInput (java.io.DataInput)1 DataInputStream (java.io.DataInputStream)1 DataOutputStream (java.io.DataOutputStream)1 DatagramPacket (java.net.DatagramPacket)1 SocketException (java.net.SocketException)1 ArrayList (java.util.ArrayList)1 IpAddressUUID (org.jgroups.stack.IpAddressUUID)1