private static boolean isValidHeader(byte[] header) throws IOException { SnappyCodec codec = SnappyCodec.readHeader(new ByteArrayInputStream(header)); if (codec.isValidMagicHeader()) { // The input data is compressed by SnappyOutputStream if (codec.version < SnappyCodec.MINIMUM_COMPATIBLE_VERSION) { throw new SnappyIOException(SnappyErrorCode.INCOMPATIBLE_VERSION, String.format( "Compressed with an incompatible codec version %d. At least version %d is required", codec.version, SnappyCodec.MINIMUM_COMPATIBLE_VERSION)); } return true; } else { return false; } }
public static SnappyCodec readHeader(InputStream in) throws IOException { DataInputStream d = new DataInputStream(in); byte[] magic = new byte[MAGIC_LEN]; d.readFully(magic, 0, MAGIC_LEN); int version = d.readInt(); int compatibleVersion = d.readInt(); return new SnappyCodec(magic, version, compatibleVersion); } }
protected int writeHeader(){ return SnappyCodec.currentHeader.writeHeader(outputBuffer, 0); }
String fileName = "foo.snap"; // write a snappy compressed file try (OutputStream os = new SnappyOutputStream(new FileOutputStream(fileName))) { os.write("Hello Snappy-World".getBytes(Charset.defaultCharset())); } // read a snappy compressed file try (InputStream is = new SnappyInputStream(new FileInputStream(fileName))) { byte[] bytes = new byte[100]; is.read(bytes); System.out.println(new String(bytes, Charset.defaultCharset())); } // check if the file is compressed with the snappy algorithm try (InputStream is = new FileInputStream(fileName)) { SnappyCodec readHeader = SnappyCodec.readHeader(is); if (readHeader.isValidMagicHeader()) { System.out.println("is a Snappy compressed file"); System.out.printf("%s: %d%n%s: %d%n", "compatible version", readHeader.compatibleVersion, "version", readHeader.version ); } else { System.out.println("is not a Snappy compressed file"); } }
protected void readHeader() throws IOException { int readBytes = 0; while (readBytes < header.length) { int ret = in.read(header, readBytes, header.length - readBytes); if (ret == -1) { break; } readBytes += ret; } // Quick test of the header if (readBytes == 0) { // Snappy produces at least 1-byte result. So the empty input is not a valid input throw new SnappyIOException(SnappyErrorCode.EMPTY_INPUT, "Cannot decompress empty stream"); } if (readBytes < header.length || !SnappyCodec.hasMagicHeaderPrefix(header)) { // do the default uncompression // (probably) compressed by Snappy.compress(byte[]) readFully(header, readBytes); return; } }
if (chunkSize == SnappyCodec.MAGIC_HEADER_HEAD) { int remainingHeaderSize = SnappyCodec.headerSize() - 4; readBytes = readNext(header, 4, remainingHeaderSize); if(readBytes < remainingHeaderSize) {