Java Code Examples for org.redisson.api.RBinaryStream#getInputStream()

The following examples show how to use org.redisson.api.RBinaryStream#getInputStream() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BinaryStreamExamples.java    From redisson-examples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws IOException {
    // connects to 127.0.0.1:6379 by default
    RedissonClient redisson = Redisson.create();

    RBinaryStream stream = redisson.getBinaryStream("myStream");
    
    byte[] values = new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
    stream.trySet(values);
    stream.set(values);
    
    InputStream is = stream.getInputStream();
    StringBuilder sb = new StringBuilder();
    int ch;
    while((ch = is.read()) != -1) {
        sb.append((char)ch);
    }
    String str = sb.toString();
    
    OutputStream os = stream.getOutputStream();
    for (int i = 0; i < values.length; i++) {
        byte c = values[i];
        os.write(c);
    }
    
    redisson.shutdown();
}
 
Example 2
Source File: RedissonBinaryStreamTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example 3
Source File: RedissonBinaryStreamTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testSkip() throws IOException {
    RBinaryStream t = redisson.getBinaryStream("test");
    t.set(new byte[] {1, 2, 3, 4, 5, 6});
    
    InputStream is = t.getInputStream();
    is.skip(3);
    byte[] b = new byte[6];
    is.read(b);
    assertThat(b).isEqualTo(new byte[] {4, 5, 6, 0, 0, 0});
}
 
Example 4
Source File: RedissonBinaryStreamTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testReadArray() 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[6];
    assertThat(s.read(b)).isEqualTo(6);
    assertThat(s.read(b)).isEqualTo(-1);
    
    assertThat(b).isEqualTo(value);
}
 
Example 5
Source File: RedissonBinaryStreamTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@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);
}