Java Code Examples for org.apache.commons.compress.utils.IOUtils#readFully()

The following examples show how to use org.apache.commons.compress.utils.IOUtils#readFully() . 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: ArArchiveInputStream.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Reads the real name from the current stream assuming the very
 * first bytes to be read are the real file name.
 *
 * @see #isBSDLongName
 *
 * @since 1.3
 */
private String getBSDLongName(final String bsdLongName) throws IOException {
  final int nameLen =
      Integer.parseInt(bsdLongName.substring(BSD_LONGNAME_PREFIX_LEN));
  final byte[] name = new byte[nameLen];
  final int read = IOUtils.readFully(input, name);
  trackReadBytes(read);
  if (read != nameLen) {
    throw new EOFException();
  }
  return ArchiveUtils.toAsciiString(name);
}
 
Example 2
Source File: ArArchiveInputStream.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Reads the GNU archive String Table.
 *
 * @see #isGNUStringTable
 */
private ArArchiveEntry readGNUStringTable(final byte[] length, final int offset, final int len) throws IOException {
  final int bufflen = asInt(length, offset, len); // Assume length will fit in an int
  namebuffer = new byte[bufflen];
  final int read = IOUtils.readFully(input, namebuffer, 0, bufflen);
  trackReadBytes(read);
  if (read != bufflen){
    throw new IOException("Failed to read complete // record: expected="
        + bufflen + " read=" + read);
  }
  return new ArArchiveEntry(GNU_STRING_TABLE_NAME, bufflen);
}
 
Example 3
Source File: AvroSource.java    From beam with Apache License 2.0 4 votes vote down vote up
@Override
public boolean readNextBlock() throws IOException {
  long startOfNextBlock;
  synchronized (progressLock) {
    startOfNextBlock = currentBlockOffset + currentBlockSizeBytes;
  }

  // Before reading the variable-sized block header, record the current number of bytes read.
  long preHeaderCount = countStream.getBytesRead();
  decoder = DecoderFactory.get().directBinaryDecoder(countStream, decoder);
  long numRecords;
  try {
    numRecords = decoder.readLong();
  } catch (EOFException e) {
    // Expected for the last block, at which the start position is the EOF. The way to detect
    // stream ending is to try reading from it.
    return false;
  }
  long blockSize = decoder.readLong();

  // Mark header size as the change in the number of bytes read.
  long headerSize = countStream.getBytesRead() - preHeaderCount;

  // Create the current block by reading blockSize bytes. Block sizes permitted by the Avro
  // specification are [32, 2^30], so the cast is safe.
  byte[] data = new byte[(int) blockSize];
  int bytesRead = IOUtils.readFully(stream, data);
  checkState(
      blockSize == bytesRead,
      "Only able to read %s/%s bytes in the block before EOF reached.",
      bytesRead,
      blockSize);
  currentBlock =
      new AvroBlock<>(
          data,
          numRecords,
          getCurrentSource().mode,
          metadata.getSchemaString(),
          metadata.getCodec());

  // Read the end of this block, which MUST be a sync marker for correctness.
  byte[] syncMarker = metadata.getSyncMarker();
  byte[] readSyncMarker = new byte[syncMarker.length];
  long syncMarkerOffset = startOfNextBlock + headerSize + blockSize;
  bytesRead = IOUtils.readFully(stream, readSyncMarker);
  checkState(
      bytesRead == syncMarker.length,
      "Only able to read %s/%s bytes of Avro sync marker at position %s before EOF reached.",
      bytesRead,
      syncMarker.length,
      syncMarkerOffset);
  if (!Arrays.equals(syncMarker, readSyncMarker)) {
    throw new IllegalStateException(
        String.format(
            "Expected the bytes [%d,%d) in file %s to be a sync marker, but found %s",
            syncMarkerOffset,
            syncMarkerOffset + syncMarker.length,
            getCurrentSource().getFileOrPatternSpec(),
            Arrays.toString(readSyncMarker)));
  }

  // Atomically update both the position and offset of the new block.
  synchronized (progressLock) {
    currentBlockOffset = startOfNextBlock;
    // Total block size includes the header, block content, and trailing sync marker.
    currentBlockSizeBytes = headerSize + blockSize + syncMarker.length;
  }

  return true;
}