Search in sources :

Example 1 with Seekable

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

the class CryptoStreamsTestBase method testSeek.

/** Test seek to different position. */
@Test(timeout = 120000)
public void testSeek() throws Exception {
    OutputStream out = getOutputStream(defaultBufferSize);
    writeData(out);
    InputStream in = getInputStream(defaultBufferSize);
    // Pos: 1/3 dataLen
    seekCheck(in, dataLen / 3);
    // Pos: 0
    seekCheck(in, 0);
    // Pos: 1/2 dataLen
    seekCheck(in, dataLen / 2);
    final long pos = ((Seekable) in).getPos();
    // Pos: -3
    try {
        seekCheck(in, -3);
        Assert.fail("Seek to negative offset should fail.");
    } catch (EOFException e) {
        GenericTestUtils.assertExceptionContains(FSExceptionMessages.NEGATIVE_SEEK, e);
    }
    Assert.assertEquals(pos, ((Seekable) in).getPos());
    // Pos: dataLen + 3
    try {
        seekCheck(in, dataLen + 3);
        Assert.fail("Seek after EOF should fail.");
    } catch (IOException e) {
        GenericTestUtils.assertExceptionContains("Cannot seek after EOF", e);
    }
    Assert.assertEquals(pos, ((Seekable) in).getPos());
    in.close();
}
Also used : Seekable(org.apache.hadoop.fs.Seekable) InputStream(java.io.InputStream) FSDataOutputStream(org.apache.hadoop.fs.FSDataOutputStream) OutputStream(java.io.OutputStream) EOFException(java.io.EOFException) IOException(java.io.IOException) Test(org.junit.Test)

Example 2 with Seekable

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

the class CryptoInputStream method read.

@Override
public ByteBuffer read(ByteBufferPool bufferPool, int maxLength, EnumSet<ReadOption> opts) throws IOException, UnsupportedOperationException {
    checkStream();
    try {
        if (outBuffer.remaining() > 0) {
            // Have some decrypted data unread, need to reset.
            ((Seekable) in).seek(getPos());
            resetStreamOffset(getPos());
        }
        final ByteBuffer buffer = ((HasEnhancedByteBufferAccess) in).read(bufferPool, maxLength, opts);
        if (buffer != null) {
            final int n = buffer.remaining();
            if (n > 0) {
                // Read n bytes
                streamOffset += buffer.remaining();
                final int pos = buffer.position();
                decrypt(buffer, n, pos);
            }
        }
        return buffer;
    } catch (ClassCastException e) {
        throw new UnsupportedOperationException("This stream does not support " + "enhanced byte buffer access.");
    }
}
Also used : Seekable(org.apache.hadoop.fs.Seekable) HasEnhancedByteBufferAccess(org.apache.hadoop.fs.HasEnhancedByteBufferAccess) ByteBuffer(java.nio.ByteBuffer)

Example 3 with Seekable

use of org.apache.hadoop.fs.Seekable 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

Seekable (org.apache.hadoop.fs.Seekable)3 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 ByteBuffer (java.nio.ByteBuffer)2 FSDataOutputStream (org.apache.hadoop.fs.FSDataOutputStream)2 Test (org.junit.Test)2 EOFException (java.io.EOFException)1 IOException (java.io.IOException)1 ByteBufferReadable (org.apache.hadoop.fs.ByteBufferReadable)1 HasEnhancedByteBufferAccess (org.apache.hadoop.fs.HasEnhancedByteBufferAccess)1