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);
}
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);
}
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);
}
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();
}
}
}
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 });
}
Aggregations