Use LF as newline
This commit is contained in:
parent
6b4df5a5f8
commit
3cf34325d7
|
@ -1,5 +1,5 @@
|
|||
|
||||
#ifndef __CONFIG_H
|
||||
#define __CONFIG_H
|
||||
|
||||
#endif // __CONFIG_H
|
||||
|
||||
#ifndef __CONFIG_H
|
||||
#define __CONFIG_H
|
||||
|
||||
#endif // __CONFIG_H
|
||||
|
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
|
@ -1,67 +1,67 @@
|
|||
/*--------------------------------------------------------------------------
|
||||
* 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
|
||||
//
|
||||
// SnappyBundleActivator.java
|
||||
// Since: 2011/06/22 10:01:46
|
||||
//
|
||||
// $URL$
|
||||
// $Author$
|
||||
//--------------------------------------
|
||||
package org.xerial.snappy;
|
||||
|
||||
import org.osgi.framework.Bundle;
|
||||
import org.osgi.framework.BundleActivator;
|
||||
import org.osgi.framework.BundleContext;
|
||||
|
||||
import java.util.jar.Manifest;
|
||||
|
||||
/**
|
||||
* OSGi bundle entry point
|
||||
*
|
||||
* @author leo
|
||||
*
|
||||
*/
|
||||
public class SnappyBundleActivator implements BundleActivator
|
||||
{
|
||||
/**
|
||||
* Name of the Snappy native library
|
||||
*/
|
||||
public static final String LIBRARY_NAME = "snappyjava";
|
||||
|
||||
/**
|
||||
* Make a call to {@link System#loadLibrary(String)} to load the native library which assumes
|
||||
* that the library is available on the path based on this {@link Bundle}'s {@link Manifest}.
|
||||
*/
|
||||
public void start(BundleContext context) throws Exception
|
||||
/*--------------------------------------------------------------------------
|
||||
* 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
|
||||
//
|
||||
// SnappyBundleActivator.java
|
||||
// Since: 2011/06/22 10:01:46
|
||||
//
|
||||
// $URL$
|
||||
// $Author$
|
||||
//--------------------------------------
|
||||
package org.xerial.snappy;
|
||||
|
||||
import org.osgi.framework.Bundle;
|
||||
import org.osgi.framework.BundleActivator;
|
||||
import org.osgi.framework.BundleContext;
|
||||
|
||||
import java.util.jar.Manifest;
|
||||
|
||||
/**
|
||||
* OSGi bundle entry point
|
||||
*
|
||||
* @author leo
|
||||
*
|
||||
*/
|
||||
public class SnappyBundleActivator implements BundleActivator
|
||||
{
|
||||
/**
|
||||
* Name of the Snappy native library
|
||||
*/
|
||||
public static final String LIBRARY_NAME = "snappyjava";
|
||||
|
||||
/**
|
||||
* Make a call to {@link System#loadLibrary(String)} to load the native library which assumes
|
||||
* that the library is available on the path based on this {@link Bundle}'s {@link Manifest}.
|
||||
*/
|
||||
public void start(BundleContext context) throws Exception
|
||||
{
|
||||
String library = System.mapLibraryName(LIBRARY_NAME);
|
||||
if (library.toLowerCase().endsWith(".dylib"))
|
||||
{
|
||||
// some MacOS JDK7+ vendors map to dylib instead of jnilib
|
||||
library = library.replace(".dylib", ".jnilib");
|
||||
}
|
||||
System.loadLibrary(library);
|
||||
SnappyLoader.setApi(new SnappyNative());
|
||||
}
|
||||
|
||||
public void stop(BundleContext context) throws Exception
|
||||
{
|
||||
SnappyLoader.setApi(null);
|
||||
SnappyLoader.cleanUpExtractedNativeLib();
|
||||
}
|
||||
}
|
||||
}
|
||||
System.loadLibrary(library);
|
||||
SnappyLoader.setApi(new SnappyNative());
|
||||
}
|
||||
|
||||
public void stop(BundleContext context) throws Exception
|
||||
{
|
||||
SnappyLoader.setApi(null);
|
||||
SnappyLoader.cleanUpExtractedNativeLib();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,124 +1,124 @@
|
|||
/*--------------------------------------------------------------------------
|
||||
* 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
|
||||
//
|
||||
// SnappyCodec.java
|
||||
// Since: 2011/04/03 14:50:20
|
||||
//
|
||||
// $URL$
|
||||
// $Author$
|
||||
//--------------------------------------
|
||||
package org.xerial.snappy;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* Preamble header for {@link SnappyOutputStream}.
|
||||
*
|
||||
* <p>
|
||||
* The magic header is the following 8 bytes data:
|
||||
*
|
||||
* <pre>
|
||||
* -126, 'S', 'N', 'A', 'P', 'P', 'Y', 0
|
||||
* </pre>
|
||||
*
|
||||
* </p>
|
||||
*
|
||||
* @author leo
|
||||
*
|
||||
*/
|
||||
public class SnappyCodec
|
||||
{
|
||||
public static final byte[] MAGIC_HEADER = new byte[] { -126, 'S', 'N', 'A', 'P', 'P', 'Y', 0 };
|
||||
public static final int MAGIC_LEN = MAGIC_HEADER.length;
|
||||
public static final int HEADER_SIZE = MAGIC_LEN + 8;
|
||||
public static final int MAGIC_HEADER_HEAD = SnappyOutputStream.readInt(MAGIC_HEADER, 0);
|
||||
public static final int MAGIC_HEADER_TAIL = SnappyOutputStream.readInt(MAGIC_HEADER, 4);
|
||||
|
||||
static {
|
||||
assert(MAGIC_HEADER_HEAD < 0);
|
||||
}
|
||||
|
||||
|
||||
public static final int DEFAULT_VERSION = 1;
|
||||
public static final int MINIMUM_COMPATIBLE_VERSION = 1;
|
||||
|
||||
public final byte[] magic;
|
||||
public final int version;
|
||||
public final int compatibleVersion;
|
||||
private final byte[] headerArray;
|
||||
|
||||
private SnappyCodec(byte[] magic, int version, int compatibleVersion) {
|
||||
this.magic = magic;
|
||||
this.version = version;
|
||||
this.compatibleVersion = compatibleVersion;
|
||||
|
||||
ByteArrayOutputStream header = new ByteArrayOutputStream(HEADER_SIZE);
|
||||
DataOutputStream d = new DataOutputStream(header);
|
||||
try {
|
||||
d.write(magic, 0, MAGIC_LEN);
|
||||
d.writeInt(version);
|
||||
d.writeInt(compatibleVersion);
|
||||
d.close();
|
||||
}
|
||||
catch(IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
headerArray = header.toByteArray();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("version:%d, compatible version:%d", version, compatibleVersion);
|
||||
}
|
||||
|
||||
public static int headerSize() {
|
||||
return HEADER_SIZE;
|
||||
}
|
||||
|
||||
public int writeHeader(byte[] dst, int dstOffset) {
|
||||
System.arraycopy(headerArray, 0, dst, dstOffset, headerArray.length);
|
||||
return headerArray.length;
|
||||
}
|
||||
|
||||
public int writeHeader(OutputStream out) throws IOException {
|
||||
out.write(headerArray, 0, headerArray.length);
|
||||
return headerArray.length;
|
||||
}
|
||||
|
||||
public boolean isValidMagicHeader() {
|
||||
return Arrays.equals(MAGIC_HEADER, magic);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
public static SnappyCodec currentHeader = new SnappyCodec(MAGIC_HEADER, DEFAULT_VERSION, MINIMUM_COMPATIBLE_VERSION);
|
||||
|
||||
}
|
||||
/*--------------------------------------------------------------------------
|
||||
* 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
|
||||
//
|
||||
// SnappyCodec.java
|
||||
// Since: 2011/04/03 14:50:20
|
||||
//
|
||||
// $URL$
|
||||
// $Author$
|
||||
//--------------------------------------
|
||||
package org.xerial.snappy;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* Preamble header for {@link SnappyOutputStream}.
|
||||
*
|
||||
* <p>
|
||||
* The magic header is the following 8 bytes data:
|
||||
*
|
||||
* <pre>
|
||||
* -126, 'S', 'N', 'A', 'P', 'P', 'Y', 0
|
||||
* </pre>
|
||||
*
|
||||
* </p>
|
||||
*
|
||||
* @author leo
|
||||
*
|
||||
*/
|
||||
public class SnappyCodec
|
||||
{
|
||||
public static final byte[] MAGIC_HEADER = new byte[] { -126, 'S', 'N', 'A', 'P', 'P', 'Y', 0 };
|
||||
public static final int MAGIC_LEN = MAGIC_HEADER.length;
|
||||
public static final int HEADER_SIZE = MAGIC_LEN + 8;
|
||||
public static final int MAGIC_HEADER_HEAD = SnappyOutputStream.readInt(MAGIC_HEADER, 0);
|
||||
public static final int MAGIC_HEADER_TAIL = SnappyOutputStream.readInt(MAGIC_HEADER, 4);
|
||||
|
||||
static {
|
||||
assert(MAGIC_HEADER_HEAD < 0);
|
||||
}
|
||||
|
||||
|
||||
public static final int DEFAULT_VERSION = 1;
|
||||
public static final int MINIMUM_COMPATIBLE_VERSION = 1;
|
||||
|
||||
public final byte[] magic;
|
||||
public final int version;
|
||||
public final int compatibleVersion;
|
||||
private final byte[] headerArray;
|
||||
|
||||
private SnappyCodec(byte[] magic, int version, int compatibleVersion) {
|
||||
this.magic = magic;
|
||||
this.version = version;
|
||||
this.compatibleVersion = compatibleVersion;
|
||||
|
||||
ByteArrayOutputStream header = new ByteArrayOutputStream(HEADER_SIZE);
|
||||
DataOutputStream d = new DataOutputStream(header);
|
||||
try {
|
||||
d.write(magic, 0, MAGIC_LEN);
|
||||
d.writeInt(version);
|
||||
d.writeInt(compatibleVersion);
|
||||
d.close();
|
||||
}
|
||||
catch(IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
headerArray = header.toByteArray();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("version:%d, compatible version:%d", version, compatibleVersion);
|
||||
}
|
||||
|
||||
public static int headerSize() {
|
||||
return HEADER_SIZE;
|
||||
}
|
||||
|
||||
public int writeHeader(byte[] dst, int dstOffset) {
|
||||
System.arraycopy(headerArray, 0, dst, dstOffset, headerArray.length);
|
||||
return headerArray.length;
|
||||
}
|
||||
|
||||
public int writeHeader(OutputStream out) throws IOException {
|
||||
out.write(headerArray, 0, headerArray.length);
|
||||
return headerArray.length;
|
||||
}
|
||||
|
||||
public boolean isValidMagicHeader() {
|
||||
return Arrays.equals(MAGIC_HEADER, magic);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
public static SnappyCodec currentHeader = new SnappyCodec(MAGIC_HEADER, DEFAULT_VERSION, MINIMUM_COMPATIBLE_VERSION);
|
||||
|
||||
}
|
||||
|
|
|
@ -1,62 +1,62 @@
|
|||
/*--------------------------------------------------------------------------
|
||||
* 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
|
||||
//
|
||||
// SnappyError.java
|
||||
// Since: 2011/03/30 15:22:43
|
||||
//
|
||||
// $URL$
|
||||
// $Author$
|
||||
//--------------------------------------
|
||||
package org.xerial.snappy;
|
||||
|
||||
/**
|
||||
* Used when serious errors (unchecked exception) are observed.
|
||||
*
|
||||
* @author leo
|
||||
*
|
||||
*/
|
||||
public class SnappyError extends Error
|
||||
{
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public final SnappyErrorCode errorCode;
|
||||
|
||||
public SnappyError(SnappyErrorCode code) {
|
||||
super();
|
||||
this.errorCode = code;
|
||||
}
|
||||
|
||||
public SnappyError(SnappyErrorCode code, Error e) {
|
||||
super(e);
|
||||
this.errorCode = code;
|
||||
}
|
||||
|
||||
public SnappyError(SnappyErrorCode code, String message) {
|
||||
super(message);
|
||||
this.errorCode = code;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMessage() {
|
||||
return String.format("[%s] %s", errorCode.name(), super.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
/*--------------------------------------------------------------------------
|
||||
* 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
|
||||
//
|
||||
// SnappyError.java
|
||||
// Since: 2011/03/30 15:22:43
|
||||
//
|
||||
// $URL$
|
||||
// $Author$
|
||||
//--------------------------------------
|
||||
package org.xerial.snappy;
|
||||
|
||||
/**
|
||||
* Used when serious errors (unchecked exception) are observed.
|
||||
*
|
||||
* @author leo
|
||||
*
|
||||
*/
|
||||
public class SnappyError extends Error
|
||||
{
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public final SnappyErrorCode errorCode;
|
||||
|
||||
public SnappyError(SnappyErrorCode code) {
|
||||
super();
|
||||
this.errorCode = code;
|
||||
}
|
||||
|
||||
public SnappyError(SnappyErrorCode code, Error e) {
|
||||
super(e);
|
||||
this.errorCode = code;
|
||||
}
|
||||
|
||||
public SnappyError(SnappyErrorCode code, String message) {
|
||||
super(message);
|
||||
this.errorCode = code;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMessage() {
|
||||
return String.format("[%s] %s", errorCode.name(), super.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,64 +1,64 @@
|
|||
/*--------------------------------------------------------------------------
|
||||
* 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
|
||||
//
|
||||
// SnappyErrorCode.java
|
||||
// Since: 2011/03/30 14:56:50
|
||||
//
|
||||
// $URL$
|
||||
// $Author$
|
||||
//--------------------------------------
|
||||
package org.xerial.snappy;
|
||||
|
||||
/**
|
||||
* Error codes of snappy-java
|
||||
*
|
||||
* @author leo
|
||||
*
|
||||
*/
|
||||
public enum SnappyErrorCode {
|
||||
|
||||
// DO NOT change these error code IDs because these numbers are used inside SnappyNative.cpp
|
||||
UNKNOWN(0),
|
||||
FAILED_TO_LOAD_NATIVE_LIBRARY(1),
|
||||
PARSING_ERROR(2),
|
||||
NOT_A_DIRECT_BUFFER(3),
|
||||
OUT_OF_MEMORY(4),
|
||||
FAILED_TO_UNCOMPRESS(5),
|
||||
EMPTY_INPUT(6),
|
||||
INCOMPATIBLE_VERSION(7),
|
||||
INVALID_CHUNK_SIZE(8)
|
||||
;
|
||||
|
||||
public final int id;
|
||||
|
||||
private SnappyErrorCode(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public static SnappyErrorCode getErrorCode(int id) {
|
||||
for (SnappyErrorCode code : SnappyErrorCode.values()) {
|
||||
if (code.id == id)
|
||||
return code;
|
||||
}
|
||||
return UNKNOWN;
|
||||
}
|
||||
|
||||
public static String getErrorMessage(int id) {
|
||||
return getErrorCode(id).name();
|
||||
}
|
||||
}
|
||||
/*--------------------------------------------------------------------------
|
||||
* 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
|
||||
//
|
||||
// SnappyErrorCode.java
|
||||
// Since: 2011/03/30 14:56:50
|
||||
//
|
||||
// $URL$
|
||||
// $Author$
|
||||
//--------------------------------------
|
||||
package org.xerial.snappy;
|
||||
|
||||
/**
|
||||
* Error codes of snappy-java
|
||||
*
|
||||
* @author leo
|
||||
*
|
||||
*/
|
||||
public enum SnappyErrorCode {
|
||||
|
||||
// DO NOT change these error code IDs because these numbers are used inside SnappyNative.cpp
|
||||
UNKNOWN(0),
|
||||
FAILED_TO_LOAD_NATIVE_LIBRARY(1),
|
||||
PARSING_ERROR(2),
|
||||
NOT_A_DIRECT_BUFFER(3),
|
||||
OUT_OF_MEMORY(4),
|
||||
FAILED_TO_UNCOMPRESS(5),
|
||||
EMPTY_INPUT(6),
|
||||
INCOMPATIBLE_VERSION(7),
|
||||
INVALID_CHUNK_SIZE(8)
|
||||
;
|
||||
|
||||
public final int id;
|
||||
|
||||
private SnappyErrorCode(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public static SnappyErrorCode getErrorCode(int id) {
|
||||
for (SnappyErrorCode code : SnappyErrorCode.values()) {
|
||||
if (code.id == id)
|
||||
return code;
|
||||
}
|
||||
return UNKNOWN;
|
||||
}
|
||||
|
||||
public static String getErrorMessage(int id) {
|
||||
return getErrorCode(id).name();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,74 +1,74 @@
|
|||
/*--------------------------------------------------------------------------
|
||||
* 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
|
||||
//
|
||||
// SnappyException.java
|
||||
// Since: 2011/03/30 14:56:14
|
||||
//
|
||||
// $URL$
|
||||
// $Author$
|
||||
//--------------------------------------
|
||||
package org.xerial.snappy;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Exception in snappy-java
|
||||
*
|
||||
* @deprecated Snappy-java now uses {@link IOException}
|
||||
* @author leo
|
||||
*
|
||||
*/
|
||||
@Deprecated
|
||||
public class SnappyException extends Exception
|
||||
{
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public final SnappyErrorCode errorCode;
|
||||
|
||||
public SnappyException(int code) {
|
||||
this(SnappyErrorCode.getErrorCode(code));
|
||||
}
|
||||
|
||||
public SnappyException(SnappyErrorCode errorCode) {
|
||||
super();
|
||||
this.errorCode = errorCode;
|
||||
}
|
||||
|
||||
public SnappyException(SnappyErrorCode errorCode, Exception e) {
|
||||
super(e);
|
||||
this.errorCode = errorCode;
|
||||
}
|
||||
|
||||
public SnappyException(SnappyErrorCode errorCode, String message) {
|
||||
super(message);
|
||||
this.errorCode = errorCode;
|
||||
}
|
||||
|
||||
public SnappyErrorCode getErrorCode() {
|
||||
return errorCode;
|
||||
}
|
||||
|
||||
public static void throwException(int errorCode) throws SnappyException {
|
||||
throw new SnappyException(errorCode);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMessage() {
|
||||
return String.format("[%s] %s", errorCode.name(), super.getMessage());
|
||||
}
|
||||
}
|
||||
/*--------------------------------------------------------------------------
|
||||
* 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
|
||||
//
|
||||
// SnappyException.java
|
||||
// Since: 2011/03/30 14:56:14
|
||||
//
|
||||
// $URL$
|
||||
// $Author$
|
||||
//--------------------------------------
|
||||
package org.xerial.snappy;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Exception in snappy-java
|
||||
*
|
||||
* @deprecated Snappy-java now uses {@link IOException}
|
||||
* @author leo
|
||||
*
|
||||
*/
|
||||
@Deprecated
|
||||
public class SnappyException extends Exception
|
||||
{
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public final SnappyErrorCode errorCode;
|
||||
|
||||
public SnappyException(int code) {
|
||||
this(SnappyErrorCode.getErrorCode(code));
|
||||
}
|
||||
|
||||
public SnappyException(SnappyErrorCode errorCode) {
|
||||
super();
|
||||
this.errorCode = errorCode;
|
||||
}
|
||||
|
||||
public SnappyException(SnappyErrorCode errorCode, Exception e) {
|
||||
super(e);
|
||||
this.errorCode = errorCode;
|
||||
}
|
||||
|
||||
public SnappyException(SnappyErrorCode errorCode, String message) {
|
||||
super(message);
|
||||
this.errorCode = errorCode;
|
||||
}
|
||||
|
||||
public SnappyErrorCode getErrorCode() {
|
||||
return errorCode;
|
||||
}
|
||||
|
||||
public static void throwException(int errorCode) throws SnappyException {
|
||||
throw new SnappyException(errorCode);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMessage() {
|
||||
return String.format("[%s] %s", errorCode.name(), super.getMessage());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,186 +1,186 @@
|
|||
/*
|
||||
* Created: Apr 12, 2013
|
||||
*/
|
||||
package org.xerial.snappy;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.ReadableByteChannel;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* Constants and utilities for implementing x-snappy-framed.
|
||||
*
|
||||
* @author Brett Okken
|
||||
* @since 1.1.0
|
||||
*/
|
||||
final class SnappyFramed {
|
||||
public static final int COMPRESSED_DATA_FLAG = 0x00;
|
||||
|
||||
public static final int UNCOMPRESSED_DATA_FLAG = 0x01;
|
||||
|
||||
public static final int STREAM_IDENTIFIER_FLAG = 0xff;
|
||||
|
||||
private static final int MASK_DELTA = 0xa282ead8;
|
||||
|
||||
/**
|
||||
* Sun specific mechanisms to clean up resources associated with direct byte buffers.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
private static final Class<? extends ByteBuffer> SUN_DIRECT_BUFFER = (Class<? extends ByteBuffer>) lookupClassQuietly("sun.nio.ch.DirectBuffer");
|
||||
private static final Method SUN_BUFFER_CLEANER;
|
||||
private static final Method SUN_CLEANER_CLEAN;
|
||||
|
||||
static
|
||||
{
|
||||
Method bufferCleaner = null;
|
||||
Method cleanerClean = null;
|
||||
try {
|
||||
//operate under the assumption that if the sun direct buffer class exists,
|
||||
//all of the sun classes exist
|
||||
if (SUN_DIRECT_BUFFER != null) {
|
||||
bufferCleaner = SUN_DIRECT_BUFFER.getMethod("cleaner", (Class[])null);
|
||||
Class<?> cleanClazz = lookupClassQuietly("sun.misc.Cleaner");
|
||||
cleanerClean = cleanClazz.getMethod("clean", (Class[])null);
|
||||
}
|
||||
} catch(Throwable t) {
|
||||
Logger.getLogger(SnappyFramed.class.getName()).log(Level.FINE, "Exception occurred attempting to lookup Sun specific DirectByteBuffer cleaner classes.", t);
|
||||
}
|
||||
SUN_BUFFER_CLEANER = bufferCleaner;
|
||||
SUN_CLEANER_CLEAN = cleanerClean;
|
||||
}
|
||||
|
||||
/**
|
||||
* The header consists of the stream identifier flag, 3 bytes indicating a
|
||||
* length of 6, and "sNaPpY" in ASCII.
|
||||
*/
|
||||
public static final byte[] HEADER_BYTES = new byte[] {
|
||||
(byte) STREAM_IDENTIFIER_FLAG, 0x06, 0x00, 0x00, 0x73, 0x4e, 0x61,
|
||||
0x50, 0x70, 0x59 };
|
||||
|
||||
public static int maskedCrc32c(byte[] data)
|
||||
{
|
||||
return maskedCrc32c(data, 0, data.length);
|
||||
}
|
||||
|
||||
public static int maskedCrc32c(byte[] data, int offset, int length)
|
||||
{
|
||||
final PureJavaCrc32C crc32c = new PureJavaCrc32C();
|
||||
crc32c.update(data, offset, length);
|
||||
return mask(crc32c.getIntegerValue());
|
||||
}
|
||||
|
||||
/**
|
||||
* Checksums are not stored directly, but masked, as checksumming data and
|
||||
* then its own checksum can be problematic. The masking is the same as used
|
||||
* in Apache Hadoop: Rotate the checksum by 15 bits, then add the constant
|
||||
* 0xa282ead8 (using wraparound as normal for unsigned integers). This is
|
||||
* equivalent to the following C code:
|
||||
*
|
||||
* <pre>
|
||||
* uint32_t mask_checksum(uint32_t x) {
|
||||
* return ((x >> 15) | (x << 17)) + 0xa282ead8;
|
||||
* }
|
||||
* </pre>
|
||||
*/
|
||||
public static int mask(int crc)
|
||||
{
|
||||
// Rotate right by 15 bits and add a constant.
|
||||
return ((crc >>> 15) | (crc << 17)) + MASK_DELTA;
|
||||
}
|
||||
|
||||
|
||||
static final int readBytes(ReadableByteChannel source, ByteBuffer dest) throws IOException
|
||||
{
|
||||
// tells how many bytes to read.
|
||||
final int expectedLength = dest.remaining();
|
||||
|
||||
int totalRead = 0;
|
||||
|
||||
// how many bytes were read.
|
||||
int lastRead = source.read(dest);
|
||||
|
||||
totalRead = lastRead;
|
||||
|
||||
// if we did not read as many bytes as we had hoped, try reading again.
|
||||
if (lastRead < expectedLength)
|
||||
{
|
||||
// as long the buffer is not full (remaining() == 0) and we have not reached EOF (lastRead == -1) keep reading.
|
||||
while (dest.remaining() != 0 && lastRead != -1)
|
||||
{
|
||||
lastRead = source.read(dest);
|
||||
|
||||
// if we got EOF, do not add to total read.
|
||||
if (lastRead != -1)
|
||||
{
|
||||
totalRead += lastRead;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (totalRead > 0)
|
||||
{
|
||||
dest.limit(dest.position());
|
||||
}
|
||||
else
|
||||
{
|
||||
dest.position(dest.limit());
|
||||
}
|
||||
|
||||
return totalRead;
|
||||
}
|
||||
|
||||
static int skip(final ReadableByteChannel source, final int skip, final ByteBuffer buffer) throws IOException
|
||||
{
|
||||
if (skip <= 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int toSkip = skip;
|
||||
int skipped = 0;
|
||||
while(toSkip > 0 && skipped != -1) {
|
||||
buffer.clear();
|
||||
if (toSkip < buffer.capacity()) {
|
||||
buffer.limit(toSkip);
|
||||
}
|
||||
|
||||
skipped = source.read(buffer);
|
||||
if (skipped > 0) {
|
||||
toSkip -= skipped;
|
||||
}
|
||||
}
|
||||
|
||||
buffer.clear();
|
||||
return skip - toSkip;
|
||||
}
|
||||
|
||||
private static Class<?> lookupClassQuietly(String name) {
|
||||
try {
|
||||
return SnappyFramed.class.getClassLoader().loadClass(name);
|
||||
} catch (Throwable t) {
|
||||
Logger.getLogger(SnappyFramed.class.getName()).log(Level.FINE, "Did not find requested class: " + name, t);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides jvm implementation specific operation to aggressively release resources associated with <i>buffer</i>.
|
||||
* @param buffer The {@code ByteBuffer} to release. Must not be {@code null}. Must be {@link ByteBuffer#isDirect() direct}.
|
||||
*/
|
||||
static void releaseDirectByteBuffer(ByteBuffer buffer)
|
||||
{
|
||||
assert buffer != null && buffer.isDirect();
|
||||
|
||||
if (SUN_DIRECT_BUFFER != null && SUN_DIRECT_BUFFER.isAssignableFrom(buffer.getClass())) {
|
||||
try {
|
||||
Object cleaner = SUN_BUFFER_CLEANER.invoke(buffer, (Object[]) null);
|
||||
SUN_CLEANER_CLEAN.invoke(cleaner, (Object[]) null);
|
||||
} catch (Throwable t) {
|
||||
Logger.getLogger(SnappyFramed.class.getName()).log(Level.FINE, "Exception occurred attempting to clean up Sun specific DirectByteBuffer.", t);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
/*
|
||||
* Created: Apr 12, 2013
|
||||
*/
|
||||
package org.xerial.snappy;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.ReadableByteChannel;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* Constants and utilities for implementing x-snappy-framed.
|
||||
*
|
||||
* @author Brett Okken
|
||||
* @since 1.1.0
|
||||
*/
|
||||
final class SnappyFramed {
|
||||
public static final int COMPRESSED_DATA_FLAG = 0x00;
|
||||
|
||||
public static final int UNCOMPRESSED_DATA_FLAG = 0x01;
|
||||
|
||||
public static final int STREAM_IDENTIFIER_FLAG = 0xff;
|
||||
|
||||
private static final int MASK_DELTA = 0xa282ead8;
|
||||
|
||||
/**
|
||||
* Sun specific mechanisms to clean up resources associated with direct byte buffers.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
private static final Class<? extends ByteBuffer> SUN_DIRECT_BUFFER = (Class<? extends ByteBuffer>) lookupClassQuietly("sun.nio.ch.DirectBuffer");
|
||||
private static final Method SUN_BUFFER_CLEANER;
|
||||
private static final Method SUN_CLEANER_CLEAN;
|
||||
|
||||
static
|
||||
{
|
||||
Method bufferCleaner = null;
|
||||
Method cleanerClean = null;
|
||||
try {
|
||||
//operate under the assumption that if the sun direct buffer class exists,
|
||||
//all of the sun classes exist
|
||||
if (SUN_DIRECT_BUFFER != null) {
|
||||
bufferCleaner = SUN_DIRECT_BUFFER.getMethod("cleaner", (Class[])null);
|
||||
Class<?> cleanClazz = lookupClassQuietly("sun.misc.Cleaner");
|
||||
cleanerClean = cleanClazz.getMethod("clean", (Class[])null);
|
||||
}
|
||||
} catch(Throwable t) {
|
||||
Logger.getLogger(SnappyFramed.class.getName()).log(Level.FINE, "Exception occurred attempting to lookup Sun specific DirectByteBuffer cleaner classes.", t);
|
||||
}
|
||||
SUN_BUFFER_CLEANER = bufferCleaner;
|
||||
SUN_CLEANER_CLEAN = cleanerClean;
|
||||
}
|
||||
|
||||
/**
|
||||
* The header consists of the stream identifier flag, 3 bytes indicating a
|
||||
* length of 6, and "sNaPpY" in ASCII.
|
||||
*/
|
||||
public static final byte[] HEADER_BYTES = new byte[] {
|
||||
(byte) STREAM_IDENTIFIER_FLAG, 0x06, 0x00, 0x00, 0x73, 0x4e, 0x61,
|
||||
0x50, 0x70, 0x59 };
|
||||
|
||||
public static int maskedCrc32c(byte[] data)
|
||||
{
|
||||
return maskedCrc32c(data, 0, data.length);
|
||||
}
|
||||
|
||||
public static int maskedCrc32c(byte[] data, int offset, int length)
|
||||
{
|
||||
final PureJavaCrc32C crc32c = new PureJavaCrc32C();
|
||||
crc32c.update(data, offset, length);
|
||||
return mask(crc32c.getIntegerValue());
|
||||
}
|
||||
|
||||
/**
|
||||
* Checksums are not stored directly, but masked, as checksumming data and
|
||||
* then its own checksum can be problematic. The masking is the same as used
|
||||
* in Apache Hadoop: Rotate the checksum by 15 bits, then add the constant
|
||||
* 0xa282ead8 (using wraparound as normal for unsigned integers). This is
|
||||
* equivalent to the following C code:
|
||||
*
|
||||
* <pre>
|
||||
* uint32_t mask_checksum(uint32_t x) {
|
||||
* return ((x >> 15) | (x << 17)) + 0xa282ead8;
|
||||
* }
|
||||
* </pre>
|
||||
*/
|
||||
public static int mask(int crc)
|
||||
{
|
||||
// Rotate right by 15 bits and add a constant.
|
||||
return ((crc >>> 15) | (crc << 17)) + MASK_DELTA;
|
||||
}
|
||||
|
||||
|
||||
static final int readBytes(ReadableByteChannel source, ByteBuffer dest) throws IOException
|
||||
{
|
||||
// tells how many bytes to read.
|
||||
final int expectedLength = dest.remaining();
|
||||
|
||||
int totalRead = 0;
|
||||
|
||||
// how many bytes were read.
|
||||
int lastRead = source.read(dest);
|
||||
|
||||
totalRead = lastRead;
|
||||
|
||||
// if we did not read as many bytes as we had hoped, try reading again.
|
||||
if (lastRead < expectedLength)
|
||||
{
|
||||
// as long the buffer is not full (remaining() == 0) and we have not reached EOF (lastRead == -1) keep reading.
|
||||
while (dest.remaining() != 0 && lastRead != -1)
|
||||
{
|
||||
lastRead = source.read(dest);
|
||||
|
||||
// if we got EOF, do not add to total read.
|
||||
if (lastRead != -1)
|
||||
{
|
||||
totalRead += lastRead;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (totalRead > 0)
|
||||
{
|
||||
dest.limit(dest.position());
|
||||
}
|
||||
else
|
||||
{
|
||||
dest.position(dest.limit());
|
||||
}
|
||||
|
||||
return totalRead;
|
||||
}
|
||||
|
||||
static int skip(final ReadableByteChannel source, final int skip, final ByteBuffer buffer) throws IOException
|
||||
{
|
||||
if (skip <= 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int toSkip = skip;
|
||||
int skipped = 0;
|
||||
while(toSkip > 0 && skipped != -1) {
|
||||
buffer.clear();
|
||||
if (toSkip < buffer.capacity()) {
|
||||
buffer.limit(toSkip);
|
||||
}
|
||||
|
||||
skipped = source.read(buffer);
|
||||
if (skipped > 0) {
|
||||
toSkip -= skipped;
|
||||
}
|
||||
}
|
||||
|
||||
buffer.clear();
|
||||
return skip - toSkip;
|
||||
}
|
||||
|
||||
private static Class<?> lookupClassQuietly(String name) {
|
||||
try {
|
||||
return SnappyFramed.class.getClassLoader().loadClass(name);
|
||||
} catch (Throwable t) {
|
||||
Logger.getLogger(SnappyFramed.class.getName()).log(Level.FINE, "Did not find requested class: " + name, t);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides jvm implementation specific operation to aggressively release resources associated with <i>buffer</i>.
|
||||
* @param buffer The {@code ByteBuffer} to release. Must not be {@code null}. Must be {@link ByteBuffer#isDirect() direct}.
|
||||
*/
|
||||
static void releaseDirectByteBuffer(ByteBuffer buffer)
|
||||
{
|
||||
assert buffer != null && buffer.isDirect();
|
||||
|
||||
if (SUN_DIRECT_BUFFER != null && SUN_DIRECT_BUFFER.isAssignableFrom(buffer.getClass())) {
|
||||
try {
|
||||
Object cleaner = SUN_BUFFER_CLEANER.invoke(buffer, (Object[]) null);
|
||||
SUN_CLEANER_CLEAN.invoke(cleaner, (Object[]) null);
|
||||
} catch (Throwable t) {
|
||||
Logger.getLogger(SnappyFramed.class.getName()).log(Level.FINE, "Exception occurred attempting to clean up Sun specific DirectByteBuffer.", t);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,309 +1,309 @@
|
|||
/*--------------------------------------------------------------------------
|
||||
* 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.
|
||||
*--------------------------------------------------------------------------*/
|
||||
#include <string>
|
||||
#include <cstring>
|
||||
#include <snappy.h>
|
||||
#include "SnappyNative.h"
|
||||
|
||||
void throw_exception(JNIEnv *env, jobject self, int errorCode)
|
||||
{
|
||||
jclass c = env->FindClass("org/xerial/snappy/SnappyNative");
|
||||
if(c==0)
|
||||
return;
|
||||
jmethodID mth_throwex = env->GetMethodID(c, "throw_error", "(I)V");
|
||||
if(mth_throwex == 0)
|
||||
return;
|
||||
env->CallVoidMethod(self, mth_throwex, (jint) errorCode);
|
||||
}
|
||||
|
||||
|
||||
JNIEXPORT jstring JNICALL Java_org_xerial_snappy_SnappyNative_nativeLibraryVersion
|
||||
(JNIEnv * env, jobject self)
|
||||
{
|
||||
return env->NewStringUTF("1.1.0");
|
||||
}
|
||||
|
||||
JNIEXPORT jlong JNICALL Java_org_xerial_snappy_SnappyNative_rawCompress__JJJ
|
||||
(JNIEnv* env, jobject self, jlong srcAddr, jlong length, jlong destAddr) {
|
||||
size_t compressedLength;
|
||||
snappy::RawCompress((char*) srcAddr, (size_t) length, (char*) destAddr, &compressedLength);
|
||||
return (jlong) compressedLength;
|
||||
}
|
||||
|
||||
|
||||
|
||||
JNIEXPORT jlong JNICALL Java_org_xerial_snappy_SnappyNative_rawUncompress__JJJ
|
||||
(JNIEnv* env, jobject self, jlong srcAddr, jlong length, jlong destAddr) {
|
||||
|
||||
size_t uncompressedLength;
|
||||
snappy::GetUncompressedLength((char*) srcAddr, (size_t) length, &uncompressedLength);
|
||||
bool ret = snappy::RawUncompress((char*) srcAddr, (size_t) length, (char*) destAddr);
|
||||
|
||||
if(!ret) {
|
||||
throw_exception(env, self, 5);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return (jlong) uncompressedLength;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* Class: org_xerial_snappy_Snappy
|
||||
* Method: compress
|
||||
* Signature: (Ljava/nio/ByteBuffer;Ljava/nio/ByteBuffer;)J
|
||||
*/
|
||||
JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_rawCompress__Ljava_nio_ByteBuffer_2IILjava_nio_ByteBuffer_2I
|
||||
(JNIEnv* env, jobject self, jobject uncompressed, jint upos, jint ulen, jobject compressed, jint cpos)
|
||||
{
|
||||
char* uncompressedBuffer = (char*) env->GetDirectBufferAddress(uncompressed);
|
||||
char* compressedBuffer = (char*) env->GetDirectBufferAddress(compressed);
|
||||
if(uncompressedBuffer == 0 || compressedBuffer == 0) {
|
||||
throw_exception(env, self, 3);
|
||||
return (jint) 0;
|
||||
}
|
||||
|
||||
size_t compressedLength;
|
||||
snappy::RawCompress(uncompressedBuffer + upos, (size_t) ulen, compressedBuffer + cpos, &compressedLength);
|
||||
return (jint) compressedLength;
|
||||
}
|
||||
|
||||
|
||||
JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_rawCompress__Ljava_lang_Object_2IILjava_lang_Object_2I
|
||||
(JNIEnv * env, jobject self, jobject input, jint inputOffset, jint inputLen, jobject output, jint outputOffset)
|
||||
{
|
||||
char* in = (char*) env->GetPrimitiveArrayCritical((jarray) input, 0);
|
||||
char* out = (char*) env->GetPrimitiveArrayCritical((jarray) output, 0);
|
||||
if(in == 0 || out == 0) {
|
||||
// out of memory
|
||||
if(in != 0) {
|
||||
env->ReleasePrimitiveArrayCritical((jarray) input, in, 0);
|
||||
}
|
||||
if(out != 0) {
|
||||
env->ReleasePrimitiveArrayCritical((jarray) output, out, 0);
|
||||
}
|
||||
throw_exception(env, self, 4);
|
||||
return 0;
|
||||
}
|
||||
|
||||
size_t compressedLength;
|
||||
snappy::RawCompress(in + inputOffset, (size_t) inputLen, out + outputOffset, &compressedLength);
|
||||
|
||||
env->ReleasePrimitiveArrayCritical((jarray) input, in, 0);
|
||||
env->ReleasePrimitiveArrayCritical((jarray) output, out, 0);
|
||||
|
||||
return (jint) compressedLength;
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_rawUncompress__Ljava_lang_Object_2IILjava_lang_Object_2I
|
||||
(JNIEnv * env, jobject self, jobject input, jint inputOffset, jint inputLength, jobject output, jint outputOffset)
|
||||
{
|
||||
char* in = (char*) env->GetPrimitiveArrayCritical((jarray) input, 0);
|
||||
char* out = (char*) env->GetPrimitiveArrayCritical((jarray) output, 0);
|
||||
if(in == 0 || out == 0) {
|
||||
// out of memory
|
||||
if(in != 0) {
|
||||
env->ReleasePrimitiveArrayCritical((jarray) input, in, 0);
|
||||
}
|
||||
if(out != 0) {
|
||||
env->ReleasePrimitiveArrayCritical((jarray) output, out, 0);
|
||||
}
|
||||
throw_exception(env, self, 4);
|
||||
return 0;
|
||||
}
|
||||
|
||||
size_t uncompressedLength;
|
||||
snappy::GetUncompressedLength(in + inputOffset, (size_t) inputLength, &uncompressedLength);
|
||||
bool ret = snappy::RawUncompress(in + inputOffset, (size_t) inputLength, out + outputOffset);
|
||||
|
||||
env->ReleasePrimitiveArrayCritical((jarray) input, in, 0);
|
||||
env->ReleasePrimitiveArrayCritical((jarray) output, out, 0);
|
||||
|
||||
if(!ret) {
|
||||
throw_exception(env, self, 5);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return (jint) uncompressedLength;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Class: org_xerial_snappy_Snappy
|
||||
* Method: uncompress
|
||||
* Signature: (Ljava/nio/ByteBuffer;Ljava/nio/ByteBuffer;)Z
|
||||
*/
|
||||
JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_rawUncompress__Ljava_nio_ByteBuffer_2IILjava_nio_ByteBuffer_2I
|
||||
(JNIEnv * env, jobject self, jobject compressed, jint cpos, jint clen, jobject decompressed, jint dpos)
|
||||
{
|
||||
char* compressedBuffer = (char*) env->GetDirectBufferAddress(compressed);
|
||||
char* decompressedBuffer = (char*) env->GetDirectBufferAddress(decompressed);
|
||||
if(compressedBuffer == 0 || decompressedBuffer == 0) {
|
||||
throw_exception(env, self, 3);
|
||||
return (jint) 0;
|
||||
}
|
||||
|
||||
size_t decompressedLength;
|
||||
snappy::GetUncompressedLength(compressedBuffer + cpos, (size_t) clen, &decompressedLength);
|
||||
bool ret = snappy::RawUncompress(compressedBuffer + cpos, (size_t) clen, decompressedBuffer + dpos);
|
||||
if(!ret) {
|
||||
throw_exception(env, self, 5);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return (jint) decompressedLength;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* Class: org_xerial_snappy_Snappy
|
||||
* Method: maxCompressedLength
|
||||
* Signature: (J)J
|
||||
*/
|
||||
|
||||
JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_maxCompressedLength
|
||||
(JNIEnv *, jobject, jint size)
|
||||
{
|
||||
size_t l = snappy::MaxCompressedLength((size_t) size);
|
||||
return (jint) l;
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: org_xerial_snappy_Snappy
|
||||
* Method: getUncompressedLength
|
||||
* Signature: (Ljava/nio/ByteBuffer;)J
|
||||
*/
|
||||
JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_uncompressedLength__Ljava_nio_ByteBuffer_2II
|
||||
(JNIEnv * env, jobject self, jobject compressed, jint cpos, jint clen)
|
||||
{
|
||||
char* compressedBuffer = (char*) env->GetDirectBufferAddress(compressed);
|
||||
if(compressedBuffer == 0) {
|
||||
throw_exception(env, self, 3);
|
||||
return (jint) 0;
|
||||
}
|
||||
|
||||
size_t result;
|
||||
bool ret = snappy::GetUncompressedLength(compressedBuffer + cpos, (size_t) clen, &result);
|
||||
if(!ret) {
|
||||
throw_exception(env, self, 2);
|
||||
return 0;
|
||||
}
|
||||
return (jint) result;
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_uncompressedLength__Ljava_lang_Object_2II
|
||||
(JNIEnv * env, jobject self, jobject input, jint offset, jint length)
|
||||
{
|
||||
char* in = (char*) env->GetPrimitiveArrayCritical((jarray) input, 0);
|
||||
if(in == 0) {
|
||||
// out of memory
|
||||
throw_exception(env, self, 4);
|
||||
return 0;
|
||||
}
|
||||
|
||||
size_t result;
|
||||
bool ret = snappy::GetUncompressedLength(in + offset, (size_t) length, &result);
|
||||
env->ReleasePrimitiveArrayCritical((jarray) input, in, 0);
|
||||
|
||||
if(!ret) {
|
||||
throw_exception(env, self, 2);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return (jint) result;
|
||||
}
|
||||
|
||||
JNIEXPORT jlong JNICALL Java_org_xerial_snappy_SnappyNative_uncompressedLength__JJ
|
||||
(JNIEnv *env, jobject self, jlong inputAddr, jlong len) {
|
||||
|
||||
|
||||
size_t result;
|
||||
bool ret = snappy::GetUncompressedLength((char*) inputAddr, (size_t) len, &result);
|
||||
if(!ret) {
|
||||
throw_exception(env, self, 2);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return (jint) result;
|
||||
}
|
||||
|
||||
|
||||
JNIEXPORT jboolean JNICALL Java_org_xerial_snappy_SnappyNative_isValidCompressedBuffer__Ljava_nio_ByteBuffer_2II
|
||||
(JNIEnv * env, jobject self, jobject compressed, jint cpos, jint clen)
|
||||
{
|
||||
char* compressedBuffer = (char*) env->GetDirectBufferAddress(compressed);
|
||||
if(compressedBuffer == 0) {
|
||||
throw_exception(env, self, 3);
|
||||
return (jint) 0;
|
||||
}
|
||||
bool ret = snappy::IsValidCompressedBuffer(compressedBuffer + cpos, (size_t) clen);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
JNIEXPORT jboolean JNICALL Java_org_xerial_snappy_SnappyNative_isValidCompressedBuffer__Ljava_lang_Object_2II
|
||||
(JNIEnv * env, jobject self, jobject input, jint offset, jint length)
|
||||
{
|
||||
char* in = (char*) env->GetPrimitiveArrayCritical((jarray) input, 0);
|
||||
if(in == 0) {
|
||||
// out of memory
|
||||
throw_exception(env, self, 4);
|
||||
return 0;
|
||||
}
|
||||
bool ret = snappy::IsValidCompressedBuffer(in + offset, (size_t) length);
|
||||
env->ReleasePrimitiveArrayCritical((jarray) input, in, 0);
|
||||
return ret;
|
||||
}
|
||||
|
||||
JNIEXPORT jboolean JNICALL Java_org_xerial_snappy_SnappyNative_isValidCompressedBuffer__JJJ
|
||||
(JNIEnv * env, jobject self, jlong inputAddr, jlong offset, jlong length)
|
||||
{
|
||||
if(inputAddr == 0) {
|
||||
// out of memory
|
||||
throw_exception(env, self, 4);
|
||||
return 0;
|
||||
}
|
||||
bool ret = snappy::IsValidCompressedBuffer((char*) (inputAddr + offset), (size_t) length);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
JNIEXPORT void JNICALL Java_org_xerial_snappy_SnappyNative_arrayCopy
|
||||
(JNIEnv * env, jobject self, jobject input, jint offset, jint length, jobject output, jint output_offset)
|
||||
{
|
||||
char* src = (char*) env->GetPrimitiveArrayCritical((jarray) input, 0);
|
||||
char* dest = (char*) env->GetPrimitiveArrayCritical((jarray) output, 0);
|
||||
if(src == 0 || dest == 0) {
|
||||
// out of memory
|
||||
if(src != 0) {
|
||||
env->ReleasePrimitiveArrayCritical((jarray) input, src, 0);
|
||||
}
|
||||
if(dest != 0) {
|
||||
env->ReleasePrimitiveArrayCritical((jarray) output, dest, 0);
|
||||
}
|
||||
throw_exception(env, self, 4);
|
||||
return;
|
||||
}
|
||||
|
||||
memcpy(dest+output_offset, src+offset, (size_t) length);
|
||||
|
||||
env->ReleasePrimitiveArrayCritical((jarray) input, src, 0);
|
||||
env->ReleasePrimitiveArrayCritical((jarray) output, dest, 0);
|
||||
}
|
||||
|
||||
|
||||
/*--------------------------------------------------------------------------
|
||||
* 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.
|
||||
*--------------------------------------------------------------------------*/
|
||||
#include <string>
|
||||
#include <cstring>
|
||||
#include <snappy.h>
|
||||
#include "SnappyNative.h"
|
||||
|
||||
void throw_exception(JNIEnv *env, jobject self, int errorCode)
|
||||
{
|
||||
jclass c = env->FindClass("org/xerial/snappy/SnappyNative");
|
||||
if(c==0)
|
||||
return;
|
||||
jmethodID mth_throwex = env->GetMethodID(c, "throw_error", "(I)V");
|
||||
if(mth_throwex == 0)
|
||||
return;
|
||||
env->CallVoidMethod(self, mth_throwex, (jint) errorCode);
|
||||
}
|
||||
|
||||
|
||||
JNIEXPORT jstring JNICALL Java_org_xerial_snappy_SnappyNative_nativeLibraryVersion
|
||||
(JNIEnv * env, jobject self)
|
||||
{
|
||||
return env->NewStringUTF("1.1.0");
|
||||
}
|
||||
|
||||
JNIEXPORT jlong JNICALL Java_org_xerial_snappy_SnappyNative_rawCompress__JJJ
|
||||
(JNIEnv* env, jobject self, jlong srcAddr, jlong length, jlong destAddr) {
|
||||
size_t compressedLength;
|
||||
snappy::RawCompress((char*) srcAddr, (size_t) length, (char*) destAddr, &compressedLength);
|
||||
return (jlong) compressedLength;
|
||||
}
|
||||
|
||||
|
||||
|
||||
JNIEXPORT jlong JNICALL Java_org_xerial_snappy_SnappyNative_rawUncompress__JJJ
|
||||
(JNIEnv* env, jobject self, jlong srcAddr, jlong length, jlong destAddr) {
|
||||
|
||||
size_t uncompressedLength;
|
||||
snappy::GetUncompressedLength((char*) srcAddr, (size_t) length, &uncompressedLength);
|
||||
bool ret = snappy::RawUncompress((char*) srcAddr, (size_t) length, (char*) destAddr);
|
||||
|
||||
if(!ret) {
|
||||
throw_exception(env, self, 5);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return (jlong) uncompressedLength;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* Class: org_xerial_snappy_Snappy
|
||||
* Method: compress
|
||||
* Signature: (Ljava/nio/ByteBuffer;Ljava/nio/ByteBuffer;)J
|
||||
*/
|
||||
JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_rawCompress__Ljava_nio_ByteBuffer_2IILjava_nio_ByteBuffer_2I
|
||||
(JNIEnv* env, jobject self, jobject uncompressed, jint upos, jint ulen, jobject compressed, jint cpos)
|
||||
{
|
||||
char* uncompressedBuffer = (char*) env->GetDirectBufferAddress(uncompressed);
|
||||
char* compressedBuffer = (char*) env->GetDirectBufferAddress(compressed);
|
||||
if(uncompressedBuffer == 0 || compressedBuffer == 0) {
|
||||
throw_exception(env, self, 3);
|
||||
return (jint) 0;
|
||||
}
|
||||
|
||||
size_t compressedLength;
|
||||
snappy::RawCompress(uncompressedBuffer + upos, (size_t) ulen, compressedBuffer + cpos, &compressedLength);
|
||||
return (jint) compressedLength;
|
||||
}
|
||||
|
||||
|
||||
JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_rawCompress__Ljava_lang_Object_2IILjava_lang_Object_2I
|
||||
(JNIEnv * env, jobject self, jobject input, jint inputOffset, jint inputLen, jobject output, jint outputOffset)
|
||||
{
|
||||
char* in = (char*) env->GetPrimitiveArrayCritical((jarray) input, 0);
|
||||
char* out = (char*) env->GetPrimitiveArrayCritical((jarray) output, 0);
|
||||
if(in == 0 || out == 0) {
|
||||
// out of memory
|
||||
if(in != 0) {
|
||||
env->ReleasePrimitiveArrayCritical((jarray) input, in, 0);
|
||||
}
|
||||
if(out != 0) {
|
||||
env->ReleasePrimitiveArrayCritical((jarray) output, out, 0);
|
||||
}
|
||||
throw_exception(env, self, 4);
|
||||
return 0;
|
||||
}
|
||||
|
||||
size_t compressedLength;
|
||||
snappy::RawCompress(in + inputOffset, (size_t) inputLen, out + outputOffset, &compressedLength);
|
||||
|
||||
env->ReleasePrimitiveArrayCritical((jarray) input, in, 0);
|
||||
env->ReleasePrimitiveArrayCritical((jarray) output, out, 0);
|
||||
|
||||
return (jint) compressedLength;
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_rawUncompress__Ljava_lang_Object_2IILjava_lang_Object_2I
|
||||
(JNIEnv * env, jobject self, jobject input, jint inputOffset, jint inputLength, jobject output, jint outputOffset)
|
||||
{
|
||||
char* in = (char*) env->GetPrimitiveArrayCritical((jarray) input, 0);
|
||||
char* out = (char*) env->GetPrimitiveArrayCritical((jarray) output, 0);
|
||||
if(in == 0 || out == 0) {
|
||||
// out of memory
|
||||
if(in != 0) {
|
||||
env->ReleasePrimitiveArrayCritical((jarray) input, in, 0);
|
||||
}
|
||||
if(out != 0) {
|
||||
env->ReleasePrimitiveArrayCritical((jarray) output, out, 0);
|
||||
}
|
||||
throw_exception(env, self, 4);
|
||||
return 0;
|
||||
}
|
||||
|
||||
size_t uncompressedLength;
|
||||
snappy::GetUncompressedLength(in + inputOffset, (size_t) inputLength, &uncompressedLength);
|
||||
bool ret = snappy::RawUncompress(in + inputOffset, (size_t) inputLength, out + outputOffset);
|
||||
|
||||
env->ReleasePrimitiveArrayCritical((jarray) input, in, 0);
|
||||
env->ReleasePrimitiveArrayCritical((jarray) output, out, 0);
|
||||
|
||||
if(!ret) {
|
||||
throw_exception(env, self, 5);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return (jint) uncompressedLength;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Class: org_xerial_snappy_Snappy
|
||||
* Method: uncompress
|
||||
* Signature: (Ljava/nio/ByteBuffer;Ljava/nio/ByteBuffer;)Z
|
||||
*/
|
||||
JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_rawUncompress__Ljava_nio_ByteBuffer_2IILjava_nio_ByteBuffer_2I
|
||||
(JNIEnv * env, jobject self, jobject compressed, jint cpos, jint clen, jobject decompressed, jint dpos)
|
||||
{
|
||||
char* compressedBuffer = (char*) env->GetDirectBufferAddress(compressed);
|
||||
char* decompressedBuffer = (char*) env->GetDirectBufferAddress(decompressed);
|
||||
if(compressedBuffer == 0 || decompressedBuffer == 0) {
|
||||
throw_exception(env, self, 3);
|
||||
return (jint) 0;
|
||||
}
|
||||
|
||||
size_t decompressedLength;
|
||||
snappy::GetUncompressedLength(compressedBuffer + cpos, (size_t) clen, &decompressedLength);
|
||||
bool ret = snappy::RawUncompress(compressedBuffer + cpos, (size_t) clen, decompressedBuffer + dpos);
|
||||
if(!ret) {
|
||||
throw_exception(env, self, 5);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return (jint) decompressedLength;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* Class: org_xerial_snappy_Snappy
|
||||
* Method: maxCompressedLength
|
||||
* Signature: (J)J
|
||||
*/
|
||||
|
||||
JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_maxCompressedLength
|
||||
(JNIEnv *, jobject, jint size)
|
||||
{
|
||||
size_t l = snappy::MaxCompressedLength((size_t) size);
|
||||
return (jint) l;
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: org_xerial_snappy_Snappy
|
||||
* Method: getUncompressedLength
|
||||
* Signature: (Ljava/nio/ByteBuffer;)J
|
||||
*/
|
||||
JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_uncompressedLength__Ljava_nio_ByteBuffer_2II
|
||||
(JNIEnv * env, jobject self, jobject compressed, jint cpos, jint clen)
|
||||
{
|
||||
char* compressedBuffer = (char*) env->GetDirectBufferAddress(compressed);
|
||||
if(compressedBuffer == 0) {
|
||||
throw_exception(env, self, 3);
|
||||
return (jint) 0;
|
||||
}
|
||||
|
||||
size_t result;
|
||||
bool ret = snappy::GetUncompressedLength(compressedBuffer + cpos, (size_t) clen, &result);
|
||||
if(!ret) {
|
||||
throw_exception(env, self, 2);
|
||||
return 0;
|
||||
}
|
||||
return (jint) result;
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_uncompressedLength__Ljava_lang_Object_2II
|
||||
(JNIEnv * env, jobject self, jobject input, jint offset, jint length)
|
||||
{
|
||||
char* in = (char*) env->GetPrimitiveArrayCritical((jarray) input, 0);
|
||||
if(in == 0) {
|
||||
// out of memory
|
||||
throw_exception(env, self, 4);
|
||||
return 0;
|
||||
}
|
||||
|
||||
size_t result;
|
||||
bool ret = snappy::GetUncompressedLength(in + offset, (size_t) length, &result);
|
||||
env->ReleasePrimitiveArrayCritical((jarray) input, in, 0);
|
||||
|
||||
if(!ret) {
|
||||
throw_exception(env, self, 2);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return (jint) result;
|
||||
}
|
||||
|
||||
JNIEXPORT jlong JNICALL Java_org_xerial_snappy_SnappyNative_uncompressedLength__JJ
|
||||
(JNIEnv *env, jobject self, jlong inputAddr, jlong len) {
|
||||
|
||||
|
||||
size_t result;
|
||||
bool ret = snappy::GetUncompressedLength((char*) inputAddr, (size_t) len, &result);
|
||||
if(!ret) {
|
||||
throw_exception(env, self, 2);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return (jint) result;
|
||||
}
|
||||
|
||||
|
||||
JNIEXPORT jboolean JNICALL Java_org_xerial_snappy_SnappyNative_isValidCompressedBuffer__Ljava_nio_ByteBuffer_2II
|
||||
(JNIEnv * env, jobject self, jobject compressed, jint cpos, jint clen)
|
||||
{
|
||||
char* compressedBuffer = (char*) env->GetDirectBufferAddress(compressed);
|
||||
if(compressedBuffer == 0) {
|
||||
throw_exception(env, self, 3);
|
||||
return (jint) 0;
|
||||
}
|
||||
bool ret = snappy::IsValidCompressedBuffer(compressedBuffer + cpos, (size_t) clen);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
JNIEXPORT jboolean JNICALL Java_org_xerial_snappy_SnappyNative_isValidCompressedBuffer__Ljava_lang_Object_2II
|
||||
(JNIEnv * env, jobject self, jobject input, jint offset, jint length)
|
||||
{
|
||||
char* in = (char*) env->GetPrimitiveArrayCritical((jarray) input, 0);
|
||||
if(in == 0) {
|
||||
// out of memory
|
||||
throw_exception(env, self, 4);
|
||||
return 0;
|
||||
}
|
||||
bool ret = snappy::IsValidCompressedBuffer(in + offset, (size_t) length);
|
||||
env->ReleasePrimitiveArrayCritical((jarray) input, in, 0);
|
||||
return ret;
|
||||
}
|
||||
|
||||
JNIEXPORT jboolean JNICALL Java_org_xerial_snappy_SnappyNative_isValidCompressedBuffer__JJJ
|
||||
(JNIEnv * env, jobject self, jlong inputAddr, jlong offset, jlong length)
|
||||
{
|
||||
if(inputAddr == 0) {
|
||||
// out of memory
|
||||
throw_exception(env, self, 4);
|
||||
return 0;
|
||||
}
|
||||
bool ret = snappy::IsValidCompressedBuffer((char*) (inputAddr + offset), (size_t) length);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
JNIEXPORT void JNICALL Java_org_xerial_snappy_SnappyNative_arrayCopy
|
||||
(JNIEnv * env, jobject self, jobject input, jint offset, jint length, jobject output, jint output_offset)
|
||||
{
|
||||
char* src = (char*) env->GetPrimitiveArrayCritical((jarray) input, 0);
|
||||
char* dest = (char*) env->GetPrimitiveArrayCritical((jarray) output, 0);
|
||||
if(src == 0 || dest == 0) {
|
||||
// out of memory
|
||||
if(src != 0) {
|
||||
env->ReleasePrimitiveArrayCritical((jarray) input, src, 0);
|
||||
}
|
||||
if(dest != 0) {
|
||||
env->ReleasePrimitiveArrayCritical((jarray) output, dest, 0);
|
||||
}
|
||||
throw_exception(env, self, 4);
|
||||
return;
|
||||
}
|
||||
|
||||
memcpy(dest+output_offset, src+offset, (size_t) length);
|
||||
|
||||
env->ReleasePrimitiveArrayCritical((jarray) input, src, 0);
|
||||
env->ReleasePrimitiveArrayCritical((jarray) output, dest, 0);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -1,87 +1,87 @@
|
|||
/*--------------------------------------------------------------------------
|
||||
* 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.
|
||||
*--------------------------------------------------------------------------*/
|
||||
//--------------------------------------
|
||||
// snappy-java Project
|
||||
//
|
||||
// SnappyNative.java
|
||||
// Since: 2011/03/30
|
||||
//
|
||||
// $URL$
|
||||
// $Author$
|
||||
//--------------------------------------
|
||||
package org.xerial.snappy;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
/**
|
||||
* JNI interface of the {@link Snappy} implementation. The native method in this class is
|
||||
* defined in SnappyNative.h (genereted by javah) and SnappyNative.cpp
|
||||
*
|
||||
* <p>
|
||||
* <b> DO NOT USE THIS CLASS since the direct use of this class might break the
|
||||
* native library code loading in {@link SnappyLoader}. </b>
|
||||
* </p>
|
||||
*
|
||||
* @author leo
|
||||
*
|
||||
*/
|
||||
public class SnappyNative
|
||||
{
|
||||
|
||||
public native String nativeLibraryVersion();
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
// Generic compression/decompression routines.
|
||||
// ------------------------------------------------------------------------
|
||||
public native long rawCompress(long inputAddr, long inputSize, long destAddr) throws IOException;
|
||||
public native long rawUncompress(long inputAddr, long inputSize, long destAddr) throws IOException;
|
||||
|
||||
public native int rawCompress(ByteBuffer input, int inputOffset, int inputLength, ByteBuffer compressed,
|
||||
int outputOffset) throws IOException;
|
||||
|
||||
public native int rawCompress(Object input, int inputOffset, int inputByteLength, Object output, int outputOffset) throws IOException;
|
||||
|
||||
public native int rawUncompress(ByteBuffer compressed, int inputOffset, int inputLength, ByteBuffer uncompressed,
|
||||
int outputOffset) throws IOException;
|
||||
|
||||
public native int rawUncompress(Object input, int inputOffset, int inputLength, Object output, int outputOffset)
|
||||
throws IOException;
|
||||
|
||||
// Returns the maximal size of the compressed representation of
|
||||
// input data that is "source_bytes" bytes in length;
|
||||
public native int maxCompressedLength(int source_bytes);
|
||||
|
||||
// This operation takes O(1) time.
|
||||
public native int uncompressedLength(ByteBuffer compressed, int offset, int len) throws IOException;
|
||||
|
||||
public native int uncompressedLength(Object input, int offset, int len) throws IOException;
|
||||
|
||||
public native long uncompressedLength(long inputAddr, long len) throws IOException;
|
||||
|
||||
public native boolean isValidCompressedBuffer(ByteBuffer compressed, int offset, int len) throws IOException;
|
||||
|
||||
public native boolean isValidCompressedBuffer(Object input, int offset, int len) throws IOException;
|
||||
|
||||
public native boolean isValidCompressedBuffer(long inputAddr, long offset, long len) throws IOException;
|
||||
|
||||
public native void arrayCopy(Object src, int offset, int byteLength, Object dest, int dOffset) throws IOException;
|
||||
|
||||
public void throw_error(int errorCode) throws IOException {
|
||||
throw new IOException(String.format("%s(%d)", SnappyErrorCode.getErrorMessage(errorCode), errorCode));
|
||||
}
|
||||
|
||||
}
|
||||
/*--------------------------------------------------------------------------
|
||||
* 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.
|
||||
*--------------------------------------------------------------------------*/
|
||||
//--------------------------------------
|
||||
// snappy-java Project
|
||||
//
|
||||
// SnappyNative.java
|
||||
// Since: 2011/03/30
|
||||
//
|
||||
// $URL$
|
||||
// $Author$
|
||||
//--------------------------------------
|
||||
package org.xerial.snappy;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
/**
|
||||
* JNI interface of the {@link Snappy} implementation. The native method in this class is
|
||||
* defined in SnappyNative.h (genereted by javah) and SnappyNative.cpp
|
||||
*
|
||||
* <p>
|
||||
* <b> DO NOT USE THIS CLASS since the direct use of this class might break the
|
||||
* native library code loading in {@link SnappyLoader}. </b>
|
||||
* </p>
|
||||
*
|
||||
* @author leo
|
||||
*
|
||||
*/
|
||||
public class SnappyNative
|
||||
{
|
||||
|
||||
public native String nativeLibraryVersion();
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
// Generic compression/decompression routines.
|
||||
// ------------------------------------------------------------------------
|
||||
public native long rawCompress(long inputAddr, long inputSize, long destAddr) throws IOException;
|
||||
public native long rawUncompress(long inputAddr, long inputSize, long destAddr) throws IOException;
|
||||
|
||||
public native int rawCompress(ByteBuffer input, int inputOffset, int inputLength, ByteBuffer compressed,
|
||||
int outputOffset) throws IOException;
|
||||
|
||||
public native int rawCompress(Object input, int inputOffset, int inputByteLength, Object output, int outputOffset) throws IOException;
|
||||
|
||||
public native int rawUncompress(ByteBuffer compressed, int inputOffset, int inputLength, ByteBuffer uncompressed,
|
||||
int outputOffset) throws IOException;
|
||||
|
||||
public native int rawUncompress(Object input, int inputOffset, int inputLength, Object output, int outputOffset)
|
||||
throws IOException;
|
||||
|
||||
// Returns the maximal size of the compressed representation of
|
||||
// input data that is "source_bytes" bytes in length;
|
||||
public native int maxCompressedLength(int source_bytes);
|
||||
|
||||
// This operation takes O(1) time.
|
||||
public native int uncompressedLength(ByteBuffer compressed, int offset, int len) throws IOException;
|
||||
|
||||
public native int uncompressedLength(Object input, int offset, int len) throws IOException;
|
||||
|
||||
public native long uncompressedLength(long inputAddr, long len) throws IOException;
|
||||
|
||||
public native boolean isValidCompressedBuffer(ByteBuffer compressed, int offset, int len) throws IOException;
|
||||
|
||||
public native boolean isValidCompressedBuffer(Object input, int offset, int len) throws IOException;
|
||||
|
||||
public native boolean isValidCompressedBuffer(long inputAddr, long offset, long len) throws IOException;
|
||||
|
||||
public native void arrayCopy(Object src, int offset, int byteLength, Object dest, int dOffset) throws IOException;
|
||||
|
||||
public void throw_error(int errorCode) throws IOException {
|
||||
throw new IOException(String.format("%s(%d)", SnappyErrorCode.getErrorMessage(errorCode), errorCode));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,44 +1,44 @@
|
|||
/*--------------------------------------------------------------------------
|
||||
* 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.
|
||||
*--------------------------------------------------------------------------*/
|
||||
|
||||
/**
|
||||
* Snappy API for compressing/decompressing data.
|
||||
*
|
||||
* Usage
|
||||
* First, import {@link org.xerial.snappy.Snappy} in your Java code:
|
||||
* <code>
|
||||
* <pre>
|
||||
* import org.xerial.snappy.Snappy;
|
||||
* </pre>
|
||||
* </code>
|
||||
* Then use {@link org.xerial.snappy.Snappy#compress(byte[])} and {@link org.xerial.snappy.Snappy#uncompress(byte[])}:
|
||||
* <code>
|
||||
* <pre>
|
||||
* String input = "Hello snappy-java! Snappy-java is a JNI-based wrapper of Snappy, a fast compresser/decompresser.";
|
||||
* byte[] compressed = Snappy.compress(input.getBytes("UTF-8"));
|
||||
* byte[] uncompressed = Snappy.uncompress(compressed);
|
||||
* String result = new String(uncompressed, "UTF-8");
|
||||
* System.out.println(result);
|
||||
* </pre>
|
||||
* </code>
|
||||
*
|
||||
* <p>In addition, high-level methods (Snappy.compress(String), Snappy.compress(float[] ..) etc. ) and low-level ones (e.g. Snappy.rawCompress(.. ), Snappy.rawUncompress(..), etc.), which minimize memory copies, can be used. </p>
|
||||
*
|
||||
* <h3>Stream-based API</h3>
|
||||
* Stream-based compressor/decompressor SnappyOutputStream, SnappyInputStream are also available for reading/writing large data sets.
|
||||
*/
|
||||
package org.xerial.snappy;
|
||||
|
||||
/*--------------------------------------------------------------------------
|
||||
* 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.
|
||||
*--------------------------------------------------------------------------*/
|
||||
|
||||
/**
|
||||
* Snappy API for compressing/decompressing data.
|
||||
*
|
||||
* Usage
|
||||
* First, import {@link org.xerial.snappy.Snappy} in your Java code:
|
||||
* <code>
|
||||
* <pre>
|
||||
* import org.xerial.snappy.Snappy;
|
||||
* </pre>
|
||||
* </code>
|
||||
* Then use {@link org.xerial.snappy.Snappy#compress(byte[])} and {@link org.xerial.snappy.Snappy#uncompress(byte[])}:
|
||||
* <code>
|
||||
* <pre>
|
||||
* String input = "Hello snappy-java! Snappy-java is a JNI-based wrapper of Snappy, a fast compresser/decompresser.";
|
||||
* byte[] compressed = Snappy.compress(input.getBytes("UTF-8"));
|
||||
* byte[] uncompressed = Snappy.uncompress(compressed);
|
||||
* String result = new String(uncompressed, "UTF-8");
|
||||
* System.out.println(result);
|
||||
* </pre>
|
||||
* </code>
|
||||
*
|
||||
* <p>In addition, high-level methods (Snappy.compress(String), Snappy.compress(float[] ..) etc. ) and low-level ones (e.g. Snappy.rawCompress(.. ), Snappy.rawUncompress(..), etc.), which minimize memory copies, can be used. </p>
|
||||
*
|
||||
* <h3>Stream-based API</h3>
|
||||
* Stream-based compressor/decompressor SnappyOutputStream, SnappyInputStream are also available for reading/writing large data sets.
|
||||
*/
|
||||
package org.xerial.snappy;
|
||||
|
||||
|
|
|
@ -1,228 +1,228 @@
|
|||
/*--------------------------------------------------------------------------
|
||||
* 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
|
||||
//
|
||||
// CalgaryTest.java
|
||||
// Since: 2011/04/04 12:10:36
|
||||
//
|
||||
// $URL$
|
||||
// $Author$
|
||||
//--------------------------------------
|
||||
package org.xerial.snappy;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
import org.xerial.util.FileResource;
|
||||
import org.xerial.util.log.Logger;
|
||||
|
||||
/**
|
||||
* Benchmark using Calgary data set
|
||||
*
|
||||
* @author leo
|
||||
*
|
||||
*/
|
||||
public class CalgaryTest
|
||||
{
|
||||
private static Logger _logger = Logger.getLogger(CalgaryTest.class);
|
||||
|
||||
@Rule
|
||||
public final TemporaryFolder tempFolder = new TemporaryFolder();
|
||||
|
||||
static byte[] readFile(String file) throws IOException {
|
||||
InputStream in = FileResource.find(CalgaryTest.class, file).openStream();
|
||||
if (in == null)
|
||||
throw new IOException("file " + file + " is not found");
|
||||
try {
|
||||
return SnappyInputStreamTest.readFully(in);
|
||||
}
|
||||
finally {
|
||||
if (in != null)
|
||||
in.close();
|
||||
}
|
||||
}
|
||||
|
||||
public static final String[] files = { "bib", "book1", "book2", "geo", "news", "obj1", "obj2", "paper1", "paper2",
|
||||
"paper3", "paper4", "paper5", "paper6", "pic", "progc", "progl", "progp", "trans" };
|
||||
|
||||
@Test
|
||||
public void block() throws Exception {
|
||||
for (String f : files) {
|
||||
byte[] orig = readFile("testdata/calgary/" + f);
|
||||
|
||||
byte[] compressed = Snappy.compress(orig);
|
||||
byte[] uncompressed = Snappy.uncompress(compressed);
|
||||
|
||||
assertArrayEquals(orig, uncompressed);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void stream() throws Exception {
|
||||
for (String f : files) {
|
||||
byte[] orig = readFile("testdata/calgary/" + f);
|
||||
|
||||
ByteArrayOutputStream compressedBuf = new ByteArrayOutputStream();
|
||||
SnappyOutputStream out = new SnappyOutputStream(compressedBuf);
|
||||
out.write(orig);
|
||||
out.close();
|
||||
|
||||
SnappyInputStream in = new SnappyInputStream(new ByteArrayInputStream(compressedBuf.toByteArray()));
|
||||
byte[] uncompressed = new byte[orig.length];
|
||||
int readBytes = in.read(uncompressed);
|
||||
assertEquals(orig.length, readBytes);
|
||||
assertArrayEquals(orig, uncompressed);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void streamFramed() throws Exception {
|
||||
for (String f : files) {
|
||||
byte[] orig = readFile("testdata/calgary/" + f);
|
||||
|
||||
ByteArrayOutputStream compressedBuf = new ByteArrayOutputStream();
|
||||
SnappyFramedOutputStream out = new SnappyFramedOutputStream(compressedBuf);
|
||||
out.write(orig);
|
||||
out.close();
|
||||
|
||||
SnappyFramedInputStream in = new SnappyFramedInputStream(new ByteArrayInputStream(compressedBuf.toByteArray()));
|
||||
|
||||
byte[] uncompressed = new byte[orig.length];
|
||||
int readBytes = readBytes(in, uncompressed, 0, orig.length);
|
||||
|
||||
assertEquals(orig.length, readBytes);
|
||||
assertArrayEquals(orig, uncompressed);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void streamFramedToFile() throws Exception {
|
||||
for (String f : files) {
|
||||
byte[] orig = readFile("testdata/calgary/" + f);
|
||||
|
||||
final File tempFile = tempFolder.newFile(f);
|
||||
final FileOutputStream compressedFOS = new FileOutputStream(tempFile);
|
||||
try
|
||||
{
|
||||
SnappyFramedOutputStream out = new SnappyFramedOutputStream(compressedFOS);
|
||||
out.write(orig);
|
||||
out.close();
|
||||
}
|
||||
finally
|
||||
{
|
||||
compressedFOS.close();
|
||||
}
|
||||
|
||||
byte[] uncompressed = new byte[orig.length];
|
||||
|
||||
final FileInputStream compressedFIS = new FileInputStream(tempFile);
|
||||
try
|
||||
{
|
||||
SnappyFramedInputStream in = new SnappyFramedInputStream(compressedFIS.getChannel());
|
||||
int readBytes = readBytes(in, uncompressed, 0, orig.length);
|
||||
|
||||
assertEquals(orig.length, readBytes);
|
||||
}
|
||||
finally
|
||||
{
|
||||
compressedFIS.close();
|
||||
}
|
||||
|
||||
assertArrayEquals(orig, uncompressed);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void streamFramedNoCRCVerify() throws Exception {
|
||||
for (String f : files) {
|
||||
byte[] orig = readFile("testdata/calgary/" + f);
|
||||
|
||||
ByteArrayOutputStream compressedBuf = new ByteArrayOutputStream();
|
||||
SnappyFramedOutputStream out = new SnappyFramedOutputStream(compressedBuf);
|
||||
out.write(orig);
|
||||
out.close();
|
||||
|
||||
SnappyFramedInputStream in = new SnappyFramedInputStream(new ByteArrayInputStream(compressedBuf.toByteArray()), false);
|
||||
|
||||
byte[] uncompressed = new byte[orig.length];
|
||||
int readBytes = readBytes(in, uncompressed, 0, orig.length);
|
||||
|
||||
assertEquals(orig.length, readBytes);
|
||||
assertArrayEquals(orig, uncompressed);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void byteWiseRead() throws Exception {
|
||||
for (String f : files) {
|
||||
byte[] orig = readFile("testdata/calgary/" + f);
|
||||
|
||||
ByteArrayOutputStream compressedBuf = new ByteArrayOutputStream();
|
||||
SnappyOutputStream out = new SnappyOutputStream(compressedBuf);
|
||||
out.write(orig);
|
||||
out.close();
|
||||
|
||||
SnappyInputStream in = new SnappyInputStream(new ByteArrayInputStream(compressedBuf.toByteArray()));
|
||||
byte[] uncompressed = new byte[orig.length];
|
||||
int cursor = 0;
|
||||
for (;;) {
|
||||
int b = in.read();
|
||||
if (b == -1)
|
||||
break;
|
||||
uncompressed[cursor++] = (byte) b;
|
||||
}
|
||||
assertEquals(orig.length, cursor);
|
||||
assertArrayEquals(orig, uncompressed);
|
||||
}
|
||||
}
|
||||
|
||||
static final int readBytes(InputStream source, byte[] dest, int offset, int length) throws IOException
|
||||
{
|
||||
// how many bytes were read.
|
||||
int lastRead = source.read(dest, offset, length);
|
||||
|
||||
int totalRead = lastRead;
|
||||
|
||||
// if we did not read as many bytes as we had hoped, try reading again.
|
||||
if (lastRead < length)
|
||||
{
|
||||
// as long the buffer is not full (remaining() == 0) and we have not reached EOF (lastRead == -1) keep reading.
|
||||
while (totalRead < length && lastRead != -1)
|
||||
{
|
||||
lastRead = source.read(dest, offset + totalRead, length - totalRead);
|
||||
|
||||
// if we got EOF, do not add to total read.
|
||||
if (lastRead != -1)
|
||||
{
|
||||
totalRead += lastRead;
|
||||
}
|
||||
}
|
||||
}
|
||||
return totalRead;
|
||||
}
|
||||
}
|
||||
/*--------------------------------------------------------------------------
|
||||
* 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
|
||||
//
|
||||
// CalgaryTest.java
|
||||
// Since: 2011/04/04 12:10:36
|
||||
//
|
||||
// $URL$
|
||||
// $Author$
|
||||
//--------------------------------------
|
||||
package org.xerial.snappy;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
import org.xerial.util.FileResource;
|
||||
import org.xerial.util.log.Logger;
|
||||
|
||||
/**
|
||||
* Benchmark using Calgary data set
|
||||
*
|
||||
* @author leo
|
||||
*
|
||||
*/
|
||||
public class CalgaryTest
|
||||
{
|
||||
private static Logger _logger = Logger.getLogger(CalgaryTest.class);
|
||||
|
||||
@Rule
|
||||
public final TemporaryFolder tempFolder = new TemporaryFolder();
|
||||
|
||||
static byte[] readFile(String file) throws IOException {
|
||||
InputStream in = FileResource.find(CalgaryTest.class, file).openStream();
|
||||
if (in == null)
|
||||
throw new IOException("file " + file + " is not found");
|
||||
try {
|
||||
return SnappyInputStreamTest.readFully(in);
|
||||
}
|
||||
finally {
|
||||
if (in != null)
|
||||
in.close();
|
||||
}
|
||||
}
|
||||
|
||||
public static final String[] files = { "bib", "book1", "book2", "geo", "news", "obj1", "obj2", "paper1", "paper2",
|
||||
"paper3", "paper4", "paper5", "paper6", "pic", "progc", "progl", "progp", "trans" };
|
||||
|
||||
@Test
|
||||
public void block() throws Exception {
|
||||
for (String f : files) {
|
||||
byte[] orig = readFile("testdata/calgary/" + f);
|
||||
|
||||
byte[] compressed = Snappy.compress(orig);
|
||||
byte[] uncompressed = Snappy.uncompress(compressed);
|
||||
|
||||
assertArrayEquals(orig, uncompressed);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void stream() throws Exception {
|
||||
for (String f : files) {
|
||||
byte[] orig = readFile("testdata/calgary/" + f);
|
||||
|
||||
ByteArrayOutputStream compressedBuf = new ByteArrayOutputStream();
|
||||
SnappyOutputStream out = new SnappyOutputStream(compressedBuf);
|
||||
out.write(orig);
|
||||
out.close();
|
||||
|
||||
SnappyInputStream in = new SnappyInputStream(new ByteArrayInputStream(compressedBuf.toByteArray()));
|
||||
byte[] uncompressed = new byte[orig.length];
|
||||
int readBytes = in.read(uncompressed);
|
||||
assertEquals(orig.length, readBytes);
|
||||
assertArrayEquals(orig, uncompressed);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void streamFramed() throws Exception {
|
||||
for (String f : files) {
|
||||
byte[] orig = readFile("testdata/calgary/" + f);
|
||||
|
||||
ByteArrayOutputStream compressedBuf = new ByteArrayOutputStream();
|
||||
SnappyFramedOutputStream out = new SnappyFramedOutputStream(compressedBuf);
|
||||
out.write(orig);
|
||||
out.close();
|
||||
|
||||
SnappyFramedInputStream in = new SnappyFramedInputStream(new ByteArrayInputStream(compressedBuf.toByteArray()));
|
||||
|
||||
byte[] uncompressed = new byte[orig.length];
|
||||
int readBytes = readBytes(in, uncompressed, 0, orig.length);
|
||||
|
||||
assertEquals(orig.length, readBytes);
|
||||
assertArrayEquals(orig, uncompressed);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void streamFramedToFile() throws Exception {
|
||||
for (String f : files) {
|
||||
byte[] orig = readFile("testdata/calgary/" + f);
|
||||
|
||||
final File tempFile = tempFolder.newFile(f);
|
||||
final FileOutputStream compressedFOS = new FileOutputStream(tempFile);
|
||||
try
|
||||
{
|
||||
SnappyFramedOutputStream out = new SnappyFramedOutputStream(compressedFOS);
|
||||
out.write(orig);
|
||||
out.close();
|
||||
}
|
||||
finally
|
||||
{
|
||||
compressedFOS.close();
|
||||
}
|
||||
|
||||
byte[] uncompressed = new byte[orig.length];
|
||||
|
||||
final FileInputStream compressedFIS = new FileInputStream(tempFile);
|
||||
try
|
||||
{
|
||||
SnappyFramedInputStream in = new SnappyFramedInputStream(compressedFIS.getChannel());
|
||||
int readBytes = readBytes(in, uncompressed, 0, orig.length);
|
||||
|
||||
assertEquals(orig.length, readBytes);
|
||||
}
|
||||
finally
|
||||
{
|
||||
compressedFIS.close();
|
||||
}
|
||||
|
||||
assertArrayEquals(orig, uncompressed);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void streamFramedNoCRCVerify() throws Exception {
|
||||
for (String f : files) {
|
||||
byte[] orig = readFile("testdata/calgary/" + f);
|
||||
|
||||
ByteArrayOutputStream compressedBuf = new ByteArrayOutputStream();
|
||||
SnappyFramedOutputStream out = new SnappyFramedOutputStream(compressedBuf);
|
||||
out.write(orig);
|
||||
out.close();
|
||||
|
||||
SnappyFramedInputStream in = new SnappyFramedInputStream(new ByteArrayInputStream(compressedBuf.toByteArray()), false);
|
||||
|
||||
byte[] uncompressed = new byte[orig.length];
|
||||
int readBytes = readBytes(in, uncompressed, 0, orig.length);
|
||||
|
||||
assertEquals(orig.length, readBytes);
|
||||
assertArrayEquals(orig, uncompressed);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void byteWiseRead() throws Exception {
|
||||
for (String f : files) {
|
||||
byte[] orig = readFile("testdata/calgary/" + f);
|
||||
|
||||
ByteArrayOutputStream compressedBuf = new ByteArrayOutputStream();
|
||||
SnappyOutputStream out = new SnappyOutputStream(compressedBuf);
|
||||
out.write(orig);
|
||||
out.close();
|
||||
|
||||
SnappyInputStream in = new SnappyInputStream(new ByteArrayInputStream(compressedBuf.toByteArray()));
|
||||
byte[] uncompressed = new byte[orig.length];
|
||||
int cursor = 0;
|
||||
for (;;) {
|
||||
int b = in.read();
|
||||
if (b == -1)
|
||||
break;
|
||||
uncompressed[cursor++] = (byte) b;
|
||||
}
|
||||
assertEquals(orig.length, cursor);
|
||||
assertArrayEquals(orig, uncompressed);
|
||||
}
|
||||
}
|
||||
|
||||
static final int readBytes(InputStream source, byte[] dest, int offset, int length) throws IOException
|
||||
{
|
||||
// how many bytes were read.
|
||||
int lastRead = source.read(dest, offset, length);
|
||||
|
||||
int totalRead = lastRead;
|
||||
|
||||
// if we did not read as many bytes as we had hoped, try reading again.
|
||||
if (lastRead < length)
|
||||
{
|
||||
// as long the buffer is not full (remaining() == 0) and we have not reached EOF (lastRead == -1) keep reading.
|
||||
while (totalRead < length && lastRead != -1)
|
||||
{
|
||||
lastRead = source.read(dest, offset + totalRead, length - totalRead);
|
||||
|
||||
// if we got EOF, do not add to total read.
|
||||
if (lastRead != -1)
|
||||
{
|
||||
totalRead += lastRead;
|
||||
}
|
||||
}
|
||||
}
|
||||
return totalRead;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,68 +1,68 @@
|
|||
/*
|
||||
* Created: Apr 15, 2013
|
||||
*/
|
||||
package org.xerial.snappy;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* Generates random data with specific expected snappy performance characteristics.
|
||||
*
|
||||
* <p>
|
||||
* This has been copied from <a href="https://github.com/dain/snappy"> dain's snappy</a> implementation..
|
||||
* </p>
|
||||
*/
|
||||
public class RandomGenerator {
|
||||
|
||||
public final byte[] data;
|
||||
public int position;
|
||||
|
||||
public RandomGenerator(double compressionRatio) {
|
||||
// We use a limited amount of data over and over again and ensure
|
||||
// that it is larger than the compression window (32KB), and also
|
||||
// large enough to serve all typical value sizes we want to write.
|
||||
Random rnd = new Random(301);
|
||||
data = new byte[1048576 + 100];
|
||||
for (int i = 0; i < 1048576; i += 100) {
|
||||
// Add a short fragment that is as compressible as specified ratio
|
||||
System.arraycopy(compressibleData(rnd, compressionRatio, 100), 0,
|
||||
data, i, 100);
|
||||
}
|
||||
}
|
||||
|
||||
public int getNextPosition(int length) {
|
||||
if (position + length > data.length) {
|
||||
position = 0;
|
||||
assert (length < data.length);
|
||||
}
|
||||
int result = position;
|
||||
position += length;
|
||||
return result;
|
||||
}
|
||||
|
||||
private static byte[] compressibleData(Random random,
|
||||
double compressionRatio, int length) {
|
||||
int raw = (int) (length * compressionRatio);
|
||||
if (raw < 1) {
|
||||
raw = 1;
|
||||
}
|
||||
byte[] rawData = generateRandomData(random, raw);
|
||||
|
||||
// Duplicate the random data until we have filled "length" bytes
|
||||
byte[] dest = new byte[length];
|
||||
for (int i = 0; i < length;) {
|
||||
int chunkLength = Math.min(rawData.length, length - i);
|
||||
System.arraycopy(rawData, 0, dest, i, chunkLength);
|
||||
i += chunkLength;
|
||||
}
|
||||
return dest;
|
||||
}
|
||||
|
||||
private static byte[] generateRandomData(Random random, int length) {
|
||||
byte[] rawData = new byte[length];
|
||||
for (int i = 0; i < rawData.length; i++) {
|
||||
rawData[i] = (byte) random.nextInt(256);
|
||||
}
|
||||
return rawData;
|
||||
}
|
||||
/*
|
||||
* Created: Apr 15, 2013
|
||||
*/
|
||||
package org.xerial.snappy;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* Generates random data with specific expected snappy performance characteristics.
|
||||
*
|
||||
* <p>
|
||||
* This has been copied from <a href="https://github.com/dain/snappy"> dain's snappy</a> implementation..
|
||||
* </p>
|
||||
*/
|
||||
public class RandomGenerator {
|
||||
|
||||
public final byte[] data;
|
||||
public int position;
|
||||
|
||||
public RandomGenerator(double compressionRatio) {
|
||||
// We use a limited amount of data over and over again and ensure
|
||||
// that it is larger than the compression window (32KB), and also
|
||||
// large enough to serve all typical value sizes we want to write.
|
||||
Random rnd = new Random(301);
|
||||
data = new byte[1048576 + 100];
|
||||
for (int i = 0; i < 1048576; i += 100) {
|
||||
// Add a short fragment that is as compressible as specified ratio
|
||||
System.arraycopy(compressibleData(rnd, compressionRatio, 100), 0,
|
||||
data, i, 100);
|
||||
}
|
||||
}
|
||||
|
||||
public int getNextPosition(int length) {
|
||||
if (position + length > data.length) {
|
||||
position = 0;
|
||||
assert (length < data.length);
|
||||
}
|
||||
int result = position;
|
||||
position += length;
|
||||
return result;
|
||||
}
|
||||
|
||||
private static byte[] compressibleData(Random random,
|
||||
double compressionRatio, int length) {
|
||||
int raw = (int) (length * compressionRatio);
|
||||
if (raw < 1) {
|
||||
raw = 1;
|
||||
}
|
||||
byte[] rawData = generateRandomData(random, raw);
|
||||
|
||||
// Duplicate the random data until we have filled "length" bytes
|
||||
byte[] dest = new byte[length];
|
||||
for (int i = 0; i < length;) {
|
||||
int chunkLength = Math.min(rawData.length, length - i);
|
||||
System.arraycopy(rawData, 0, dest, i, chunkLength);
|
||||
i += chunkLength;
|
||||
}
|
||||
return dest;
|
||||
}
|
||||
|
||||
private static byte[] generateRandomData(Random random, int length) {
|
||||
byte[] rawData = new byte[length];
|
||||
for (int i = 0; i < rawData.length; i++) {
|
||||
rawData[i] = (byte) random.nextInt(256);
|
||||
}
|
||||
return rawData;
|
||||
}
|
||||
}
|
|
@ -1,179 +1,179 @@
|
|||
/*--------------------------------------------------------------------------
|
||||
* 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
|
||||
//
|
||||
// SnappyInputStreamTest.java
|
||||
// Since: 2011/03/31 22:31:51
|
||||
//
|
||||
// $URL$
|
||||
// $Author$
|
||||
//--------------------------------------
|
||||
package org.xerial.snappy;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.xerial.util.FileResource;
|
||||
import org.xerial.util.log.Logger;
|
||||
import scala.Array;
|
||||
|
||||
public class SnappyInputStreamTest
|
||||
{
|
||||
private static Logger _logger = Logger.getLogger(SnappyInputStreamTest.class);
|
||||
|
||||
public static byte[] readResourceFile(String fileName) throws IOException {
|
||||
BufferedInputStream input = new BufferedInputStream(FileResource.find(SnappyOutputStreamTest.class, fileName)
|
||||
.openStream());
|
||||
assertNotNull(input);
|
||||
return readFully(input);
|
||||
}
|
||||
|
||||
public static byte[] readFully(InputStream input) throws IOException {
|
||||
try {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
byte[] buf = new byte[4096];
|
||||
for (int readBytes = 0; (readBytes = input.read(buf)) != -1;) {
|
||||
out.write(buf, 0, readBytes);
|
||||
}
|
||||
out.flush();
|
||||
return out.toByteArray();
|
||||
}
|
||||
finally {
|
||||
input.close();
|
||||
}
|
||||
}
|
||||
|
||||
public static byte[] byteWiseReadFully(InputStream input) throws IOException {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
byte[] buf = new byte[4096];
|
||||
for (int readData = 0; (readData = input.read()) != -1;) {
|
||||
out.write(readData);
|
||||
}
|
||||
out.flush();
|
||||
return out.toByteArray();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void read() throws Exception {
|
||||
ByteArrayOutputStream compressedBuf = new ByteArrayOutputStream();
|
||||
SnappyOutputStream snappyOut = new SnappyOutputStream(compressedBuf);
|
||||
byte[] orig = readResourceFile("alice29.txt");
|
||||
snappyOut.write(orig);
|
||||
snappyOut.close();
|
||||
byte[] compressed = compressedBuf.toByteArray();
|
||||
_logger.debug("compressed size: " + compressed.length);
|
||||
|
||||
SnappyInputStream in = new SnappyInputStream(new ByteArrayInputStream(compressed));
|
||||
byte[] uncompressed = readFully(in);
|
||||
|
||||
assertEquals(orig.length, uncompressed.length);
|
||||
assertArrayEquals(orig, uncompressed);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readBlockCompressedData() throws Exception {
|
||||
byte[] orig = readResourceFile("alice29.txt");
|
||||
byte[] compressed = Snappy.compress(orig);
|
||||
|
||||
SnappyInputStream in = new SnappyInputStream(new ByteArrayInputStream(compressed));
|
||||
byte[] uncompressed = readFully(in);
|
||||
|
||||
assertEquals(orig.length, uncompressed.length);
|
||||
assertArrayEquals(orig, uncompressed);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void biteWiseRead() throws Exception {
|
||||
byte[] orig = readResourceFile("testdata/calgary/paper6");
|
||||
byte[] compressed = Snappy.compress(orig);
|
||||
|
||||
SnappyInputStream in = new SnappyInputStream(new ByteArrayInputStream(compressed));
|
||||
byte[] uncompressed = byteWiseReadFully(in);
|
||||
|
||||
assertEquals(orig.length, uncompressed.length);
|
||||
assertArrayEquals(orig, uncompressed);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void available() throws Exception {
|
||||
byte[] orig = readResourceFile("testdata/calgary/paper6");
|
||||
byte[] compressed = Snappy.compress(orig);
|
||||
|
||||
SnappyInputStream in = new SnappyInputStream(new ByteArrayInputStream(compressed));
|
||||
byte[] buf = new byte[4];
|
||||
for (int readBytes = 0; (readBytes = in.read(buf)) != -1;) {
|
||||
assertTrue(in.available() >= 0);
|
||||
}
|
||||
assertTrue(in.available() == 0);
|
||||
in.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void emptyStream() throws Exception {
|
||||
try {
|
||||
SnappyInputStream in = new SnappyInputStream(new ByteArrayInputStream(new byte[0]));
|
||||
byte[] uncompressed = readFully(in);
|
||||
assertEquals(0, uncompressed.length);
|
||||
fail("should not reach here");
|
||||
}
|
||||
catch(SnappyIOException e) {
|
||||
assertEquals(SnappyErrorCode.EMPTY_INPUT, e.getErrorCode());
|
||||
}
|
||||
}
|
||||
|
||||
public static byte[] compressResource(String resourcePath) throws Exception {
|
||||
ByteArrayOutputStream compressedBuf = new ByteArrayOutputStream();
|
||||
SnappyOutputStream snappyOut = new SnappyOutputStream(compressedBuf);
|
||||
byte[] orig = readResourceFile(resourcePath);
|
||||
snappyOut.write(orig);
|
||||
snappyOut.close();
|
||||
return compressedBuf.toByteArray();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void chunkRead() throws Exception {
|
||||
byte[] chunk1 = compressResource("alice29.txt");
|
||||
byte[] chunk2 = compressResource("testdata/calgary/paper6");
|
||||
|
||||
byte[] concatenated = new byte[chunk1.length + chunk2.length];
|
||||
System.arraycopy(chunk1, 0, concatenated, 0, chunk1.length);
|
||||
System.arraycopy(chunk2, 0, concatenated, chunk1.length, chunk2.length);
|
||||
|
||||
SnappyInputStream in = new SnappyInputStream(new ByteArrayInputStream(concatenated));
|
||||
byte[] uncompressed = readFully(in);
|
||||
|
||||
byte[] orig1 = readResourceFile("alice29.txt");
|
||||
byte[] orig2 = readResourceFile("testdata/calgary/paper6");
|
||||
assertEquals(orig1.length + orig2.length, uncompressed.length);
|
||||
byte[] uncompressed1 = new byte[orig1.length];
|
||||
byte[] uncompressed2 = new byte[orig2.length];
|
||||
System.arraycopy(uncompressed, 0, uncompressed1, 0, orig1.length);
|
||||
System.arraycopy(uncompressed, orig1.length, uncompressed2, 0, orig2.length);
|
||||
|
||||
assertArrayEquals(orig1, uncompressed1);
|
||||
assertArrayEquals(orig2, uncompressed2);
|
||||
}
|
||||
|
||||
}
|
||||
/*--------------------------------------------------------------------------
|
||||
* 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
|
||||
//
|
||||
// SnappyInputStreamTest.java
|
||||
// Since: 2011/03/31 22:31:51
|
||||
//
|
||||
// $URL$
|
||||
// $Author$
|
||||
//--------------------------------------
|
||||
package org.xerial.snappy;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.xerial.util.FileResource;
|
||||
import org.xerial.util.log.Logger;
|
||||
import scala.Array;
|
||||
|
||||
public class SnappyInputStreamTest
|
||||
{
|
||||
private static Logger _logger = Logger.getLogger(SnappyInputStreamTest.class);
|
||||
|
||||
public static byte[] readResourceFile(String fileName) throws IOException {
|
||||
BufferedInputStream input = new BufferedInputStream(FileResource.find(SnappyOutputStreamTest.class, fileName)
|
||||
.openStream());
|
||||
assertNotNull(input);
|
||||
return readFully(input);
|
||||
}
|
||||
|
||||
public static byte[] readFully(InputStream input) throws IOException {
|
||||
try {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
byte[] buf = new byte[4096];
|
||||
for (int readBytes = 0; (readBytes = input.read(buf)) != -1;) {
|
||||
out.write(buf, 0, readBytes);
|
||||
}
|
||||
out.flush();
|
||||
return out.toByteArray();
|
||||
}
|
||||
finally {
|
||||
input.close();
|
||||
}
|
||||
}
|
||||
|
||||
public static byte[] byteWiseReadFully(InputStream input) throws IOException {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
byte[] buf = new byte[4096];
|
||||
for (int readData = 0; (readData = input.read()) != -1;) {
|
||||
out.write(readData);
|
||||
}
|
||||
out.flush();
|
||||
return out.toByteArray();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void read() throws Exception {
|
||||
ByteArrayOutputStream compressedBuf = new ByteArrayOutputStream();
|
||||
SnappyOutputStream snappyOut = new SnappyOutputStream(compressedBuf);
|
||||
byte[] orig = readResourceFile("alice29.txt");
|
||||
snappyOut.write(orig);
|
||||
snappyOut.close();
|
||||
byte[] compressed = compressedBuf.toByteArray();
|
||||
_logger.debug("compressed size: " + compressed.length);
|
||||
|
||||
SnappyInputStream in = new SnappyInputStream(new ByteArrayInputStream(compressed));
|
||||
byte[] uncompressed = readFully(in);
|
||||
|
||||
assertEquals(orig.length, uncompressed.length);
|
||||
assertArrayEquals(orig, uncompressed);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readBlockCompressedData() throws Exception {
|
||||
byte[] orig = readResourceFile("alice29.txt");
|
||||
byte[] compressed = Snappy.compress(orig);
|
||||
|
||||
SnappyInputStream in = new SnappyInputStream(new ByteArrayInputStream(compressed));
|
||||
byte[] uncompressed = readFully(in);
|
||||
|
||||
assertEquals(orig.length, uncompressed.length);
|
||||
assertArrayEquals(orig, uncompressed);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void biteWiseRead() throws Exception {
|
||||
byte[] orig = readResourceFile("testdata/calgary/paper6");
|
||||
byte[] compressed = Snappy.compress(orig);
|
||||
|
||||
SnappyInputStream in = new SnappyInputStream(new ByteArrayInputStream(compressed));
|
||||
byte[] uncompressed = byteWiseReadFully(in);
|
||||
|
||||
assertEquals(orig.length, uncompressed.length);
|
||||
assertArrayEquals(orig, uncompressed);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void available() throws Exception {
|
||||
byte[] orig = readResourceFile("testdata/calgary/paper6");
|
||||
byte[] compressed = Snappy.compress(orig);
|
||||
|
||||
SnappyInputStream in = new SnappyInputStream(new ByteArrayInputStream(compressed));
|
||||
byte[] buf = new byte[4];
|
||||
for (int readBytes = 0; (readBytes = in.read(buf)) != -1;) {
|
||||
assertTrue(in.available() >= 0);
|
||||
}
|
||||
assertTrue(in.available() == 0);
|
||||
in.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void emptyStream() throws Exception {
|
||||
try {
|
||||
SnappyInputStream in = new SnappyInputStream(new ByteArrayInputStream(new byte[0]));
|
||||
byte[] uncompressed = readFully(in);
|
||||
assertEquals(0, uncompressed.length);
|
||||
fail("should not reach here");
|
||||
}
|
||||
catch(SnappyIOException e) {
|
||||
assertEquals(SnappyErrorCode.EMPTY_INPUT, e.getErrorCode());
|
||||
}
|
||||
}
|
||||
|
||||
public static byte[] compressResource(String resourcePath) throws Exception {
|
||||
ByteArrayOutputStream compressedBuf = new ByteArrayOutputStream();
|
||||
SnappyOutputStream snappyOut = new SnappyOutputStream(compressedBuf);
|
||||
byte[] orig = readResourceFile(resourcePath);
|
||||
snappyOut.write(orig);
|
||||
snappyOut.close();
|
||||
return compressedBuf.toByteArray();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void chunkRead() throws Exception {
|
||||
byte[] chunk1 = compressResource("alice29.txt");
|
||||
byte[] chunk2 = compressResource("testdata/calgary/paper6");
|
||||
|
||||
byte[] concatenated = new byte[chunk1.length + chunk2.length];
|
||||
System.arraycopy(chunk1, 0, concatenated, 0, chunk1.length);
|
||||
System.arraycopy(chunk2, 0, concatenated, chunk1.length, chunk2.length);
|
||||
|
||||
SnappyInputStream in = new SnappyInputStream(new ByteArrayInputStream(concatenated));
|
||||
byte[] uncompressed = readFully(in);
|
||||
|
||||
byte[] orig1 = readResourceFile("alice29.txt");
|
||||
byte[] orig2 = readResourceFile("testdata/calgary/paper6");
|
||||
assertEquals(orig1.length + orig2.length, uncompressed.length);
|
||||
byte[] uncompressed1 = new byte[orig1.length];
|
||||
byte[] uncompressed2 = new byte[orig2.length];
|
||||
System.arraycopy(uncompressed, 0, uncompressed1, 0, orig1.length);
|
||||
System.arraycopy(uncompressed, orig1.length, uncompressed2, 0, orig2.length);
|
||||
|
||||
assertArrayEquals(orig1, uncompressed1);
|
||||
assertArrayEquals(orig2, uncompressed2);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,125 +1,125 @@
|
|||
/*--------------------------------------------------------------------------
|
||||
* 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
|
||||
//
|
||||
// SnappyLoaderTest.java
|
||||
// Since: 2011/06/22 23:59:47
|
||||
//
|
||||
// $URL$
|
||||
// $Author$
|
||||
//--------------------------------------
|
||||
package org.xerial.snappy;
|
||||
|
||||
import org.codehaus.plexus.classworlds.ClassWorld;
|
||||
import org.codehaus.plexus.classworlds.realm.ClassRealm;
|
||||
import org.junit.Test;
|
||||
import org.xerial.util.FileResource;
|
||||
import org.xerial.util.log.Logger;
|
||||
|
||||
import java.io.*;
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
public class SnappyLoaderTest
|
||||
{
|
||||
private static Logger _logger = Logger.getLogger(SnappyLoaderTest.class);
|
||||
|
||||
public static BufferedInputStream openByteStream(Class< ? > referenceClass, String resourceFileName)
|
||||
throws IOException {
|
||||
URL url = FileResource.find(referenceClass, resourceFileName);
|
||||
if (url != null) {
|
||||
return new BufferedInputStream(url.openStream());
|
||||
}
|
||||
else
|
||||
return null;
|
||||
}
|
||||
|
||||
public static <T> String loadIntoString(Class<T> referenceClass, String path) throws IOException {
|
||||
BufferedInputStream in = openByteStream(referenceClass, path);
|
||||
if (in == null)
|
||||
throw new FileNotFoundException(
|
||||
String.format("reference class:%s, path:%s", referenceClass.getName(), path));
|
||||
|
||||
ByteArrayOutputStream buf = new ByteArrayOutputStream();
|
||||
try {
|
||||
byte[] tmp = new byte[4028];
|
||||
for (int readBytes = 0; (readBytes = in.read(tmp)) != -1;) {
|
||||
buf.write(tmp, 0, readBytes);
|
||||
}
|
||||
buf.flush();
|
||||
return buf.toString();
|
||||
}
|
||||
finally {
|
||||
in.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void loadSnappyByDiffentClassloadersInTheSameJVM() throws Exception {
|
||||
|
||||
// Parent class loader cannot see Snappy.class
|
||||
ClassLoader parent = this.getClass().getClassLoader().getParent();
|
||||
ClassWorld cw = new ClassWorld();
|
||||
ClassRealm P = cw.newRealm("P", parent);
|
||||
try {
|
||||
P.loadClass("org.xerial.snappy.Snappy");
|
||||
fail("org.xerial.snappy.Snappy is found in the parent");
|
||||
}
|
||||
catch (ClassNotFoundException e) {
|
||||
// OK
|
||||
}
|
||||
|
||||
// Prepare the child class loaders which can load Snappy.class
|
||||
URL classPath = new File("target/classes").toURI().toURL();
|
||||
ClassRealm L1 = cw.newRealm("l1", URLClassLoader.newInstance(new URL[] { classPath }, parent));
|
||||
ClassRealm L2 = cw.newRealm("l2", URLClassLoader.newInstance(new URL[] { classPath }, parent));
|
||||
|
||||
// Actually load Snappy.class in a child class loader
|
||||
|
||||
Class< ? > S1 = L1.loadClass("org.xerial.snappy.Snappy");
|
||||
Method m = S1.getMethod("compress", String.class);
|
||||
byte[] v = (byte[]) m.invoke(null, "hello world");
|
||||
|
||||
// Load Snappy.class from another child class loader
|
||||
Class< ? > S2 = L2.loadClass("org.xerial.snappy.Snappy");
|
||||
Method m2 = S2.getMethod("compress", String.class);
|
||||
byte[] v2 = (byte[]) m2.invoke(null, "hello world");
|
||||
|
||||
assertArrayEquals(v, v2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void load() throws Exception {
|
||||
SnappyLoader.load();
|
||||
_logger.debug(Snappy.maxCompressedLength(1024));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void autoLoad() throws Exception {
|
||||
_logger.debug(Snappy.maxCompressedLength(1024));
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
// Test for loading native library specified in -Djava.library.path
|
||||
System.setProperty(SnappyLoader.KEY_SNAPPY_USE_SYSTEMLIB, "true");
|
||||
_logger.debug(Snappy.maxCompressedLength(1024));
|
||||
}
|
||||
}
|
||||
/*--------------------------------------------------------------------------
|
||||
* 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
|
||||
//
|
||||
// SnappyLoaderTest.java
|
||||
// Since: 2011/06/22 23:59:47
|
||||
//
|
||||
// $URL$
|
||||
// $Author$
|
||||
//--------------------------------------
|
||||
package org.xerial.snappy;
|
||||
|
||||
import org.codehaus.plexus.classworlds.ClassWorld;
|
||||
import org.codehaus.plexus.classworlds.realm.ClassRealm;
|
||||
import org.junit.Test;
|
||||
import org.xerial.util.FileResource;
|
||||
import org.xerial.util.log.Logger;
|
||||
|
||||
import java.io.*;
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
public class SnappyLoaderTest
|
||||
{
|
||||
private static Logger _logger = Logger.getLogger(SnappyLoaderTest.class);
|
||||
|
||||
public static BufferedInputStream openByteStream(Class< ? > referenceClass, String resourceFileName)
|
||||
throws IOException {
|
||||
URL url = FileResource.find(referenceClass, resourceFileName);
|
||||
if (url != null) {
|
||||
return new BufferedInputStream(url.openStream());
|
||||
}
|
||||
else
|
||||
return null;
|
||||
}
|
||||
|
||||
public static <T> String loadIntoString(Class<T> referenceClass, String path) throws IOException {
|
||||
BufferedInputStream in = openByteStream(referenceClass, path);
|
||||
if (in == null)
|
||||
throw new FileNotFoundException(
|
||||
String.format("reference class:%s, path:%s", referenceClass.getName(), path));
|
||||
|
||||
ByteArrayOutputStream buf = new ByteArrayOutputStream();
|
||||
try {
|
||||
byte[] tmp = new byte[4028];
|
||||
for (int readBytes = 0; (readBytes = in.read(tmp)) != -1;) {
|
||||
buf.write(tmp, 0, readBytes);
|
||||
}
|
||||
buf.flush();
|
||||
return buf.toString();
|
||||
}
|
||||
finally {
|
||||
in.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void loadSnappyByDiffentClassloadersInTheSameJVM() throws Exception {
|
||||
|
||||
// Parent class loader cannot see Snappy.class
|
||||
ClassLoader parent = this.getClass().getClassLoader().getParent();
|
||||
ClassWorld cw = new ClassWorld();
|
||||
ClassRealm P = cw.newRealm("P", parent);
|
||||
try {
|
||||
P.loadClass("org.xerial.snappy.Snappy");
|
||||
fail("org.xerial.snappy.Snappy is found in the parent");
|
||||
}
|
||||
catch (ClassNotFoundException e) {
|
||||
// OK
|
||||
}
|
||||
|
||||
// Prepare the child class loaders which can load Snappy.class
|
||||
URL classPath = new File("target/classes").toURI().toURL();
|
||||
ClassRealm L1 = cw.newRealm("l1", URLClassLoader.newInstance(new URL[] { classPath }, parent));
|
||||
ClassRealm L2 = cw.newRealm("l2", URLClassLoader.newInstance(new URL[] { classPath }, parent));
|
||||
|
||||
// Actually load Snappy.class in a child class loader
|
||||
|
||||
Class< ? > S1 = L1.loadClass("org.xerial.snappy.Snappy");
|
||||
Method m = S1.getMethod("compress", String.class);
|
||||
byte[] v = (byte[]) m.invoke(null, "hello world");
|
||||
|
||||
// Load Snappy.class from another child class loader
|
||||
Class< ? > S2 = L2.loadClass("org.xerial.snappy.Snappy");
|
||||
Method m2 = S2.getMethod("compress", String.class);
|
||||
byte[] v2 = (byte[]) m2.invoke(null, "hello world");
|
||||
|
||||
assertArrayEquals(v, v2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void load() throws Exception {
|
||||
SnappyLoader.load();
|
||||
_logger.debug(Snappy.maxCompressedLength(1024));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void autoLoad() throws Exception {
|
||||
_logger.debug(Snappy.maxCompressedLength(1024));
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
// Test for loading native library specified in -Djava.library.path
|
||||
System.setProperty(SnappyLoader.KEY_SNAPPY_USE_SYSTEMLIB, "true");
|
||||
_logger.debug(Snappy.maxCompressedLength(1024));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,304 +1,304 @@
|
|||
/*--------------------------------------------------------------------------
|
||||
* 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.
|
||||
*--------------------------------------------------------------------------*/
|
||||
//--------------------------------------
|
||||
// snappy-java Project
|
||||
//
|
||||
// SnappyTest.java
|
||||
// Since: 2011/03/30
|
||||
//
|
||||
// $URL$
|
||||
// $Author$
|
||||
//--------------------------------------
|
||||
package org.xerial.snappy;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.xerial.util.log.Logger;
|
||||
|
||||
public class SnappyTest
|
||||
{
|
||||
private static Logger _logger = Logger.getLogger(SnappyTest.class);
|
||||
|
||||
@Test
|
||||
public void getVersion() throws Exception {
|
||||
String version = Snappy.getNativeLibraryVersion();
|
||||
_logger.debug("version: " + version);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void directBufferCheck() throws Exception {
|
||||
|
||||
try {
|
||||
ByteBuffer src = ByteBuffer.allocate(1024);
|
||||
src.put("hello world".getBytes());
|
||||
src.flip();
|
||||
ByteBuffer dest = ByteBuffer.allocate(1024);
|
||||
int maxCompressedLen = Snappy.compress(src, dest);
|
||||
}
|
||||
catch (SnappyError e) {
|
||||
Assert.assertTrue(e.errorCode == SnappyErrorCode.NOT_A_DIRECT_BUFFER);
|
||||
return;
|
||||
}
|
||||
|
||||
fail("shouldn't reach here");
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void directBuffer() throws Exception {
|
||||
|
||||
StringBuilder s = new StringBuilder();
|
||||
for (int i = 0; i < 20; ++i) {
|
||||
s.append("Hello world!");
|
||||
}
|
||||
String origStr = s.toString();
|
||||
byte[] orig = origStr.getBytes();
|
||||
int BUFFER_SIZE = orig.length;
|
||||
ByteBuffer src = ByteBuffer.allocateDirect(orig.length);
|
||||
src.put(orig);
|
||||
src.flip();
|
||||
_logger.debug("input size: " + src.remaining());
|
||||
int maxCompressedLen = Snappy.maxCompressedLength(src.remaining());
|
||||
_logger.debug("max compressed length:" + maxCompressedLen);
|
||||
|
||||
ByteBuffer compressed = ByteBuffer.allocateDirect(maxCompressedLen);
|
||||
int compressedSize = Snappy.compress(src, compressed);
|
||||
_logger.debug("compressed length: " + compressedSize);
|
||||
|
||||
assertTrue(Snappy.isValidCompressedBuffer(compressed));
|
||||
|
||||
assertEquals(0, src.position());
|
||||
assertEquals(orig.length, src.remaining());
|
||||
assertEquals(orig.length, src.limit());
|
||||
|
||||
assertEquals(0, compressed.position());
|
||||
assertEquals(compressedSize, compressed.limit());
|
||||
assertEquals(compressedSize, compressed.remaining());
|
||||
|
||||
int uncompressedLen = Snappy.uncompressedLength(compressed);
|
||||
_logger.debug("uncompressed length: " + uncompressedLen);
|
||||
ByteBuffer extract = ByteBuffer.allocateDirect(uncompressedLen);
|
||||
int uncompressedLen2 = Snappy.uncompress(compressed, extract);
|
||||
assertEquals(uncompressedLen, uncompressedLen2);
|
||||
assertEquals(uncompressedLen, extract.remaining());
|
||||
|
||||
byte[] b = new byte[uncompressedLen];
|
||||
extract.get(b);
|
||||
String decompressed = new String(b);
|
||||
_logger.debug(decompressed);
|
||||
|
||||
assertEquals(origStr, decompressed);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bufferOffset() throws Exception {
|
||||
|
||||
String m = "ACCAGGGGGGGGGGGGGGGGGGGGATAGATATTTCCCGAGATATTTTATATAAAAAAA";
|
||||
byte[] orig = m.getBytes();
|
||||
final int offset = 100;
|
||||
ByteBuffer input = ByteBuffer.allocateDirect(orig.length + offset);
|
||||
input.position(offset);
|
||||
input.put(orig);
|
||||
input.flip();
|
||||
input.position(offset);
|
||||
|
||||
// compress
|
||||
int maxCompressedLength = Snappy.maxCompressedLength(input.remaining());
|
||||
final int offset2 = 40;
|
||||
ByteBuffer compressed = ByteBuffer.allocateDirect(maxCompressedLength + offset2);
|
||||
compressed.position(offset2);
|
||||
Snappy.compress(input, compressed);
|
||||
assertTrue(Snappy.isValidCompressedBuffer(compressed));
|
||||
|
||||
// uncompress
|
||||
final int offset3 = 80;
|
||||
int uncompressedLength = Snappy.uncompressedLength(compressed);
|
||||
ByteBuffer uncompressed = ByteBuffer.allocateDirect(uncompressedLength + offset3);
|
||||
uncompressed.position(offset3);
|
||||
Snappy.uncompress(compressed, uncompressed);
|
||||
assertEquals(offset3, uncompressed.position());
|
||||
assertEquals(offset3 + uncompressedLength, uncompressed.limit());
|
||||
assertEquals(uncompressedLength, uncompressed.remaining());
|
||||
|
||||
// extract string
|
||||
byte[] recovered = new byte[uncompressedLength];
|
||||
uncompressed.get(recovered);
|
||||
String m2 = new String(recovered);
|
||||
|
||||
assertEquals(m, m2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void byteArrayCompress() throws Exception {
|
||||
|
||||
String m = "ACCAGGGGGGGGGGGGGGGGGGGGATAGATATTTCCCGAGATATTTTATATAAAAAAA";
|
||||
byte[] input = m.getBytes();
|
||||
byte[] output = new byte[Snappy.maxCompressedLength(input.length)];
|
||||
int compressedSize = Snappy.compress(input, 0, input.length, output, 0);
|
||||
byte[] uncompressed = new byte[input.length];
|
||||
|
||||
assertTrue(Snappy.isValidCompressedBuffer(output, 0, compressedSize));
|
||||
int uncompressedSize = Snappy.uncompress(output, 0, compressedSize, uncompressed, 0);
|
||||
String m2 = new String(uncompressed);
|
||||
assertEquals(m, m2);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void rangeCheck() throws Exception {
|
||||
String m = "ACCAGGGGGGGGGGGGGGGGGGGGATAGATATTTCCCGAGATATTTTATATAAAAAAA";
|
||||
byte[] input = m.getBytes();
|
||||
byte[] output = new byte[Snappy.maxCompressedLength(input.length)];
|
||||
int compressedSize = Snappy.compress(input, 0, input.length, output, 0);
|
||||
|
||||
assertTrue(Snappy.isValidCompressedBuffer(output, 0, compressedSize));
|
||||
// Intentionally set an invalid range
|
||||
assertFalse(Snappy.isValidCompressedBuffer(output, 0, compressedSize + 1));
|
||||
assertFalse(Snappy.isValidCompressedBuffer(output, 1, compressedSize));
|
||||
|
||||
// Test the ByteBuffer API
|
||||
ByteBuffer bin = ByteBuffer.allocateDirect(input.length);
|
||||
bin.put(input);
|
||||
bin.flip();
|
||||
ByteBuffer bout = ByteBuffer.allocateDirect(Snappy.maxCompressedLength(bin.remaining()));
|
||||
int compressedSize2 = Snappy.compress(bin, bout);
|
||||
assertEquals(compressedSize, compressedSize2);
|
||||
|
||||
assertTrue(Snappy.isValidCompressedBuffer(bout));
|
||||
// Intentionally set an invalid range
|
||||
bout.limit(bout.limit() + 1);
|
||||
assertFalse(Snappy.isValidCompressedBuffer(bout));
|
||||
bout.limit(bout.limit() - 1);
|
||||
bout.position(1);
|
||||
assertFalse(Snappy.isValidCompressedBuffer(bout));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void highLevelAPI() throws Exception {
|
||||
|
||||
String m = "Hello! 01234 ACGDSFSDFJ World. FDSDF02394234 fdsfda03924";
|
||||
byte[] input = m.getBytes();
|
||||
byte[] output = Snappy.compress(input);
|
||||
|
||||
byte[] uncompressed = Snappy.uncompress(output);
|
||||
String m2 = new String(uncompressed);
|
||||
assertEquals(m, m2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void lowLevelAPI() throws Exception {
|
||||
|
||||
String m = "Hello! 01234 ACGDSFSDFJ World. FDSDF02394234 fdsfda03924";
|
||||
byte[] input = m.getBytes();
|
||||
byte[] output = Snappy.rawCompress(input, input.length);
|
||||
|
||||
byte[] uncompressed = Snappy.uncompress(output);
|
||||
String m2 = new String(uncompressed);
|
||||
assertEquals(m, m2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void simpleUsage() throws Exception {
|
||||
|
||||
String input = "Hello snappy-java! Snappy-java is a JNI-based wrapper"
|
||||
+ " for using Snappy from Google (written in C++), a fast compresser/decompresser.";
|
||||
byte[] compressed = Snappy.compress(input.getBytes("UTF-8"));
|
||||
byte[] uncompressed = Snappy.uncompress(compressed);
|
||||
String result = new String(uncompressed, "UTF-8");
|
||||
_logger.debug(result);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void floatArray() throws Exception {
|
||||
float[] data = new float[] { 1.0f, -0.3f, 1.3f, 234.4f, 34 };
|
||||
byte[] compressed = Snappy.compress(data);
|
||||
float[] result = Snappy.uncompressFloatArray(compressed);
|
||||
assertArrayEquals(data, result, 0.0f);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void doubleArray() throws Exception {
|
||||
double[] data = new double[] { 1.0, -0.3, 1.3, 234.4, 34 };
|
||||
byte[] compressed = Snappy.compress(data);
|
||||
double[] result = Snappy.uncompressDoubleArray(compressed);
|
||||
assertArrayEquals(data, result, 0.0f);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void longArray() throws Exception {
|
||||
long[] data = new long[] { 2, 3, 15, 4234, 43251531412342342L, 23423422342L };
|
||||
byte[] compressed = Snappy.compress(data);
|
||||
long[] result = Snappy.uncompressLongArray(compressed);
|
||||
assertArrayEquals(data, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shortArray() throws Exception {
|
||||
short[] data = new short[] { 432, -32267, 1, 3, 34, 43, 34, Short.MAX_VALUE, -1 };
|
||||
byte[] compressed = Snappy.compress(data);
|
||||
short[] result = Snappy.uncompressShortArray(compressed);
|
||||
assertArrayEquals(data, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void intArray() throws Exception {
|
||||
int[] data = new int[] { 432, -32267, 1, 3, 34, 43, 34, Short.MAX_VALUE, -1, Integer.MAX_VALUE, 3424, 43 };
|
||||
byte[] compressed = Snappy.compress(data);
|
||||
int[] result = Snappy.uncompressIntArray(compressed);
|
||||
assertArrayEquals(data, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void charArray() throws Exception {
|
||||
char[] data = new char[] { 'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '!' };
|
||||
byte[] compressed = Snappy.compress(data);
|
||||
char[] result = Snappy.uncompressCharArray(compressed);
|
||||
assertArrayEquals(data, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void string() throws Exception {
|
||||
String s = "Hello Snappy! Snappy! Snappy!";
|
||||
byte[] compressed = Snappy.compress(s);
|
||||
String uncompressedString = Snappy.uncompressString(compressed);
|
||||
assertEquals(s, uncompressedString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isValidCompressedData() throws Exception {
|
||||
|
||||
byte[] b = new byte[] { (byte) 91, (byte) 34, (byte) 80, (byte) 73, (byte) 34, (byte) 93 };
|
||||
|
||||
assertFalse(Snappy.isValidCompressedBuffer(b));
|
||||
|
||||
try {
|
||||
byte[] uncompressed = Snappy.uncompress(b);
|
||||
fail("cannot reach here since the input is invalid data");
|
||||
}
|
||||
catch (IOException e) {
|
||||
_logger.debug(e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
/*--------------------------------------------------------------------------
|
||||
* 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.
|
||||
*--------------------------------------------------------------------------*/
|
||||
//--------------------------------------
|
||||
// snappy-java Project
|
||||
//
|
||||
// SnappyTest.java
|
||||
// Since: 2011/03/30
|
||||
//
|
||||
// $URL$
|
||||
// $Author$
|
||||
//--------------------------------------
|
||||
package org.xerial.snappy;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.xerial.util.log.Logger;
|
||||
|
||||
public class SnappyTest
|
||||
{
|
||||
private static Logger _logger = Logger.getLogger(SnappyTest.class);
|
||||
|
||||
@Test
|
||||
public void getVersion() throws Exception {
|
||||
String version = Snappy.getNativeLibraryVersion();
|
||||
_logger.debug("version: " + version);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void directBufferCheck() throws Exception {
|
||||
|
||||
try {
|
||||
ByteBuffer src = ByteBuffer.allocate(1024);
|
||||
src.put("hello world".getBytes());
|
||||
src.flip();
|
||||
ByteBuffer dest = ByteBuffer.allocate(1024);
|
||||
int maxCompressedLen = Snappy.compress(src, dest);
|
||||
}
|
||||
catch (SnappyError e) {
|
||||
Assert.assertTrue(e.errorCode == SnappyErrorCode.NOT_A_DIRECT_BUFFER);
|
||||
return;
|
||||
}
|
||||
|
||||
fail("shouldn't reach here");
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void directBuffer() throws Exception {
|
||||
|
||||
StringBuilder s = new StringBuilder();
|
||||
for (int i = 0; i < 20; ++i) {
|
||||
s.append("Hello world!");
|
||||
}
|
||||
String origStr = s.toString();
|
||||
byte[] orig = origStr.getBytes();
|
||||
int BUFFER_SIZE = orig.length;
|
||||
ByteBuffer src = ByteBuffer.allocateDirect(orig.length);
|
||||
src.put(orig);
|
||||
src.flip();
|
||||
_logger.debug("input size: " + src.remaining());
|
||||
int maxCompressedLen = Snappy.maxCompressedLength(src.remaining());
|
||||
_logger.debug("max compressed length:" + maxCompressedLen);
|
||||
|
||||
ByteBuffer compressed = ByteBuffer.allocateDirect(maxCompressedLen);
|
||||
int compressedSize = Snappy.compress(src, compressed);
|
||||
_logger.debug("compressed length: " + compressedSize);
|
||||
|
||||
assertTrue(Snappy.isValidCompressedBuffer(compressed));
|
||||
|
||||
assertEquals(0, src.position());
|
||||
assertEquals(orig.length, src.remaining());
|
||||
assertEquals(orig.length, src.limit());
|
||||
|
||||
assertEquals(0, compressed.position());
|
||||
assertEquals(compressedSize, compressed.limit());
|
||||
assertEquals(compressedSize, compressed.remaining());
|
||||
|
||||
int uncompressedLen = Snappy.uncompressedLength(compressed);
|
||||
_logger.debug("uncompressed length: " + uncompressedLen);
|
||||
ByteBuffer extract = ByteBuffer.allocateDirect(uncompressedLen);
|
||||
int uncompressedLen2 = Snappy.uncompress(compressed, extract);
|
||||
assertEquals(uncompressedLen, uncompressedLen2);
|
||||
assertEquals(uncompressedLen, extract.remaining());
|
||||
|
||||
byte[] b = new byte[uncompressedLen];
|
||||
extract.get(b);
|
||||
String decompressed = new String(b);
|
||||
_logger.debug(decompressed);
|
||||
|
||||
assertEquals(origStr, decompressed);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bufferOffset() throws Exception {
|
||||
|
||||
String m = "ACCAGGGGGGGGGGGGGGGGGGGGATAGATATTTCCCGAGATATTTTATATAAAAAAA";
|
||||
byte[] orig = m.getBytes();
|
||||
final int offset = 100;
|
||||
ByteBuffer input = ByteBuffer.allocateDirect(orig.length + offset);
|
||||
input.position(offset);
|
||||
input.put(orig);
|
||||
input.flip();
|
||||
input.position(offset);
|
||||
|
||||
// compress
|
||||
int maxCompressedLength = Snappy.maxCompressedLength(input.remaining());
|
||||
final int offset2 = 40;
|
||||
ByteBuffer compressed = ByteBuffer.allocateDirect(maxCompressedLength + offset2);
|
||||
compressed.position(offset2);
|
||||
Snappy.compress(input, compressed);
|
||||
assertTrue(Snappy.isValidCompressedBuffer(compressed));
|
||||
|
||||
// uncompress
|
||||
final int offset3 = 80;
|
||||
int uncompressedLength = Snappy.uncompressedLength(compressed);
|
||||
ByteBuffer uncompressed = ByteBuffer.allocateDirect(uncompressedLength + offset3);
|
||||
uncompressed.position(offset3);
|
||||
Snappy.uncompress(compressed, uncompressed);
|
||||
assertEquals(offset3, uncompressed.position());
|
||||
assertEquals(offset3 + uncompressedLength, uncompressed.limit());
|
||||
assertEquals(uncompressedLength, uncompressed.remaining());
|
||||
|
||||
// extract string
|
||||
byte[] recovered = new byte[uncompressedLength];
|
||||
uncompressed.get(recovered);
|
||||
String m2 = new String(recovered);
|
||||
|
||||
assertEquals(m, m2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void byteArrayCompress() throws Exception {
|
||||
|
||||
String m = "ACCAGGGGGGGGGGGGGGGGGGGGATAGATATTTCCCGAGATATTTTATATAAAAAAA";
|
||||
byte[] input = m.getBytes();
|
||||
byte[] output = new byte[Snappy.maxCompressedLength(input.length)];
|
||||
int compressedSize = Snappy.compress(input, 0, input.length, output, 0);
|
||||
byte[] uncompressed = new byte[input.length];
|
||||
|
||||
assertTrue(Snappy.isValidCompressedBuffer(output, 0, compressedSize));
|
||||
int uncompressedSize = Snappy.uncompress(output, 0, compressedSize, uncompressed, 0);
|
||||
String m2 = new String(uncompressed);
|
||||
assertEquals(m, m2);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void rangeCheck() throws Exception {
|
||||
String m = "ACCAGGGGGGGGGGGGGGGGGGGGATAGATATTTCCCGAGATATTTTATATAAAAAAA";
|
||||
byte[] input = m.getBytes();
|
||||
byte[] output = new byte[Snappy.maxCompressedLength(input.length)];
|
||||
int compressedSize = Snappy.compress(input, 0, input.length, output, 0);
|
||||
|
||||
assertTrue(Snappy.isValidCompressedBuffer(output, 0, compressedSize));
|
||||
// Intentionally set an invalid range
|
||||
assertFalse(Snappy.isValidCompressedBuffer(output, 0, compressedSize + 1));
|
||||
assertFalse(Snappy.isValidCompressedBuffer(output, 1, compressedSize));
|
||||
|
||||
// Test the ByteBuffer API
|
||||
ByteBuffer bin = ByteBuffer.allocateDirect(input.length);
|
||||
bin.put(input);
|
||||
bin.flip();
|
||||
ByteBuffer bout = ByteBuffer.allocateDirect(Snappy.maxCompressedLength(bin.remaining()));
|
||||
int compressedSize2 = Snappy.compress(bin, bout);
|
||||
assertEquals(compressedSize, compressedSize2);
|
||||
|
||||
assertTrue(Snappy.isValidCompressedBuffer(bout));
|
||||
// Intentionally set an invalid range
|
||||
bout.limit(bout.limit() + 1);
|
||||
assertFalse(Snappy.isValidCompressedBuffer(bout));
|
||||
bout.limit(bout.limit() - 1);
|
||||
bout.position(1);
|
||||
assertFalse(Snappy.isValidCompressedBuffer(bout));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void highLevelAPI() throws Exception {
|
||||
|
||||
String m = "Hello! 01234 ACGDSFSDFJ World. FDSDF02394234 fdsfda03924";
|
||||
byte[] input = m.getBytes();
|
||||
byte[] output = Snappy.compress(input);
|
||||
|
||||
byte[] uncompressed = Snappy.uncompress(output);
|
||||
String m2 = new String(uncompressed);
|
||||
assertEquals(m, m2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void lowLevelAPI() throws Exception {
|
||||
|
||||
String m = "Hello! 01234 ACGDSFSDFJ World. FDSDF02394234 fdsfda03924";
|
||||
byte[] input = m.getBytes();
|
||||
byte[] output = Snappy.rawCompress(input, input.length);
|
||||
|
||||
byte[] uncompressed = Snappy.uncompress(output);
|
||||
String m2 = new String(uncompressed);
|
||||
assertEquals(m, m2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void simpleUsage() throws Exception {
|
||||
|
||||
String input = "Hello snappy-java! Snappy-java is a JNI-based wrapper"
|
||||
+ " for using Snappy from Google (written in C++), a fast compresser/decompresser.";
|
||||
byte[] compressed = Snappy.compress(input.getBytes("UTF-8"));
|
||||
byte[] uncompressed = Snappy.uncompress(compressed);
|
||||
String result = new String(uncompressed, "UTF-8");
|
||||
_logger.debug(result);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void floatArray() throws Exception {
|
||||
float[] data = new float[] { 1.0f, -0.3f, 1.3f, 234.4f, 34 };
|
||||
byte[] compressed = Snappy.compress(data);
|
||||
float[] result = Snappy.uncompressFloatArray(compressed);
|
||||
assertArrayEquals(data, result, 0.0f);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void doubleArray() throws Exception {
|
||||
double[] data = new double[] { 1.0, -0.3, 1.3, 234.4, 34 };
|
||||
byte[] compressed = Snappy.compress(data);
|
||||
double[] result = Snappy.uncompressDoubleArray(compressed);
|
||||
assertArrayEquals(data, result, 0.0f);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void longArray() throws Exception {
|
||||
long[] data = new long[] { 2, 3, 15, 4234, 43251531412342342L, 23423422342L };
|
||||
byte[] compressed = Snappy.compress(data);
|
||||
long[] result = Snappy.uncompressLongArray(compressed);
|
||||
assertArrayEquals(data, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shortArray() throws Exception {
|
||||
short[] data = new short[] { 432, -32267, 1, 3, 34, 43, 34, Short.MAX_VALUE, -1 };
|
||||
byte[] compressed = Snappy.compress(data);
|
||||
short[] result = Snappy.uncompressShortArray(compressed);
|
||||
assertArrayEquals(data, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void intArray() throws Exception {
|
||||
int[] data = new int[] { 432, -32267, 1, 3, 34, 43, 34, Short.MAX_VALUE, -1, Integer.MAX_VALUE, 3424, 43 };
|
||||
byte[] compressed = Snappy.compress(data);
|
||||
int[] result = Snappy.uncompressIntArray(compressed);
|
||||
assertArrayEquals(data, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void charArray() throws Exception {
|
||||
char[] data = new char[] { 'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '!' };
|
||||
byte[] compressed = Snappy.compress(data);
|
||||
char[] result = Snappy.uncompressCharArray(compressed);
|
||||
assertArrayEquals(data, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void string() throws Exception {
|
||||
String s = "Hello Snappy! Snappy! Snappy!";
|
||||
byte[] compressed = Snappy.compress(s);
|
||||
String uncompressedString = Snappy.uncompressString(compressed);
|
||||
assertEquals(s, uncompressedString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isValidCompressedData() throws Exception {
|
||||
|
||||
byte[] b = new byte[] { (byte) 91, (byte) 34, (byte) 80, (byte) 73, (byte) 34, (byte) 93 };
|
||||
|
||||
assertFalse(Snappy.isValidCompressedBuffer(b));
|
||||
|
||||
try {
|
||||
byte[] uncompressed = Snappy.uncompress(b);
|
||||
fail("cannot reach here since the input is invalid data");
|
||||
}
|
||||
catch (IOException e) {
|
||||
_logger.debug(e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue