Search in sources :

Example 1 with ByteBufferReadable

use of org.apache.hadoop.fs.ByteBufferReadable in project hadoop by apache.

the class CryptoStreamsTestBase method testCombinedOp.

@Test(timeout = 120000)
public void testCombinedOp() throws Exception {
    OutputStream out = getOutputStream(defaultBufferSize);
    writeData(out);
    final int len1 = dataLen / 8;
    final int len2 = dataLen / 10;
    InputStream in = getInputStream(defaultBufferSize);
    // Read len1 data.
    byte[] readData = new byte[len1];
    readAll(in, readData, 0, len1);
    byte[] expectedData = new byte[len1];
    System.arraycopy(data, 0, expectedData, 0, len1);
    Assert.assertArrayEquals(readData, expectedData);
    long pos = ((Seekable) in).getPos();
    Assert.assertEquals(len1, pos);
    // Seek forward len2
    ((Seekable) in).seek(pos + len2);
    // Skip forward len2
    long n = in.skip(len2);
    Assert.assertEquals(len2, n);
    // Pos: 1/4 dataLen
    positionedReadCheck(in, dataLen / 4);
    // Pos should be len1 + len2 + len2
    pos = ((Seekable) in).getPos();
    Assert.assertEquals(len1 + len2 + len2, pos);
    // Read forward len1
    ByteBuffer buf = ByteBuffer.allocate(len1);
    int nRead = ((ByteBufferReadable) in).read(buf);
    Assert.assertEquals(nRead, buf.position());
    readData = new byte[nRead];
    buf.rewind();
    buf.get(readData);
    expectedData = new byte[nRead];
    System.arraycopy(data, (int) pos, expectedData, 0, nRead);
    Assert.assertArrayEquals(readData, expectedData);
    long lastPos = pos;
    // Pos should be lastPos + nRead
    pos = ((Seekable) in).getPos();
    Assert.assertEquals(lastPos + nRead, pos);
    // Pos: 1/3 dataLen
    positionedReadCheck(in, dataLen / 3);
    // Read forward len1
    readData = new byte[len1];
    readAll(in, readData, 0, len1);
    expectedData = new byte[len1];
    System.arraycopy(data, (int) pos, expectedData, 0, len1);
    Assert.assertArrayEquals(readData, expectedData);
    lastPos = pos;
    // Pos should be lastPos + len1
    pos = ((Seekable) in).getPos();
    Assert.assertEquals(lastPos + len1, pos);
    // Read forward len1
    buf = ByteBuffer.allocate(len1);
    nRead = ((ByteBufferReadable) in).read(buf);
    Assert.assertEquals(nRead, buf.position());
    readData = new byte[nRead];
    buf.rewind();
    buf.get(readData);
    expectedData = new byte[nRead];
    System.arraycopy(data, (int) pos, expectedData, 0, nRead);
    Assert.assertArrayEquals(readData, expectedData);
    lastPos = pos;
    // Pos should be lastPos + nRead
    pos = ((Seekable) in).getPos();
    Assert.assertEquals(lastPos + nRead, pos);
    // ByteBuffer read after EOF
    ((Seekable) in).seek(dataLen);
    buf.clear();
    n = ((ByteBufferReadable) in).read(buf);
    Assert.assertEquals(n, -1);
    in.close();
}
Also used : Seekable(org.apache.hadoop.fs.Seekable) InputStream(java.io.InputStream) FSDataOutputStream(org.apache.hadoop.fs.FSDataOutputStream) OutputStream(java.io.OutputStream) ByteBufferReadable(org.apache.hadoop.fs.ByteBufferReadable) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Aggregations

InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 ByteBuffer (java.nio.ByteBuffer)1 ByteBufferReadable (org.apache.hadoop.fs.ByteBufferReadable)1 FSDataOutputStream (org.apache.hadoop.fs.FSDataOutputStream)1 Seekable (org.apache.hadoop.fs.Seekable)1 Test (org.junit.Test)1