Search in sources :

Example 21 with ByteArrayDataOutputStream

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

the class EncryptTest method marshal.

protected static Buffer marshal(final View view) throws Exception {
    ByteArrayDataOutputStream out = new ByteArrayDataOutputStream(Util.size(view));
    out.writeShort(1);
    if (view != null)
        view.writeTo(out);
    return out.getBuffer();
}
Also used : ByteArrayDataOutputStream(org.jgroups.util.ByteArrayDataOutputStream)

Example 22 with ByteArrayDataOutputStream

use of org.jgroups.util.ByteArrayDataOutputStream 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 23 with ByteArrayDataOutputStream

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

the class BuffersTest method testRead3.

public void testRead3() throws Exception {
    byte[] cookie = { 'b', 'e', 'l', 'a' };
    IpAddress addr = new IpAddress(7500);
    ByteArrayDataOutputStream out = new ByteArrayDataOutputStream();
    addr.writeTo(out);
    MockSocketChannel ch = new MockSocketChannel().bytesToRead((ByteBuffer) ByteBuffer.allocate(cookie.length + Global.SHORT_SIZE + out.position()).put(cookie).putShort(Version.version).put(out.buffer(), 0, out.position()).flip());
    int remaining = ch.bytesToRead().remaining();
    ch.bytesToRead().limit(remaining - 10);
    Buffers bufs = new Buffers(ByteBuffer.allocate(cookie.length), ByteBuffer.allocate(Global.SHORT_SIZE), ByteBuffer.allocate(out.position()));
    boolean rc = bufs.read(ch);
    assert !rc;
    ch.bytesToRead().limit(remaining - 2);
    rc = bufs.read(ch);
    assert !rc;
    ch.bytesToRead().limit(remaining);
    rc = bufs.read(ch);
    assert rc;
    readCookieVersionAndAddress(bufs, cookie, addr);
}
Also used : MockSocketChannel(org.jgroups.nio.MockSocketChannel) IpAddress(org.jgroups.stack.IpAddress) Buffers(org.jgroups.nio.Buffers) ByteArrayDataOutputStream(org.jgroups.util.ByteArrayDataOutputStream)

Example 24 with ByteArrayDataOutputStream

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

the class ByteArrayDataInputOutputStreamTest method testExpanding2.

public void testExpanding2() {
    ByteArrayDataOutputStream out = new ByteArrayDataOutputStream(1);
    out.write(new byte[] { 'b', 'e', 'l', 'a' });
    out = new ByteArrayDataOutputStream(1);
    out.writeBoolean(true);
    out.writeBoolean(false);
    assert out.position() == 2;
    out = new ByteArrayDataOutputStream(1);
    out.writeShort(22);
    out.writeShort(23);
    assert out.position() == 4;
    out = new ByteArrayDataOutputStream(1);
    out.writeInt(23);
    out.writeInt(24);
    assert out.position() == 8;
}
Also used : ByteArrayDataOutputStream(org.jgroups.util.ByteArrayDataOutputStream)

Example 25 with ByteArrayDataOutputStream

use of org.jgroups.util.ByteArrayDataOutputStream 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

ByteArrayDataOutputStream (org.jgroups.util.ByteArrayDataOutputStream)33 ByteArrayDataInputStream (org.jgroups.util.ByteArrayDataInputStream)19 IpAddress (org.jgroups.stack.IpAddress)3 Buffers (org.jgroups.nio.Buffers)2 MockSocketChannel (org.jgroups.nio.MockSocketChannel)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 DataInputStream (java.io.DataInputStream)1 DataOutputStream (java.io.DataOutputStream)1 DatagramPacket (java.net.DatagramPacket)1 SocketException (java.net.SocketException)1 UnknownHostException (java.net.UnknownHostException)1 ByteBuffer (java.nio.ByteBuffer)1 Address (org.jgroups.Address)1 IpAddressUUID (org.jgroups.stack.IpAddressUUID)1