Java Code Examples for org.apache.hadoop.fs.PositionedReadable

The following examples show how to use org.apache.hadoop.fs.PositionedReadable. These examples are extracted from open source projects. 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 Project: hadoop   Source File: CryptoInputStream.java    License: Apache License 2.0 6 votes vote down vote up
/** Positioned read. It is thread-safe */
@Override
public int read(long position, byte[] buffer, int offset, int length)
    throws IOException {
  checkStream();
  try {
    final int n = ((PositionedReadable) in).read(position, buffer, offset, 
        length);
    if (n > 0) {
      // This operation does not change the current offset of the file
      decrypt(position, buffer, offset, n);
    }
    
    return n;
  } catch (ClassCastException e) {
    throw new UnsupportedOperationException("This stream does not support " +
        "positioned read.");
  }
}
 
Example 2
Source Project: hadoop   Source File: CryptoInputStream.java    License: Apache License 2.0 6 votes vote down vote up
/** Positioned read fully. It is thread-safe */
@Override
public void readFully(long position, byte[] buffer, int offset, int length)
    throws IOException {
  checkStream();
  try {
    ((PositionedReadable) in).readFully(position, buffer, offset, length);
    if (length > 0) {
      // This operation does not change the current offset of the file
      decrypt(position, buffer, offset, length);
    }
  } catch (ClassCastException e) {
    throw new UnsupportedOperationException("This stream does not support " +
        "positioned readFully.");
  }
}
 
Example 3
Source Project: dremio-oss   Source File: TestPDFSProtocol.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testOnMessageSuccessful() throws IOException {
  InputStream mis = mock(InputStream.class, withSettings().extraInterfaces(Seekable.class, PositionedReadable.class));
  doReturn(42).when(mis).read(any(byte[].class), anyInt(), anyInt());

  FSDataInputStream fdis = new FSDataInputStream(mis);
  Response response = getResponse(7L, 4096, fdis);

  InOrder inOrder = Mockito.inOrder(mis);

  inOrder.verify((Seekable) mis).seek(7);
  inOrder.verify(mis).read(any(byte[].class), anyInt(), anyInt());

  assertEquals(42, ((DFS.GetFileDataResponse) response.pBody).getRead());
  assertEquals(42, response.dBodies[0].readableBytes());
}
 
Example 4
Source Project: dremio-oss   Source File: TestPDFSProtocol.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testOnMessageEOF() throws IOException {
  InputStream mis = mock(InputStream.class, withSettings().extraInterfaces(Seekable.class, PositionedReadable.class));
  doReturn(-1).when(mis).read(any(byte[].class), anyInt(), anyInt());

  FSDataInputStream fdis = new FSDataInputStream(mis);
  Response response = getResponse(7L, 4096, fdis);

  InOrder inOrder = Mockito.inOrder(mis);

  inOrder.verify((Seekable) mis).seek(7);
  inOrder.verify(mis).read(any(byte[].class), anyInt(), anyInt());

  assertEquals(-1, ((DFS.GetFileDataResponse) response.pBody).getRead());
  assertEquals(0, response.dBodies.length);
}
 
Example 5
Source Project: big-c   Source File: CryptoInputStream.java    License: Apache License 2.0 6 votes vote down vote up
/** Positioned read. It is thread-safe */
@Override
public int read(long position, byte[] buffer, int offset, int length)
    throws IOException {
  checkStream();
  try {
    final int n = ((PositionedReadable) in).read(position, buffer, offset, 
        length);
    if (n > 0) {
      // This operation does not change the current offset of the file
      decrypt(position, buffer, offset, n);
    }
    
    return n;
  } catch (ClassCastException e) {
    throw new UnsupportedOperationException("This stream does not support " +
        "positioned read.");
  }
}
 
Example 6
Source Project: big-c   Source File: CryptoInputStream.java    License: Apache License 2.0 6 votes vote down vote up
/** Positioned read fully. It is thread-safe */
@Override
public void readFully(long position, byte[] buffer, int offset, int length)
    throws IOException {
  checkStream();
  try {
    ((PositionedReadable) in).readFully(position, buffer, offset, length);
    if (length > 0) {
      // This operation does not change the current offset of the file
      decrypt(position, buffer, offset, length);
    }
  } catch (ClassCastException e) {
    throw new UnsupportedOperationException("This stream does not support " +
        "positioned readFully.");
  }
}
 
Example 7
/** Get input stream. */
private PositionedReadable in() throws IOException {
    synchronized (mux) {
        if (opened) {
            if (err != null)
                throw err;
        }
        else {
            opened = true;

            try {
                in = fs.open(path, bufSize);

                if (in == null)
                    throw new IOException("Failed to open input stream (file system returned null): " + path);
            }
            catch (IOException e) {
                err = e;

                throw err;
            }
        }

        return in;
    }
}
 
