static String badPositionIndexes(int start, int end, int size) { if (start < 0 || start > size) { return badPositionIndex(start, size, "start index"); } if (end < 0 || end > size) { return badPositionIndex(end, size, "end index"); } // end < start return String.format("end index (%s) must not be less than start index (%s)", end, start); }
static void checkPositionIndexes(int start, int end, int size) { // Carefully optimized for execution by hotspot (explanatory comment above) if (start < 0 || end < start || end > size) { throw new IndexOutOfBoundsException(badPositionIndexes(start, end, size)); } }
public SnappyFramedOutputStream(OutputStream out, boolean writeChecksums, int blockSize, double minCompressionRatio) throws IOException { this.out = SnappyInternalUtils.checkNotNull(out, "out is null"); this.writeChecksums = writeChecksums; SnappyInternalUtils.checkArgument(minCompressionRatio > 0 && minCompressionRatio <= 1.0, "minCompressionRatio %1s must be between (0,1.0].", minCompressionRatio); this.minCompressionRatio = minCompressionRatio; this.blockSize = blockSize; this.buffer = new byte[blockSize]; this.outputBuffer = new byte[compressor.maxCompressedLength(blockSize)]; out.write(SnappyFramed.HEADER_BYTES); SnappyInternalUtils.checkArgument(blockSize > 0 && blockSize <= MAX_BLOCK_SIZE, "blockSize must be in (0, 65536]", blockSize); }
@Override public int read(byte[] output, int offset, int length) throws IOException { SnappyInternalUtils.checkNotNull(output, "output is null"); SnappyInternalUtils.checkPositionIndexes(offset, offset + length, output.length); if (closed) { throw new IOException("Stream is closed"); } if (length == 0) { return 0; } if (!ensureBuffer()) { return -1; } int size = min(length, available()); System.arraycopy(buffer, position, output, offset, size); position += size; return size; }
private boolean readBlockHeader() throws IOException { int read = SnappyInternalUtils.readBytes(in, frameHeader, 0, frameHeader.length); if (read == -1) { return false; } if (read < frameHeader.length) { throw new EOFException("encountered EOF while reading block header"); } return true; }
public static int decompress( final Object inputBase, final long inputAddress, final long inputLimit, final Object outputBase, final long outputAddress, final long outputLimit) { // Read the uncompressed length from the front of the input long input = inputAddress; int[] varInt = readUncompressedLength(inputBase, input, inputLimit); int expectedLength = varInt[0]; input += varInt[1]; SnappyInternalUtils.checkArgument(expectedLength <= (outputLimit - outputAddress), "Uncompressed length %s must be less than %s", expectedLength, (outputLimit - outputAddress)); // Process the entire input int uncompressedSize = uncompressAll( inputBase, input, inputLimit, outputBase, outputAddress, outputLimit); if (!(expectedLength == uncompressedSize)) { throw new MalformedInputException(0, String.format("Recorded length is %s bytes but actual length after decompression is %s bytes ", expectedLength, uncompressedSize)); } return expectedLength; }
throws IOException checkNotNull(source, "source is null"); checkNotNull(dest, "dest is null");
@Override public int read(byte[] output, int offset, int length) throws IOException { SnappyInternalUtils.checkNotNull(output, "output is null"); SnappyInternalUtils.checkPositionIndexes(offset, offset + length, output.length); if (closed) { throw new IOException("Stream is closed"); } if (length == 0) { return 0; } if (!ensureBuffer()) { return -1; } int size = min(length, available()); System.arraycopy(buffer, position, output, offset, size); position += size; return size; }
private boolean readBlockHeader() throws IOException { int read = SnappyInternalUtils.readBytes(in, frameHeader, 0, frameHeader.length); if (read == -1) { return false; } if (read < frameHeader.length) { throw new EOFException("encountered EOF while reading block header"); } return true; }
public static int decompress( final Object inputBase, final long inputAddress, final long inputLimit, final Object outputBase, final long outputAddress, final long outputLimit) { // Read the uncompressed length from the front of the input long input = inputAddress; int[] varInt = readUncompressedLength(inputBase, input, inputLimit); int expectedLength = varInt[0]; input += varInt[1]; SnappyInternalUtils.checkArgument(expectedLength <= (outputLimit - outputAddress), "Uncompressed length %s must be less than %s", expectedLength, (outputLimit - outputAddress)); // Process the entire input int uncompressedSize = uncompressAll( inputBase, input, inputLimit, outputBase, outputAddress, outputLimit); if (!(expectedLength == uncompressedSize)) { throw new MalformedInputException(0, String.format("Recorded length is %s bytes but actual length after decompression is %s bytes ", expectedLength, uncompressedSize)); } return expectedLength; }
throws IOException checkNotNull(source, "source is null"); checkNotNull(dest, "dest is null");
throws IOException SnappyInternalUtils.checkNotNull(input, "input is null"); SnappyInternalUtils.checkPositionIndexes(offset, offset + length, input.length); if (closed) { throw new IOException("Stream is closed");
public SnappyFramedOutputStream(OutputStream out, boolean writeChecksums, int blockSize, double minCompressionRatio) throws IOException { this.out = SnappyInternalUtils.checkNotNull(out, "out is null"); this.writeChecksums = writeChecksums; SnappyInternalUtils.checkArgument(minCompressionRatio > 0 && minCompressionRatio <= 1.0, "minCompressionRatio %1s must be between (0,1.0].", minCompressionRatio); this.minCompressionRatio = minCompressionRatio; this.blockSize = blockSize; this.buffer = new byte[blockSize]; this.outputBuffer = new byte[compressor.maxCompressedLength(blockSize)]; out.write(SnappyFramed.HEADER_BYTES); SnappyInternalUtils.checkArgument(blockSize > 0 && blockSize <= MAX_BLOCK_SIZE, "blockSize must be in (0, 65536]", blockSize); }
public SnappyFramedInputStream(InputStream in, boolean verifyChecksums) throws IOException { this.in = in; this.verifyChecksums = verifyChecksums; allocateBuffersBasedOnSize(MAX_BLOCK_SIZE + 5); this.frameHeader = new byte[4]; // stream must begin with stream header byte[] actualHeader = new byte[SnappyFramed.HEADER_BYTES.length]; int read = SnappyInternalUtils.readBytes(in, actualHeader, 0, actualHeader.length); if (read < SnappyFramed.HEADER_BYTES.length) { throw new EOFException("encountered EOF while reading stream header"); } if (!Arrays.equals(SnappyFramed.HEADER_BYTES, actualHeader)) { throw new IOException("invalid stream header"); } }
static void checkPositionIndexes(int start, int end, int size) { // Carefully optimized for execution by hotspot (explanatory comment above) if (start < 0 || end < start || end > size) { throw new IndexOutOfBoundsException(badPositionIndexes(start, end, size)); } }
static String badPositionIndexes(int start, int end, int size) { if (start < 0 || start > size) { return badPositionIndex(start, size, "start index"); } if (end < 0 || end > size) { return badPositionIndex(end, size, "end index"); } // end < start return String.format("end index (%s) must not be less than start index (%s)", end, start); }
throws IOException SnappyInternalUtils.checkNotNull(input, "input is null"); SnappyInternalUtils.checkPositionIndexes(offset, offset + length, input.length); if (closed) { throw new IOException("Stream is closed");
public SnappyFramedInputStream(InputStream in, boolean verifyChecksums) throws IOException { this.in = in; this.verifyChecksums = verifyChecksums; allocateBuffersBasedOnSize(MAX_BLOCK_SIZE + 5); this.frameHeader = new byte[4]; // stream must begin with stream header byte[] actualHeader = new byte[SnappyFramed.HEADER_BYTES.length]; int read = SnappyInternalUtils.readBytes(in, actualHeader, 0, actualHeader.length); if (read < SnappyFramed.HEADER_BYTES.length) { throw new EOFException("encountered EOF while reading stream header"); } if (!Arrays.equals(SnappyFramed.HEADER_BYTES, actualHeader)) { throw new IOException("invalid stream header"); } }