Search in sources :

Example 1 with HasEnhancedByteBufferAccess

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

the class CryptoStreamsTestBase method testHasEnhancedByteBufferAccess.

@Test(timeout = 120000)
public void testHasEnhancedByteBufferAccess() throws Exception {
    OutputStream out = getOutputStream(defaultBufferSize);
    writeData(out);
    InputStream in = getInputStream(defaultBufferSize);
    final int len1 = dataLen / 8;
    // ByteBuffer size is len1
    ByteBuffer buffer = ((HasEnhancedByteBufferAccess) in).read(getBufferPool(), len1, EnumSet.of(ReadOption.SKIP_CHECKSUMS));
    int n1 = buffer.remaining();
    byte[] readData = new byte[n1];
    buffer.get(readData);
    byte[] expectedData = new byte[n1];
    System.arraycopy(data, 0, expectedData, 0, n1);
    Assert.assertArrayEquals(readData, expectedData);
    ((HasEnhancedByteBufferAccess) in).releaseBuffer(buffer);
    // Read len1 bytes
    readData = new byte[len1];
    readAll(in, readData, 0, len1);
    expectedData = new byte[len1];
    System.arraycopy(data, n1, expectedData, 0, len1);
    Assert.assertArrayEquals(readData, expectedData);
    // ByteBuffer size is len1
    buffer = ((HasEnhancedByteBufferAccess) in).read(getBufferPool(), len1, EnumSet.of(ReadOption.SKIP_CHECKSUMS));
    int n2 = buffer.remaining();
    readData = new byte[n2];
    buffer.get(readData);
    expectedData = new byte[n2];
    System.arraycopy(data, n1 + len1, expectedData, 0, n2);
    Assert.assertArrayEquals(readData, expectedData);
    ((HasEnhancedByteBufferAccess) in).releaseBuffer(buffer);
    in.close();
}
Also used : InputStream(java.io.InputStream) FSDataOutputStream(org.apache.hadoop.fs.FSDataOutputStream) OutputStream(java.io.OutputStream) HasEnhancedByteBufferAccess(org.apache.hadoop.fs.HasEnhancedByteBufferAccess) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 2 with HasEnhancedByteBufferAccess

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

Aggregations

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