private void readBlock() throws IOException { if (mFileBuffer == null) { mFileBuffer = new byte[BlockCompressedStreamConstants.MAX_COMPRESSED_BLOCK_SIZE]; } int count = readBytes(mFileBuffer, 0, BlockCompressedStreamConstants.BLOCK_HEADER_LENGTH); if (count == 0) { // Handle case where there is no empty gzip block at end. mCurrentOffset = 0; mBlockAddress += mLastBlockLength; mCurrentBlock = new byte[0]; return; } if (count != BlockCompressedStreamConstants.BLOCK_HEADER_LENGTH) { throw new IOException("Premature end of file"); } final int blockLength = unpackInt16(mFileBuffer, BlockCompressedStreamConstants.BLOCK_LENGTH_OFFSET) + 1; if (blockLength < BlockCompressedStreamConstants.BLOCK_HEADER_LENGTH || blockLength > mFileBuffer.length) { throw new IOException("Unexpected compressed block length: " + blockLength); } final int remaining = blockLength - BlockCompressedStreamConstants.BLOCK_HEADER_LENGTH; count = readBytes(mFileBuffer, BlockCompressedStreamConstants.BLOCK_HEADER_LENGTH, remaining); if (count != remaining) { throw new FileTruncatedException("Premature end of file"); } inflateBlock(mFileBuffer, blockLength); mCurrentOffset = 0; mBlockAddress += mLastBlockLength; mLastBlockLength = blockLength; }
/** * Reads the next block from the input stream * @param buffer buffer to read next block into. Must be at least MAX_COMPRESSED_BLOCK_SIZE in size. */ protected CompressionBlock readNextBlock(byte[] buffer) throws IOException { long blockAddress = mStreamOffset; if (buffer == null || buffer.length < BlockCompressedStreamConstants.MAX_COMPRESSED_BLOCK_SIZE) { throw new IllegalArgumentException("Decompression buffer too small."); } final int headerByteCount = readBytes(buffer, 0, BlockCompressedStreamConstants.BLOCK_HEADER_LENGTH); mStreamOffset += headerByteCount; if (headerByteCount == 0) { // Handle case where there is no empty gzip block at end. return null; } if (headerByteCount != BlockCompressedStreamConstants.BLOCK_HEADER_LENGTH) { throw new IOException(INCORRECT_HEADER_SIZE_MSG + getSource()); } final int blockLength = unpackInt16(buffer, BlockCompressedStreamConstants.BLOCK_LENGTH_OFFSET) + 1; if (blockLength < BlockCompressedStreamConstants.BLOCK_HEADER_LENGTH || blockLength > buffer.length) { throw new IOException(UNEXPECTED_BLOCK_LENGTH_MSG + blockLength + " for " + getSource()); } final int remaining = blockLength - BlockCompressedStreamConstants.BLOCK_HEADER_LENGTH; final int dataByteCount = readBytes(buffer, BlockCompressedStreamConstants.BLOCK_HEADER_LENGTH, remaining); mStreamOffset += dataByteCount; if (dataByteCount != remaining) { throw new FileTruncatedException(PREMATURE_END_MSG + getSource()); } return new CompressionBlock(blockAddress, buffer, blockLength); }
return new DecompressedBlock(blockAddress, headerByteCount, new IOException(INCORRECT_HEADER_SIZE_MSG + getSource())); final int blockLength = unpackInt16(mFileBuffer, BlockCompressedStreamConstants.BLOCK_LENGTH_OFFSET) + 1; if (blockLength < BlockCompressedStreamConstants.BLOCK_HEADER_LENGTH || blockLength > mFileBuffer.length) { return new DecompressedBlock(blockAddress, blockLength,
return new DecompressedBlock(blockAddress, headerByteCount, new IOException(INCORRECT_HEADER_SIZE_MSG + getSource())); final int blockLength = unpackInt16(mFileBuffer, BlockCompressedStreamConstants.BLOCK_LENGTH_OFFSET) + 1; if (blockLength < BlockCompressedStreamConstants.BLOCK_HEADER_LENGTH || blockLength > mFileBuffer.length) { return new DecompressedBlock(blockAddress, blockLength,