Example 8
Source Project: circus-train   Source File: ThrottledInputStream.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Read bytes starting from the specified position. This requires rawStream is an instance of
 * {@link PositionedReadable}.
 */
public int read(long position, byte[] buffer, int offset, int length) throws IOException {
  if (!(rawStream instanceof PositionedReadable)) {
    throw new UnsupportedOperationException("positioned read is not supported by the internal stream");
  }
  throttle();
  int readLen = ((PositionedReadable) rawStream).read(position, buffer, offset, length);
  if (readLen != -1) {
    bytesRead += readLen;
  }
  return readLen;
}
 
Example 9
Source Project: hadoop   Source File: ThrottledInputStream.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Read bytes starting from the specified position. This requires rawStream is
 * an instance of {@link PositionedReadable}.
 */
public int read(long position, byte[] buffer, int offset, int length)
    throws IOException {
  if (!(rawStream instanceof PositionedReadable)) {
    throw new UnsupportedOperationException(
        "positioned read is not supported by the internal stream");
  }
  throttle();
  int readLen = ((PositionedReadable) rawStream).read(position, buffer,
      offset, length);
  if (readLen != -1) {
    bytesRead += readLen;
  }
  return readLen;
}
 
Example 10
Source Project: hadoop   Source File: CompressionInputStream.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * This method returns the current position in the stream.
 *
 * @return Current position in stream as a long
 */
@Override
public long getPos() throws IOException {
  if (!(in instanceof Seekable) || !(in instanceof PositionedReadable)){
    //This way of getting the current position will not work for file
    //size which can be fit in an int and hence can not be returned by
    //available method.
    return (this.maxAvailableData - this.in.available());
  }
  else{
    return ((Seekable)this.in).getPos();
  }

}
 
Example 11
Source Project: hadoop   Source File: CryptoStreamsTestBase.java    License: Apache License 2.0 5 votes vote down vote up
private int readAll(InputStream in, long pos, byte[] b, int off, int len) 
    throws IOException {
  int n = 0;
  int total = 0;
  while (n != -1) {
    total += n;
    if (total >= len) {
      break;
    }
    n = ((PositionedReadable) in).read(pos + total, b, off + total, 
        len - total);
  }
  
  return total;
}
 
Example 12
Source Project: hadoop   Source File: CryptoStreamsTestBase.java    License: Apache License 2.0 5 votes vote down vote up
private void readFullyCheck(InputStream in, int pos) throws Exception {
  byte[] result = new byte[dataLen - pos];
  ((PositionedReadable) in).readFully(pos, result);
  
  byte[] expectedData = new byte[dataLen - pos];
  System.arraycopy(data, pos, expectedData, 0, dataLen - pos);
  Assert.assertArrayEquals(result, expectedData);
  
  result = new byte[dataLen]; // Exceeds maximum length 
  try {
    ((PositionedReadable) in).readFully(pos, result);
    Assert.fail("Read fully exceeds maximum length should fail.");
  } catch (IOException e) {
  }
}
 
Example 13
Source Project: dremio-oss   Source File: TestFSDataInputStreamWrapper.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void test() throws Exception {
  Class<?> byteBufferPositionedReadableClass = getClass("org.apache.hadoop.fs.ByteBufferPositionedReadable");

  assumeNonMaprProfile();
  final IOException ioException = new IOException("test io exception");
  final FSError fsError = newFSError(ioException);
  FSDataInputStream fdis = new FSDataInputStream(mock(InputStream.class, withSettings().extraInterfaces(Seekable.class, byteBufferPositionedReadableClass == null ? AutoCloseable.class : byteBufferPositionedReadableClass, PositionedReadable.class, ByteBufferReadable.class).defaultAnswer(new Answer<Object>() {
    @Override
    public Object answer(InvocationOnMock invocation) throws Throwable {
      throw fsError;
    }
  })));

  FSInputStream fdisw = FSDataInputStreamWrapper.of(fdis);
  Object[] params = getDummyArguments(method);
  try {
    method.invoke(fdisw, params);
  } catch(InvocationTargetException e) {
    if (byteBufferPositionedReadableClass == null) {
      assertThat(e.getTargetException(), anyOf(is(instanceOf(IOException.class)), is(instanceOf(UnsupportedOperationException.class))));
    } else {
      assertThat(e.getTargetException(), is(instanceOf(IOException.class)));
    }
    if (e.getTargetException() instanceof IOException) {
      assertThat((IOException) e.getTargetException(), is(sameInstance(ioException)));
    }
  }
}
 
Example 14
Source Project: big-c   Source File: ThrottledInputStream.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Read bytes starting from the specified position. This requires rawStream is
 * an instance of {@link PositionedReadable}.
 */
