mirror of
https://github.com/xerial/snappy-java.git
synced 2025-07-23 14:04:39 +02:00
Add preamble to Snappy stream
This commit is contained in:
parent
b1158e563f
commit
0fcb9f8b9a
75
src/main/java/org/xerial/snappy/SnappyInputStream.java
Executable file
75
src/main/java/org/xerial/snappy/SnappyInputStream.java
Executable file
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -43,9 +43,11 @@ import java.util.Arrays;
|
|||||||
*/
|
*/
|
||||||
public class SnappyOutputStream extends OutputStream
|
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;
|
protected final OutputStream out;
|
||||||
private final int blockSize;
|
private final int blockSize;
|
||||||
@ -68,7 +70,7 @@ public class SnappyOutputStream extends OutputStream
|
|||||||
protected void writeHeader() throws IOException {
|
protected void writeHeader() throws IOException {
|
||||||
byte[] header = new byte[16]; // header size
|
byte[] header = new byte[16]; // header size
|
||||||
Arrays.fill(header, (byte) 0);
|
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);
|
assert (version.length <= 16);
|
||||||
System.arraycopy(version, 0, header, 0, version.length);
|
System.arraycopy(version, 0, header, 0, version.length);
|
||||||
out.write(header);
|
out.write(header);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user