Search in sources :

Example 1 with RBinaryStream

use of org.redisson.api.RBinaryStream in project redisson by redisson.

the class RedissonBinaryStreamTest method testRead.

@Test
public void testRead() throws IOException {
    RBinaryStream stream = redisson.getBinaryStream("test");
    byte[] value = { 1, 2, 3, 4, 5, (byte) 0xFF };
    stream.set(value);
    InputStream s = stream.getInputStream();
    int b = 0;
    byte[] readValue = new byte[6];
    int i = 0;
    while (true) {
        b = s.read();
        if (b == -1) {
            break;
        }
        readValue[i] = (byte) b;
        i++;
    }
    assertThat(readValue).isEqualTo(value);
}
Also used : RBinaryStream(org.redisson.api.RBinaryStream) InputStream(java.io.InputStream) Test(org.junit.jupiter.api.Test)

Example 2 with RBinaryStream

use of org.redisson.api.RBinaryStream in project redisson by redisson.

the class RedissonBinaryStreamTest method testChannelPosition.

@Test
public void testChannelPosition() throws IOException {
    RBinaryStream stream = redisson.getBinaryStream("test");
    SeekableByteChannel c = stream.getChannel();
    c.write(ByteBuffer.wrap(new byte[] { 1, 2, 3, 4, 5, 6, 7 }));
    c.position(3);
    ByteBuffer b = ByteBuffer.allocate(3);
    c.read(b);
    assertThat(c.position()).isEqualTo(6);
    byte[] bb = new byte[3];
    b.flip();
    b.get(bb);
    assertThat(bb).isEqualTo(new byte[] { 4, 5, 6 });
}
Also used : SeekableByteChannel(java.nio.channels.SeekableByteChannel) RBinaryStream(org.redisson.api.RBinaryStream) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test)

Example 3 with RBinaryStream

use of org.redisson.api.RBinaryStream in project redisson by redisson.

the class RedissonBinaryStreamTest method testWriteArrayWithOffset.

@Test
public void testWriteArrayWithOffset() throws IOException {
    RBinaryStream stream = redisson.getBinaryStream("test");
    OutputStream os = stream.getOutputStream();
    byte[] value = { 1, 2, 3, 4, 5, 6 };
    os.write(value, 0, 3);
    byte[] s = stream.get();
    assertThat(s).isEqualTo(new byte[] { 1, 2, 3 });
    os.write(value, 3, 3);
    s = stream.get();
    assertThat(s).isEqualTo(value);
}
Also used : RBinaryStream(org.redisson.api.RBinaryStream) OutputStream(java.io.OutputStream) Test(org.junit.jupiter.api.Test)

Example 4 with RBinaryStream

use of org.redisson.api.RBinaryStream in project redisson by redisson.

the class RedissonBinaryStreamTest method testChannelTruncate.

@Test
public void testChannelTruncate() throws IOException {
    RBinaryStream stream = redisson.getBinaryStream("test");
    SeekableByteChannel c = stream.getChannel();
    c.write(ByteBuffer.wrap(new byte[] { 1, 2, 3, 4, 5, 6, 7 }));
    assertThat(c.size()).isEqualTo(7);
    c.truncate(3);
    c.position(0);
    c.truncate(10);
    ByteBuffer b = ByteBuffer.allocate(3);
    c.read(b);
    byte[] bb = new byte[3];
    b.flip();
    b.get(bb);
    assertThat(c.size()).isEqualTo(3);
    assertThat(bb).isEqualTo(new byte[] { 1, 2, 3 });
}
Also used : SeekableByteChannel(java.nio.channels.SeekableByteChannel) RBinaryStream(org.redisson.api.RBinaryStream) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test)

Example 5 with RBinaryStream

use of org.redisson.api.RBinaryStream in project redisson by redisson.

the class RedissonBinaryStreamTest method testWriteArray.

@Test
public void testWriteArray() throws IOException {
    RBinaryStream stream = redisson.getBinaryStream("test");
    OutputStream os = stream.getOutputStream();
    byte[] value = { 1, 2, 3, 4, 5, 6 };
    os.write(value);
    byte[] s = stream.get();
    assertThat(s).isEqualTo(value);
}
Also used : RBinaryStream(org.redisson.api.RBinaryStream) OutputStream(java.io.OutputStream) Test(org.junit.jupiter.api.Test)

Aggregations

RBinaryStream (org.redisson.api.RBinaryStream)14 Test (org.junit.jupiter.api.Test)12 InputStream (java.io.InputStream)5 ByteBuffer (java.nio.ByteBuffer)4 SeekableByteChannel (java.nio.channels.SeekableByteChannel)3 OutputStream (java.io.OutputStream)2 BigInteger (java.math.BigInteger)1 AsynchronousByteChannel (java.nio.channels.AsynchronousByteChannel)1 MessageDigest (java.security.MessageDigest)1