Search in sources :

Example 6 with RBinaryStream

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

the class RedissonBinaryStreamTest method testReadArrayWithOffset.

@Test
public void testReadArrayWithOffset() throws IOException {
    RBinaryStream stream = redisson.getBinaryStream("test");
    byte[] value = { 1, 2, 3, 4, 5, 6 };
    stream.set(value);
    InputStream s = stream.getInputStream();
    byte[] b = new byte[4];
    assertThat(s.read(b, 1, 3)).isEqualTo(3);
    byte[] valuesRead = { 0, 1, 2, 3 };
    assertThat(b).isEqualTo(valuesRead);
}
Also used : RBinaryStream(org.redisson.api.RBinaryStream) InputStream(java.io.InputStream) Test(org.junit.jupiter.api.Test)

Example 7 with RBinaryStream

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

the class RedissonBinaryStreamTest method testSet1024.

// @Test
public void testSet1024() {
    RBinaryStream stream = redisson.getBinaryStream("test");
    byte[] bytes = new byte[1024 * 1024 * 1024];
    ThreadLocalRandom.current().nextBytes(bytes);
    stream.set(bytes);
    assertThat(stream.size()).isEqualTo(bytes.length);
    assertThat(redisson.getBucket("{test}:parts").isExists()).isTrue();
    assertThat(redisson.getBucket("test").size()).isEqualTo(512 * 1024 * 1024);
    assertThat(redisson.getBucket("test:1").size()).isEqualTo(bytes.length - 512 * 1024 * 1024);
}
Also used : RBinaryStream(org.redisson.api.RBinaryStream)

Example 8 with RBinaryStream

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

the class RedissonBinaryStreamTest method testEmptyRead.

@Test
public void testEmptyRead() throws IOException {
    RBinaryStream stream = redisson.getBinaryStream("test");
    assertThat(stream.getInputStream().read()).isEqualTo(-1);
}
Also used : RBinaryStream(org.redisson.api.RBinaryStream) Test(org.junit.jupiter.api.Test)

Example 9 with RBinaryStream

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

the class RedissonBinaryStreamTest method testLimit.

private void testLimit(int sizeInMBs, int chunkSize) throws IOException, NoSuchAlgorithmException {
    RBinaryStream stream = redisson.getBinaryStream("test");
    MessageDigest hash = MessageDigest.getInstance("SHA-1");
    hash.reset();
    for (int i = 0; i < sizeInMBs; i++) {
        byte[] bytes = new byte[chunkSize];
        ThreadLocalRandom.current().nextBytes(bytes);
        hash.update(bytes);
        stream.getOutputStream().write(bytes);
    }
    String writtenDataHash = new BigInteger(1, hash.digest()).toString(16);
    hash.reset();
    InputStream s = stream.getInputStream();
    long readBytesTotal = 0;
    while (true) {
        byte[] bytes = new byte[ThreadLocalRandom.current().nextInt(0, chunkSize)];
        int readBytes = s.read(bytes);
        if (readBytes == -1) {
            break;
        }
        if (readBytes < bytes.length) {
            bytes = Arrays.copyOf(bytes, readBytes);
        }
        hash.update(bytes);
        readBytesTotal += readBytes;
    }
    String readDataHash = new BigInteger(1, hash.digest()).toString(16);
    assertThat(writtenDataHash).isEqualTo(readDataHash);
    assertThat(readBytesTotal).isEqualTo(sizeInMBs * chunkSize);
    assertThat(stream.size()).isEqualTo(sizeInMBs * chunkSize);
    assertThat(stream.size()).isEqualTo(sizeInMBs * chunkSize);
    assertThat(redisson.getBucket("test").isExists()).isTrue();
    if (sizeInMBs * chunkSize <= 512 * 1024 * 1024) {
        assertThat(redisson.getBucket("test:parts").isExists()).isFalse();
        assertThat(redisson.getBucket("test:1").isExists()).isFalse();
    } else {
        int parts = (sizeInMBs * chunkSize) / (512 * 1024 * 1024);
        for (int i = 1; i < parts - 1; i++) {
            assertThat(redisson.getBucket("test:" + i).isExists()).isTrue();
        }
    }
}
Also used : RBinaryStream(org.redisson.api.RBinaryStream) InputStream(java.io.InputStream) BigInteger(java.math.BigInteger) MessageDigest(java.security.MessageDigest)

Example 10 with RBinaryStream

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

the class RedissonBinaryStreamTest method testChannelOverwrite.

@Test
public void testChannelOverwrite() throws IOException {
    RBinaryStream stream = redisson.getBinaryStream("test");
    SeekableByteChannel c = stream.getChannel();
    assertThat(c.write(ByteBuffer.wrap(new byte[] { 1, 2, 3, 4, 5, 6, 7 }))).isEqualTo(7);
    c.position(3);
    assertThat(c.write(ByteBuffer.wrap(new byte[] { 0, 9, 10 }))).isEqualTo(3);
    assertThat(c.position()).isEqualTo(6);
    ByteBuffer b = ByteBuffer.allocate(3);
    int r = c.read(b);
    assertThat(c.position()).isEqualTo(7);
    assertThat(r).isEqualTo(1);
    b.flip();
    byte[] bb = new byte[b.remaining()];
    b.get(bb);
    assertThat(bb).isEqualTo(new byte[] { 7 });
    c.position(0);
    ByteBuffer state = ByteBuffer.allocate(7);
    c.read(state);
    byte[] bb1 = new byte[7];
    state.flip();
    state.get(bb1);
    assertThat(bb1).isEqualTo(new byte[] { 1, 2, 3, 0, 9, 10, 7 });
}
Also used : SeekableByteChannel(java.nio.channels.SeekableByteChannel) RBinaryStream(org.redisson.api.RBinaryStream) ByteBuffer(java.nio.ByteBuffer) 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