public int read(long position, byte[] buffer, int offset, int length)
    throws IOException {
  if (!(rawStream instanceof PositionedReadable)) {
    throw new UnsupportedOperationException(
        "positioned read is not supported by the internal stream");
  }
  throttle();
  int readLen = ((PositionedReadable) rawStream).read(position, buffer,
      offset, length);
  if (readLen != -1) {
    bytesRead += readLen;
  }
  return readLen;
}
 
Example 15
Source Project: big-c   Source File: CompressionInputStream.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * This method returns the current position in the stream.
 *
 * @return Current position in stream as a long
 */
@Override
public long getPos() throws IOException {
  if (!(in instanceof Seekable) || !(in instanceof PositionedReadable)){
    //This way of getting the current position will not work for file
    //size which can be fit in an int and hence can not be returned by
    //available method.
    return (this.maxAvailableData - this.in.available());
  }
  else{
    return ((Seekable)this.in).getPos();
  }

}
 
Example 16
Source Project: big-c   Source File: CryptoStreamsTestBase.java    License: Apache License 2.0 5 votes vote down vote up
private int readAll(InputStream in, long pos, byte[] b, int off, int len) 
    throws IOException {
  int n = 0;
  int total = 0;
  while (n != -1) {
    total += n;
    if (total >= len) {
      break;
    }
    n = ((PositionedReadable) in).read(pos + total, b, off + total, 
        len - total);
  }
  
  return total;
}
 
Example 17
Source Project: big-c   Source File: CryptoStreamsTestBase.java    License: Apache License 2.0 5 votes vote down vote up
private void readFullyCheck(InputStream in, int pos) throws Exception {
  byte[] result = new byte[dataLen - pos];
  ((PositionedReadable) in).readFully(pos, result);
  
  byte[] expectedData = new byte[dataLen - pos];
  System.arraycopy(data, pos, expectedData, 0, dataLen - pos);
  Assert.assertArrayEquals(result, expectedData);
  
  result = new byte[dataLen]; // Exceeds maximum length 
  try {
    ((PositionedReadable) in).readFully(pos, result);
    Assert.fail("Read fully exceeds maximum length should fail.");
  } catch (IOException e) {
  }
}
 
Example 18
Source Project: hbase   Source File: ThrottledInputStream.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Read bytes starting from the specified position. This requires rawStream is
 * an instance of {@link PositionedReadable}.
 * @param position
 * @param buffer
 * @param offset
 * @param length
 * @return the number of bytes read
 */
public int read(long position, byte[] buffer, int offset, int length)
    throws IOException {
  if (!(rawStream instanceof PositionedReadable)) {
    throw new UnsupportedOperationException(
      "positioned read is not supported by the internal stream");
  }
  throttle();
  int readLen = ((PositionedReadable) rawStream).read(position, buffer,
    offset, length);
  if (readLen != -1) {
    bytesRead += readLen;
  }
  return readLen;
}
 
Example 19
Source Project: spork   Source File: IndexedStorage.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Create a line reader that reads from the given stream using the
 * given buffer-size.
 * @param in The input stream
 * @param bufferSize Size of the read buffer
 * @throws IOException
 */
public IndexedStorageLineReader(InputStream in, int bufferSize) {
    if( !(in instanceof Seekable) || !(in instanceof PositionedReadable) ) {
          throw new IllegalArgumentException(
              "In is not an instance of Seekable or PositionedReadable");
    }

    this.in = in;
    this.bufferSize = bufferSize;
    this.buffer = new byte[this.bufferSize];
}
 
Example 20
Source Project: hbase   Source File: TestFSErrorsExposed.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public int read(long position, byte[] buffer, int offset, int length)
  throws IOException {
  injectFault();
  return ((PositionedReadable)in).read(position, buffer, offset, length);
}
 
Example 21
Source Project: hadoop   Source File: CompressionInputStream.java    License: Apache License 2.0 3 votes vote down vote up
/**
 * Create a compression input stream that reads
 * the decompressed bytes from the given stream.
 * 
 * @param in The input stream to be compressed.
 * @throws IOException
 */
protected CompressionInputStream(InputStream in) throws IOException {
  if (!(in instanceof Seekable) || !(in instanceof PositionedReadable)) {
      this.maxAvailableData = in.available();
  }
  this.in = in;
}
 
Example 22
Source Project: big-c   Source File: CompressionInputStream.java    License: Apache License 2.0 3 votes vote down vote up
/**
 * Create a compression input stream that reads
 * the decompressed bytes from the given stream.
 * 
 * @param in The input stream to be compressed.
 * @throws IOException
 */
protected CompressionInputStream(InputStream in) throws IOException {
  if (!(in instanceof Seekable) || !(in instanceof PositionedReadable)) {
      this.maxAvailableData = in.available();
  }
  this.in = in;
}