Search in sources :

Example 16 with ByteArrayDataInputStream

use of org.jgroups.util.ByteArrayDataInputStream in project JGroups by belaban.

the class MessageTest method testReadFromSkipPayloadNullPayload.

public static void testReadFromSkipPayloadNullPayload() throws Exception {
    Message msg = new Message(Util.createRandomAddress("A")).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);
    if (payload_position >= 0)
        msg2.setBuffer(buf, payload_position, buf.length - payload_position);
    assert msg2.getOffset() == 0;
    assert msg2.getLength() == msg.getLength();
    assert msg.getRawBuffer() == null;
    assert msg2.getRawBuffer() == null;
    assert msg.getBuffer() == null;
    assert msg2.getBuffer() == null;
    assert msg2.size() == msg.size();
    Message copy = msg2.copy();
    assert copy.getOffset() == 0;
    assert copy.getLength() == msg.getLength();
    assert copy.getRawBuffer() == null;
    assert copy.getBuffer() == null;
    assert copy.size() == msg2.size();
}
Also used : Message(org.jgroups.Message) ByteArrayDataInputStream(org.jgroups.util.ByteArrayDataInputStream)

Example 17 with ByteArrayDataInputStream

use of org.jgroups.util.ByteArrayDataInputStream in project JGroups by belaban.

the class IpAddressTest method testStreamable.

public void testStreamable() throws Exception {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    DataOutputStream oos = new DataOutputStream(bos);
    byte[] buf = null;
    ByteArrayInputStream bis = null;
    DataInputStream ois;
    IpAddress a2, b2, x, x2, y, y2;
    x = createStackConformantAddress(5555);
    y = createStackConformantAddress(1111);
    a.writeTo(oos);
    b.writeTo(oos);
    x.writeTo(oos);
    y.writeTo(oos);
    buf = bos.toByteArray();
    bis = new ByteArrayInputStream(buf);
    ois = new DataInputStream(bis);
    a2 = new IpAddress();
    a2.readFrom(ois);
    b2 = new IpAddress();
    b2.readFrom(ois);
    x2 = new IpAddress();
    x2.readFrom(ois);
    y2 = new IpAddress();
    y2.readFrom(ois);
    Assert.assertEquals(a, a2);
    Assert.assertEquals(b, b2);
    assert y2.getIpAddress() != null;
    Assert.assertEquals(1111, y2.getPort());
    ByteArrayDataOutputStream out = new ByteArrayDataOutputStream();
    j.writeTo(out);
    k.writeTo(out);
    ByteArrayDataInputStream in = new ByteArrayDataInputStream(out.buffer(), 0, out.position());
    IpAddressUUID tmp = new IpAddressUUID();
    tmp.readFrom(in);
    assert tmp.equals(j);
    tmp = new IpAddressUUID();
    tmp.readFrom(in);
    assert tmp.equals(k);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) DataOutputStream(java.io.DataOutputStream) ByteArrayDataOutputStream(org.jgroups.util.ByteArrayDataOutputStream) IpAddress(org.jgroups.stack.IpAddress) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataInputStream(java.io.DataInputStream) ByteArrayDataInputStream(org.jgroups.util.ByteArrayDataInputStream) ByteArrayDataInputStream(org.jgroups.util.ByteArrayDataInputStream) ByteArrayDataOutputStream(org.jgroups.util.ByteArrayDataOutputStream) IpAddressUUID(org.jgroups.stack.IpAddressUUID)

Example 18 with ByteArrayDataInputStream

use of org.jgroups.util.ByteArrayDataInputStream in project JGroups by belaban.

the class ByteArrayDataInputOutputStreamTest method testReadFully.

public void testReadFully() {
    byte[] name = "Bela".getBytes();
    ByteArrayDataInputStream in = new ByteArrayDataInputStream(name);
    byte[] buf = new byte[10];
    int read = in.read(buf, 0, buf.length);
    assert read == 4;
    assert in.position() == 4;
    in.position(0);
    try {
        in.readFully(buf, 0, buf.length);
        assert false : "readFully() should have thrown an EOFException";
    } catch (Exception eof_ex) {
        System.out.println("got exception as expected: " + eof_ex);
        assert eof_ex instanceof EOFException;
    }
}
Also used : EOFException(java.io.EOFException) ByteArrayDataInputStream(org.jgroups.util.ByteArrayDataInputStream) IOException(java.io.IOException) EOFException(java.io.EOFException)

Example 19 with ByteArrayDataInputStream

use of org.jgroups.util.ByteArrayDataInputStream in project JGroups by belaban.

the class ByteArrayDataInputOutputStreamTest method testLimit.

public void testLimit() {
    byte[] buf = new byte[100];
    buf[0] = 'B';
    buf[1] = 'e';
    buf[2] = 'l';
    buf[3] = 'a';
    ByteArrayDataInputStream in = new ByteArrayDataInputStream(buf);
    assert in.position() == 0;
    assert in.limit() == buf.length;
    assert in.capacity() == buf.length;
    in = new ByteArrayDataInputStream(buf, 0, 4);
    assert in.position() == 0;
    assert in.limit() == 4;
    assert in.capacity() == buf.length;
    byte[] tmp = new byte[4];
    in.read(tmp, 0, tmp.length);
    assert "Bela".equals(new String(tmp));
}
Also used : ByteArrayDataInputStream(org.jgroups.util.ByteArrayDataInputStream)

Example 20 with ByteArrayDataInputStream

use of org.jgroups.util.ByteArrayDataInputStream in project JGroups by belaban.

the class ByteArrayDataInputOutputStreamTest method testChar.

public void testChar() throws IOException {
    ByteArrayDataOutputStream out = new ByteArrayDataOutputStream(1024);
    for (int i = 0; i < 500; i++) {
        int ch = 'a' + i;
        out.writeChar(ch);
    }
    ByteArrayDataInputStream in = new ByteArrayDataInputStream(out.buffer());
    for (int i = 0; i < 500; i++) {
        char ch = in.readChar();
        assert ch == 'a' + i;
    }
}
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