From 0fcb9f8b9a591fb32e4e711ebaa6b0d48f2f9ef4 Mon Sep 17 00:00:00 2001 From: "Taro L. Saito" Date: Thu, 31 Mar 2011 21:47:47 +0900 Subject: [PATCH] Add preamble to Snappy stream --- .../org/xerial/snappy/SnappyInputStream.java | 75 +++++++++++++++++++ .../org/xerial/snappy/SnappyOutputStream.java | 10 ++- 2 files changed, 81 insertions(+), 4 deletions(-) create mode 100755 src/main/java/org/xerial/snappy/SnappyInputStream.java diff --git a/src/main/java/org/xerial/snappy/SnappyInputStream.java b/src/main/java/org/xerial/snappy/SnappyInputStream.java new file mode 100755 index 0000000..0cc9dde --- /dev/null +++ b/src/main/java/org/xerial/snappy/SnappyInputStream.java @@ -0,0 +1,75 @@ +/*-------------------------------------------------------------------------- + * Copyright 2011 Taro L. Saito + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *--------------------------------------------------------------------------*/ +//-------------------------------------- +// XerialJ +// +// SnappyInputStream.java +// Since: 2011/03/31 20:14:56 +// +// $URL$ +// $Author$ +//-------------------------------------- +package org.xerial.snappy; + +import java.io.IOException; +import java.io.InputStream; + +/** + * A stream filter for reading data compressed by {@link SnappyOutputStream}. + * + * @author leo + * + */ +public class SnappyInputStream extends InputStream +{ + protected final InputStream in; + private int blockSize = SnappyOutputStream.DEFAULT_BLOCK_SIZE; + + private byte[] compressed; + private byte[] uncompressed; + + public SnappyInputStream(InputStream input) throws IOException { + this.in = input; + readHeader(); + } + + protected void readHeader() throws IOException { + byte[] header = new byte[SnappyOutputStream.PREAMBLE_SIZE]; + int readBytes = in.read(header); + if (readBytes != header.length) { + throw new IOException("Invalid Snappy stream"); + } + String headerStr = new String(header, 0, SnappyOutputStream.HEADER_SIZE, "UTF-8"); + if (!headerStr.startsWith(SnappyOutputStream.STREAM_FORMAT_VERSION)) { + throw new IOException("Incompatible stream version"); + } + blockSize = SnappyOutputStream.readInt(header, SnappyOutputStream.HEADER_SIZE); + if (blockSize < 0) { + throw new IOException("Invalid block size: " + blockSize); + } + + compressed = new byte[blockSize]; + uncompressed = new byte[blockSize]; + + } + + @Override + public int read() throws IOException { + // TODO Auto-generated method stub + return 0; + } + +} diff --git a/src/main/java/org/xerial/snappy/SnappyOutputStream.java b/src/main/java/org/xerial/snappy/SnappyOutputStream.java index 718ead8..b6d0859 100755 --- a/src/main/java/org/xerial/snappy/SnappyOutputStream.java +++ b/src/main/java/org/xerial/snappy/SnappyOutputStream.java @@ -43,13 +43,15 @@ import java.util.Arrays; */ public class SnappyOutputStream extends OutputStream { - public static final int HEADER_SIZE = 16 + 4; // version (16 bytes) & block size (int: 4 bytes) + public final static String STREAM_FORMAT_VERSION = "snappy-1.0.1"; + public static final int HEADER_SIZE = 16; // version (16 bytes) + public static final int PREAMBLE_SIZE = HEADER_SIZE + 4; // version (16 bytes) & block size (int: 4 bytes) - private static final int DEFAULT_BLOCK_SIZE = 1 << 15; // use 2^15 = 32KB as block size + static final int DEFAULT_BLOCK_SIZE = 1 << 15; // use 2^15 = 32KB as block size protected final OutputStream out; private final int blockSize; - private int cursor = 0; + private int cursor = 0; protected byte[] uncompressed; protected byte[] compressed; @@ -68,7 +70,7 @@ public class SnappyOutputStream extends OutputStream protected void writeHeader() throws IOException { byte[] header = new byte[16]; // header size Arrays.fill(header, (byte) 0); - byte[] version = "snappy-1.0.1".getBytes("UTF-8"); + byte[] version = STREAM_FORMAT_VERSION.getBytes("UTF-8"); assert (version.length <= 16); System.arraycopy(version, 0, header, 0, version.length); out.write(header);