Merge branch 'develop'

This commit is contained in:
Taro L. Saito 2016-02-16 08:26:22 -08:00
commit fd07ae7afe
50 changed files with 4937 additions and 4097 deletions

View File

@ -1,6 +0,0 @@
#Tue Mar 29 16:49:37 JST 2011
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
org.eclipse.jdt.core.compiler.compliance=1.6
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.source=1.6

File diff suppressed because one or more lines are too long

View File

@ -1,9 +0,0 @@
#Tue Mar 29 16:49:37 JST 2011
activeProfiles=
eclipse.preferences.version=1
fullBuildGoals=process-test-resources
includeModules=false
resolveWorkspaceProjects=true
resourceFilterGoals=process-resources resources\:testResources
skipCompilerPlugin=true
version=1

View File

@ -1,6 +1,9 @@
language: scala
sudo: false
scala:
- 2.11.1
- 2.11.7
jdk:
- openjdk6
- openjdk7

View File

@ -35,7 +35,7 @@ endif
$(SNAPPY_ARCHIVE):
@mkdir -p $(@D)
curl -o$@ http://snappy.googlecode.com/files/snappy-$(VERSION).tar.gz
curl -L -o$@ https://github.com/google/snappy/releases/download/$(VERSION)/snappy-$(VERSION).tar.gz
$(SNAPPY_UNPACKED): $(SNAPPY_ARCHIVE)
$(TAR) xvfz $< -C $(TARGET)
@ -47,7 +47,7 @@ $(SNAPPY_GIT_UNPACKED):
git clone $(GIT_REPO_URL) $(SNAPPY_SRC_DIR)
git --git-dir=$(SNAPPY_SRC_DIR)/.git --work-tree=$(SNAPPY_SRC_DIR) checkout -b local/snappy-$(GIT_SNAPPY_BRANCH) $(GIT_SNAPPY_BRANCH)
touch $@
cd $(SNAPPY_SRC_DIR) && ./configure
cd $(SNAPPY_SRC_DIR) && ./autogen.sh && ./configure
jni-header: $(SRC)/org/xerial/snappy/SnappyNative.h

View File

@ -42,7 +42,7 @@ endif
# os=Default is meant to be generic unix/linux
known_os_archs := Linux-x86 Linux-x86_64 Linux-arm Linux-armhf Linux-ppc Linux-ppc64 Mac-x86 Mac-x86_64 FreeBSD-x86_64 Windows-x86 Windows-x86_64 SunOS-x86 SunOS-sparc SunOS-x86_64 AIX-ppc64
known_os_archs := Linux-x86 Linux-x86_64 Linux-arm Linux-armhf Linux-ppc Linux-ppc64 Linux-s390 Linux-s390x Mac-x86 Mac-x86_64 FreeBSD-x86_64 Windows-x86 Windows-x86_64 SunOS-x86 SunOS-sparc SunOS-x86_64 AIX-ppc AIX-ppc64
os_arch := $(OS_NAME)-$(OS_ARCH)
IBM_JDK_7 := $(findstring IBM, $(shell $(JAVA) -version 2>&1 | grep IBM | grep "JRE 1.7"))
@ -116,6 +116,17 @@ Linux-ppc64_LINKFLAGS := -shared -static-libgcc -static-libstdc++
Linux-ppc64_LIBNAME := libsnappyjava.so
Linux-ppc64_SNAPPY_FLAGS :=
AIX-ppc_CXX := g++
AIX-ppc_STRIP := strip
AIX-ppc_LIBNAME := libsnappyjava.a
ifeq ($(IBM_JDK_7),)
AIX-ppc_CXXFLAGS := -DHAVE_CONFIG_H -Ilib/inc_linux -I$(JAVA_HOME)/include -Ilib/inc_mac -O2 -fPIC -fvisibility=hidden
else
AIX-ppc_CXXFLAGS := -DHAVE_CONFIG_H -I$(JAVA_HOME)/include/aix -Ilib/inc_ibm -I$(JAVA_HOME)/include -Ilib/inc_mac -O2 -fPIC -maix32
endif
AIX-ppc_LINKFLAGS := -shared -static-libgcc -static-libstdc++ -lcrypt
AIX-ppc_SNAPPY_FLAGS :=
AIX-ppc64_CXX := g++
AIX-ppc64_STRIP := strip -X64
AIX-ppc64_LIBNAME := libsnappyjava.a
@ -127,6 +138,28 @@ endif
AIX-ppc64_LINKFLAGS := -shared -static-libgcc -static-libstdc++ -lcrypt
AIX-ppc64_SNAPPY_FLAGS :=
Linux-s390_CXX := g++
Linux-s390_STRIP := strip
ifeq ($(IBM_JDK_7),)
Linux-s390_CXXFLAGS := -DHAVE_CONFIG_H -Ilib/inc_linux -I$(JAVA_HOME)/include -Ilib/inc_mac -O2 -fPIC -fvisibility=hidden -m31
else
Linux-s390_CXXFLAGS := -DHAVE_CONFIG_H -I$(JAVA_HOME)/include/linux -Ilib/inc_ibm -I$(JAVA_HOME)/include -Ilib/inc_mac -O2 -fPIC -m31
endif
Linux-s390_LINKFLAGS := -shared -static-libgcc -static-libstdc++
Linux-s390_LIBNAME := libsnappyjava.so
Linux-s390_SNAPPY_FLAGS :=
Linux-s390x_CXX := g++
Linux-s390x_STRIP := strip
ifeq ($(IBM_JDK_7),)
Linux-s390x_CXXFLAGS := -DHAVE_CONFIG_H -Ilib/inc_linux -I$(JAVA_HOME)/include -Ilib/inc_mac -O2 -fPIC -fvisibility=hidden -m64
else
Linux-s390x_CXXFLAGS := -DHAVE_CONFIG_H -I$(JAVA_HOME)/include/linux -Ilib/inc_ibm -I$(JAVA_HOME)/include -Ilib/inc_mac -O2 -fPIC -m64
endif
Linux-s390x_LINKFLAGS := -shared -static-libgcc -static-libstdc++
Linux-s390x_LIBNAME := libsnappyjava.so
Linux-s390x_SNAPPY_FLAGS :=
SunOS-x86_CXX := g++
SunOS-x86_STRIP := strip
SunOS-x86_CXXFLAGS := -include lib/inc_linux/jni_md.h -I$(JAVA_HOME)/include -O2 -fPIC -fvisibility=hidden

View File

@ -1,5 +1,17 @@
Since vesion 1.1.0.x, Java 6 (1.6) or higher is required.
## snappy-java-1.1.2.1 (2016-01-22)
* Fixed #131
## snappy-java-1.1.2 (22 September 2015)
* This is a backward compatible release for 1.1.x.
* Add AIX (32-bit) support.
* There is no upgrade for the native libraries of the other platforms.
* A major change since 1.1.1 is a support for reading concatenated results of SnappyOutputStream(s)
## snappy-java-1.1.2-RC2 (18 May 2015)
* Fix #107: SnappyOutputStream.close() is not idempotent
## snappy-java-1.1.2-RC1 (13 May 2015)
* SnappyInputStream now supports reading concatenated compressed results of SnappyOutputStream
* There has been no compressed format change since 1.0.5.x. So You can read the compressed results interchangeablly between these versions.

View File

@ -1,31 +1,31 @@
The snappy-java is a Java port of the snappy
<http://code.google.com/p/snappy/>, a fast C++ compresser/decompresser developed by Google.
## Features
## Features
* Fast compression/decompression around 200~400MB/sec.
* Less memory usage. SnappyOutputStream uses only 32KB+ in default.
* Less memory usage. SnappyOutputStream uses only 32KB+ in default.
* JNI-based implementation to achieve comparable performance to the native C++ version.
* Although snappy-java uses JNI, it can be used safely with multiple class loaders (e.g. Tomcat, etc.).
* Compression/decompression of Java primitive arrays (`float[]`, `double[]`, `int[]`, `short[]`, `long[]`, etc.)
* Portable across various operating systems; Snappy-java contains native libraries built for Window/Mac/Linux (64-bit). snappy-java loads one of these libraries according to your machine environment (It looks system properties, `os.name` and `os.arch`).
* Simple usage. Add the snappy-java-(version).jar file to your classpath. Then call compression/decompression methods in `org.xerial.snappy.Snappy`.
* [Framing-format support](http://snappy.googlecode.com/svn/trunk/framing_format.txt) (Since 1.1.0 version)
* Portable across various operating systems; Snappy-java contains native libraries built for Window/Mac/Linux (64-bit). snappy-java loads one of these libraries according to your machine environment (It looks system properties, `os.name` and `os.arch`).
* Simple usage. Add the snappy-java-(version).jar file to your classpath. Then call compression/decompression methods in `org.xerial.snappy.Snappy`.
* [Framing-format support](https://github.com/google/snappy/blob/master/framing_format.txt) (Since 1.1.0 version)
* OSGi support
* [Apache License Version 2.0](http://www.apache.org/licenses/LICENSE-2.0). Free for both commercial and non-commercial use.
## Performance
## Performance
* Snappy's main target is very high-speed compression/decompression with reasonable compression size. So the compression ratio of snappy-java is modest and about the same as `LZF` (ranging 20%-100% according to the dataset).
* Here are some [benchmark results](https://github.com/ning/jvm-compressor-benchmark/wiki), comparing
snappy-java and the other compressors
`LZO-java`/`LZF`/`QuickLZ`/`Gzip`/`Bzip2`. Thanks [Tatu Saloranta @cotowncoder](http://twitter.com/#!/cowtowncoder) for providing the benchmark suite.
`LZO-java`/`LZF`/`QuickLZ`/`Gzip`/`Bzip2`. Thanks [Tatu Saloranta @cotowncoder](http://twitter.com/#!/cowtowncoder) for providing the benchmark suite.
* The benchmark result indicates snappy-java is the fastest compreesor/decompressor in Java: http://ning.github.com/jvm-compressor-benchmark/results/canterbury-roundtrip-2011-07-28/index.html
* The decompression speed is twice as fast as the others: http://ning.github.com/jvm-compressor-benchmark/results/canterbury-uncompress-2011-07-28/index.html
## Download
## Download
* [Release Notes](Milestone.md)
* [Release Notes](Milestone.md)
The current stable version is available from here:
* Release version: http://central.maven.org/maven2/org/xerial/snappy/snappy-java/
@ -39,7 +39,7 @@ Add the following dependency to your pom.xml:
<dependency>
<groupId>org.xerial.snappy</groupId>
<artifactId>snappy-java</artifactId>
<version>1.1.2-RC1</version>
<version>1.1.2.1</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
@ -47,11 +47,11 @@ Add the following dependency to your pom.xml:
### Using with sbt
```
libraryDependencies += "org.xerial.snappy" % "snappy-java" % "1.1.2-RC1"
libraryDependencies += "org.xerial.snappy" % "snappy-java" % "1.1.2.1"
```
## Usage
## Usage
First, import `org.xerial.snapy.Snappy` in your Java code:
```java
@ -65,24 +65,24 @@ 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);
```
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.
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.
### Stream-based API
Stream-based compressor/decompressor `SnappyOutputStream`/`SnappyInputStream` are also available for reading/writing large data sets. `SnappyFramedOutputStream`/`SnappyFramedInputStream` can be used for the [framing format](https://code.google.com/p/snappy/source/browse/trunk/framing_format.txt).
Stream-based compressor/decompressor `SnappyOutputStream`/`SnappyInputStream` are also available for reading/writing large data sets. `SnappyFramedOutputStream`/`SnappyFramedInputStream` can be used for the [framing format](https://github.com/google/snappy/blob/master/framing_format.txt).
* See also [Javadoc API](https://oss.sonatype.org/service/local/repositories/releases/archive/org/xerial/snappy/snappy-java/1.1.1.6/snappy-java-1.1.1.6-javadoc.jar/!/index.html)
* See also [Javadoc API](https://oss.sonatype.org/service/local/repositories/releases/archive/org/xerial/snappy/snappy-java/1.1.2.1/snappy-java-1.1.2.1-javadoc.jar/!/index.html)
#### Compatibility Notes
* `SnappyOutputStream` and `SnappyInputStream` use `[magic header:16 bytes]([block size:int32][compressed data:byte array])*` format. You can read the result of `Snappy.compress` with `SnappyInputStream`, but you cannot read the compressed data generated by `SnappyOutputStream` with `Snappy.uncompress`. Here is the compatibility matrix of data foramt:
| Write\Read | `Snappy.uncompress` | `SnappyInputStream` | `SnappyFramedInputStream` |
| --------------- |:-------------------:|:------------------:|:-----------------------:|
| `Snappy.compress` | ok | ok | x |
| `Snappy.compress` | ok | ok | x |
| `SnappyOutputStream` | x | ok | x |
| `SnappyFramedOutputStream` | x | x | ok |
@ -90,7 +90,7 @@ Stream-based compressor/decompressor `SnappyOutputStream`/`SnappyInputStream` ar
If you have snappy-java-(VERSION).jar in the current directory, use `-classpath` option as follows:
$ javac -classpath ".;snappy-java-(VERSION).jar" Sample.java # in Windows
or
or
$ javac -classpath ".:snappy-java-(VERSION).jar" Sample.java # in Mac or Linux
@ -99,23 +99,23 @@ If you have snappy-java-(VERSION).jar in the current directory, use `-classpath`
## Public discussion group
Post bug reports or feature request to the Issue Tracker: <https://github.com/xerial/snappy-java/issues>
Public discussion forum is here: <http://groups.google.com/group/xerial?hl=en Xerial Public Discussion Group>
Public discussion forum is here: [Xerial Public Discussion Group](http://groups.google.com/group/xerial?hl=en)
## Building from the source code
## Building from the source code
See the [installation instruction](https://github.com/xerial/snappy-java/blob/develop/INSTALL). Building from the source code is an option when your OS platform and CPU architecture is not supported. To build snappy-java, you need Git, JDK (1.6 or higher), g++ compiler (mingw in Windows) etc.
$ git clone https://github.com/xerial/snappy-java.git
$ cd snappy-java
$ make
When building on Solaris use
$ gmake
A file `target/snappy-java-$(version).jar` is the product additionally containing the native library built for your platform.
## Building linux x86_64 binary
## Building Linux x86\_64 binary
snappy-java tries to static link libstdc++ to increase the availability for various Linux versions. However, standard distributions of 64-bit Linux OS rarely provide libstdc++ compiled with `-fPIC` option. I currently uses custom g++, compiled as follows:
@ -135,6 +135,14 @@ $ make install
This g++ build enables static linking of libstdc++. For more infomation on building GCC, see GCC's home page.
## Building Linux s390/s390x binaries
Older snapshots of snappy contain a buggy config.h.in that does not work properly on some big-endian platforms like Linux on IBM z (s390/s390x). Building snappy-java on s390/s390x requires fetching the snappy source from GitHub, and processing the source with autoconf to obtain a usable config.h. On a RHEL s390x system, these steps produced a working 64-bit snappy-java build (the process should be similar for other distributions):
$ sudo yum install java-1.7.1-ibm-devel libstdc++-static-devel
$ export JAVA_HOME=/usr/lib/jvm/java-1.7.1-ibm-1.7.1.2.10-1jpp.3.el7_0.s390x
$ make USE_GIT=1 GIT_REPO_URL=https://github.com/google/snappy.git GIT_SNAPPY_BRANCH=master IBM_JDK_7=1
## Cross-compiling for other platforms
The Makefile contains rules for cross-compiling the native library for other platforms so that the snappy-java JAR can support multiple platforms. For example, to build the native libraries for x86 Linux, x86 and x86-64 Windows, and soft- and hard-float ARM:
@ -165,13 +173,21 @@ snappy-java uses sbt (simple build tool for Scala) as a build tool. Here is a si
> ~test-only * # run tests that matches a given name pattern
> publishM2 # publish jar to $HOME/.m2/repository
> package # create jar file
> findbugs # Produce findbugs report in target/findbugs
> jacoco:cover # Report the code coverage of tests to target/jacoco folder
If you need to see detailed debug messages, launch sbt with `-Dloglevel=debug` option:
```
$ ./sbt -Dloglevel=debug
```
For the details of sbt usage, see my blog post: [Building Java Projects with sbt](http://xerial.org/blog/2014/03/24/sbt/)
## Miscellaneous Notes
### Using snappy-java with Tomcat 6 (or higher) Web Server
Simply put the snappy-java's jar to WEB-INF/lib folder of your web application. Usual JNI-library specific problem no longer exists since snappy-java version 1.0.3 or higher can be loaded by multiple class loaders.
Simply put the snappy-java's jar to WEB-INF/lib folder of your web application. Usual JNI-library specific problem no longer exists since snappy-java version 1.0.3 or higher can be loaded by multiple class loaders.
----
Snappy-java is developed by [Taro L. Saito](http://www.xerial.org/leo). Twitter [@taroleo](http://twitter.com/#!/taroleo)

View File

@ -1,3 +1,4 @@
import de.johoop.findbugs4sbt.ReportType
name := "snappy-java"
@ -10,7 +11,7 @@ description := "snappy-java: A fast compression/decompression library"
sonatypeProfileName := "org.xerial"
pomExtra := {
<url>https://github.comm/xerial/snappy-java</url>
<url>https://github.com/xerial/snappy-java</url>
<licenses>
<license>
<name>The Apache Software License, Version 2.0</name>
@ -68,6 +69,14 @@ logBuffered in Test := false
incOptions := incOptions.value.withNameHashing(true)
findbugsSettings
findbugsReportType := Some(ReportType.FancyHtml)
findbugsReportPath := Some(crossTarget.value / "findbugs" / "report.html")
jacoco.settings
libraryDependencies ++= Seq(
"junit" % "junit" % "4.8.2" % "test",
"org.codehaus.plexus" % "plexus-classworlds" % "2.4" % "test",
@ -92,15 +101,21 @@ OsgiKeys.importPackage := Seq("""org.osgi.framework;version="[1.5,2)"""")
OsgiKeys.additionalHeaders := Map(
"Bundle-NativeCode" -> Seq(
"org/xerial/snappy/native/Windows/x86_64/snappyjava.dll;osname=win32;processor=x86-64",
"org/xerial/snappy/native/Windows/x86_64/snappyjava.dll;osname=win32;processor=x64",
"org/xerial/snappy/native/Windows/x86_64/snappyjava.dll;osname=win32;processor=amd64",
"org/xerial/snappy/native/Windows/x86/snappyjava.dll;osname=win32;processor=x86",
"org/xerial/snappy/native/Mac/x86/libsnappyjava.jnilib;osname=macosx;processor=x86",
"org/xerial/snappy/native/Mac/x86_64/libsnappyjava.jnilib;osname=macosx;processor=x86-64",
"org/xerial/snappy/native/Linux/x86_64/libsnappyjava.so;osname=linux;processor=x86-64",
"org/xerial/snappy/native/Linux/x86_64/libsnappyjava.so;osname=linux;processor=x64",
"org/xerial/snappy/native/Linux/x86_64/libsnappyjava.so;osname=linux;processor=amd64",
"org/xerial/snappy/native/Linux/x86/libsnappyjava.so;osname=linux;processor=x86",
"org/xerial/snappy/native/Linux/aarch64/libsnappyjava.so;osname=linux;processor=aarch64",
"org/xerial/snappy/native/Linux/arm/libsnappyjava.so;osname=linux;processor=arm",
"org/xerial/snappy/native/Linux/ppc64/libsnappyjava.so;osname=linux;processor=ppc64",
"org/xerial/snappy/native/Linux/ppc64le/libsnappyjava.so;osname=linux;processor=ppc64le",
"org/xerial/snappy/native/Linux/s390x/libsnappyjava.so;osname=linux;processor=s390x",
"org/xerial/snappy/native/AIX/ppc/libsnappyjava.a;osname=aix;processor=ppc",
"org/xerial/snappy/native/AIX/ppc64/libsnappyjava.a;osname=aix;processor=ppc64",
"org/xerial/snappy/native/SunOS/x86/libsnappyjava.so;osname=sunos;processor=x86",
"org/xerial/snappy/native/SunOS/x86_64/libsnappyjava.so;osname=sunos;processor=x86-64",
@ -131,3 +146,8 @@ releaseProcess := Seq[ReleaseStep](
ReleaseStep(action = Command.process("sonatypeReleaseAll", _)),
pushChanges
)
com.etsy.sbt.Checkstyle.checkstyleSettings
com.etsy.sbt.Checkstyle.CheckstyleTasks.checkstyleConfig := file("src/checkstyle/checks.xml")

View File

@ -1,26 +1,26 @@
/*
* Copyright (c) 1996, 2006, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
@ -1951,9 +1951,10 @@ JNI_OnUnload(JavaVM *vm, void *reserved);
#define JNI_VERSION_1_2 0x00010002
#define JNI_VERSION_1_4 0x00010004
#define JNI_VERSION_1_6 0x00010006
#define JNI_VERSION_1_8 0x00010008
#ifdef __cplusplus
} /* extern "C" */
#endif /* __cplusplus */
#endif /* !_JAVASOFT_JNI_H_ */
#endif /* !_JAVASOFT_JNI_H_ */

View File

@ -1,5 +1,5 @@
#ifndef __CONFIG_H
#define __CONFIG_H
#endif // __CONFIG_H
#ifndef __CONFIG_H
#define __CONFIG_H
#endif // __CONFIG_H

View File

@ -1,2 +1,2 @@
sbt.version=0.13.8
sbt.version=0.13.9

View File

@ -1,12 +1,14 @@
addSbtPlugin("com.github.gseitz" % "sbt-release" % "1.0.0")
addSbtPlugin("org.xerial.sbt" % "sbt-sonatype" % "0.5.0")
addSbtPlugin("org.xerial.sbt" % "sbt-sonatype" % "1.1")
addSbtPlugin("com.jsuereth" % "sbt-pgp" % "1.0.0")
addSbtPlugin("de.johoop" % "findbugs4sbt" % "1.3.0")
addSbtPlugin("de.johoop" % "findbugs4sbt" % "1.4.0")
addSbtPlugin("de.johoop" % "jacoco4sbt" % "2.1.5")
addSbtPlugin("com.typesafe.sbt" % "sbt-osgi" % "0.7.0")
addSbtPlugin("com.etsy" % "sbt-checkstyle-plugin" % "0.4.3")

364
sbt
View File

@ -1,36 +1,25 @@
#!/usr/bin/env bash
#
# A more capable sbt runner, coincidentally also called sbt.
# Author: Paul Phillips <paulp@typesafe.com>
# Author: Paul Phillips <paulp@improving.org>
# todo - make this dynamic
declare -r sbt_release_version="0.13.8"
declare -r sbt_unreleased_version="0.13.8-SNAPSHOT" # -sbt-dev doesn't work at present
declare -r sbt_unreleased_version="0.13.9-M1"
declare -r buildProps="project/build.properties"
declare sbt_jar sbt_dir sbt_create sbt_launch_dir
declare scala_version java_home sbt_explicit_version
declare verbose debug quiet noshare batch trace_level log_level
declare sbt_saved_stty
declare sbt_jar sbt_dir sbt_create sbt_version
declare scala_version sbt_explicit_version
declare verbose noshare batch trace_level log_level
declare sbt_saved_stty debugUs
echoerr () { [[ -z "$quiet" ]] && echo "$@" >&2; }
vlog () { [[ -n "$verbose$debug" ]] && echoerr "$@"; }
dlog () { [[ -n "$debug" ]] && echoerr "$@"; }
# we'd like these set before we get around to properly processing arguments
for arg in "$@"; do
case "$arg" in
-q|-quiet) quiet=true ;;
-d|-debug) debug=true ;;
-v|-verbose) verbose=true ;;
*) ;;
esac
done
echoerr () { echo >&2 "$@"; }
vlog () { [[ -n "$verbose" ]] && echoerr "$@"; }
# spaces are possible, e.g. sbt.version = 0.13.0
build_props_sbt () {
[[ -r "$buildProps" ]] && \
grep '^sbt\.version' "$buildProps" | tr '=' ' ' | awk '{ print $2; }'
grep '^sbt\.version' "$buildProps" | tr '=\r' ' ' | awk '{ print $2; }'
}
update_build_props_sbt () {
@ -41,31 +30,24 @@ update_build_props_sbt () {
perl -pi -e "s/^sbt\.version\b.*\$/sbt.version=${ver}/" "$buildProps"
grep -q '^sbt.version[ =]' "$buildProps" || printf "\nsbt.version=%s\n" "$ver" >> "$buildProps"
echoerr "!!!"
echoerr "!!! Updated file $buildProps setting sbt.version to: $ver"
echoerr "!!! Previous value was: $old"
echoerr "!!!"
vlog "!!!"
vlog "!!! Updated file $buildProps setting sbt.version to: $ver"
vlog "!!! Previous value was: $old"
vlog "!!!"
}
}
sbt_version () {
if [[ -n "$sbt_explicit_version" ]]; then
echo "$sbt_explicit_version"
else
local v="$(build_props_sbt)"
if [[ -n "$v" ]]; then
echo "$v"
else
echo "$sbt_release_version"
fi
fi
set_sbt_version () {
sbt_version="${sbt_explicit_version:-$(build_props_sbt)}"
[[ -n "$sbt_version" ]] || sbt_version=$sbt_release_version
export sbt_version
}
# restore stty settings (echo in particular)
onSbtRunnerExit() {
[[ -n "$sbt_saved_stty" ]] || return
dlog ""
dlog "restoring stty: $sbt_saved_stty"
vlog ""
vlog "restoring stty: $sbt_saved_stty"
stty "$sbt_saved_stty"
unset sbt_saved_stty
}
@ -73,7 +55,7 @@ onSbtRunnerExit() {
# save stty and trap exit, to ensure echo is reenabled if we are interrupted.
trap onSbtRunnerExit EXIT
sbt_saved_stty="$(stty -g 2>/dev/null)"
dlog "Saved stty: $sbt_saved_stty"
vlog "Saved stty: $sbt_saved_stty"
# this seems to cover the bases on OSX, and someone will
# have to tell me about the others.
@ -119,12 +101,12 @@ init_default_option_file () {
declare -r cms_opts="-XX:+CMSClassUnloadingEnabled -XX:+UseConcMarkSweepGC"
declare -r jit_opts="-XX:ReservedCodeCacheSize=256m -XX:+TieredCompilation"
declare -r default_jvm_opts="-ea -Dfile.encoding=UTF8 -Xms512m -Xmx1536m -Xss2m $jit_opts $cms_opts"
declare -r default_jvm_opts_common="-Xms512m -Xmx1536m -Xss2m $jit_opts $cms_opts"
declare -r noshare_opts="-Dsbt.global.base=project/.sbtboot -Dsbt.boot.directory=project/.boot -Dsbt.ivy.home=project/.ivy"
declare -r latest_28="2.8.2"
declare -r latest_29="2.9.3"
declare -r latest_210="2.10.5"
declare -r latest_211="2.11.6"
declare -r latest_211="2.11.7"
declare -r script_path="$(get_script_path "$BASH_SOURCE")"
declare -r script_name="${script_path##*/}"
@ -133,7 +115,7 @@ declare -r script_name="${script_path##*/}"
declare java_cmd="java"
declare sbt_opts_file="$(init_default_option_file SBT_OPTS .sbtopts)"
declare jvm_opts_file="$(init_default_option_file JVM_OPTS .jvmopts)"
declare sbt_launch_repo="http://typesafe.artifactoryonline.com/typesafe/ivy-releases"
declare sbt_launch_repo="http://repo.typesafe.com/typesafe/ivy-releases"
# pull -J and -D options to give to java.
declare -a residual_args
@ -144,14 +126,79 @@ declare -a sbt_commands
# args to jvm/sbt via files or environment variables
declare -a extra_jvm_opts extra_sbt_opts
# if set, use JAVA_HOME over java found in path
[[ -e "$JAVA_HOME/bin/java" ]] && java_cmd="$JAVA_HOME/bin/java"
addJava () {
vlog "[addJava] arg = '$1'"
java_args+=("$1")
}
addSbt () {
vlog "[addSbt] arg = '$1'"
sbt_commands+=("$1")
}
setThisBuild () {
vlog "[addBuild] args = '$@'"
local key="$1" && shift
addSbt "set $key in ThisBuild := $@"
}
addScalac () {
vlog "[addScalac] arg = '$1'"
scalac_args+=("$1")
}
addResidual () {
vlog "[residual] arg = '$1'"
residual_args+=("$1")
}
addResolver () {
addSbt "set resolvers += $1"
}
addDebugger () {
addJava "-Xdebug"
addJava "-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=$1"
}
setScalaVersion () {
[[ "$1" == *"-SNAPSHOT" ]] && addResolver 'Resolver.sonatypeRepo("snapshots")'
addSbt "++ $1"
}
setJavaHome () {
java_cmd="$1/bin/java"
setThisBuild javaHome "Some(file(\"$1\"))"
export JAVA_HOME="$1"
export JDK_HOME="$1"
export PATH="$JAVA_HOME/bin:$PATH"
}
setJavaHomeQuietly () {
addSbt warn
setJavaHome "$1"
addSbt info
}
# if set, use JDK_HOME/JAVA_HOME over java found in path
if [[ -e "$JDK_HOME/lib/tools.jar" ]]; then
setJavaHomeQuietly "$JDK_HOME"
elif [[ -e "$JAVA_HOME/bin/java" ]]; then
setJavaHomeQuietly "$JAVA_HOME"
fi
# directory to store sbt launchers
declare sbt_launch_dir="$HOME/.sbt/launchers"
[[ -d "$sbt_launch_dir" ]] || mkdir -p "$sbt_launch_dir"
[[ -w "$sbt_launch_dir" ]] || sbt_launch_dir="$(mktemp -d -t sbt_extras_launchers.XXXXXX)"
java_version () {
local version=$("$java_cmd" -version 2>&1 | grep -E -e '(java|openjdk) version' | awk '{ print $3 }' | tr -d \")
vlog "Detected Java version: $version"
echo "${version:2:1}"
}
# MaxPermSize critical on pre-8 jvms but incurs noisy warning on 8+
default_jvm_opts () {
local v="$(java_version)"
if [[ $v -ge 8 ]]; then
echo "$default_jvm_opts_common"
else
echo "-XX:MaxPermSize=384m $default_jvm_opts_common"
fi
}
build_props_scala () {
if [[ -r "$buildProps" ]]; then
versionLine="$(grep '^build.scala.versions' "$buildProps")"
@ -162,22 +209,20 @@ build_props_scala () {
execRunner () {
# print the arguments one to a line, quoting any containing spaces
[[ "$verbose" || "$debug" ]] && echo "# Executing command line:" && {
vlog "# Executing command line:" && {
for arg; do
if [[ -n "$arg" ]]; then
if printf "%s\n" "$arg" | grep -q ' '; then
printf "\"%s\"\n" "$arg"
printf >&2 "\"%s\"\n" "$arg"
else
printf "%s\n" "$arg"
printf >&2 "%s\n" "$arg"
fi
fi
done
echo ""
vlog ""
}
if [[ -n "$batch" ]]; then
exec </dev/null
fi
[[ -n "$batch" ]] && exec </dev/null
exec "$@"
}
@ -193,13 +238,13 @@ download_url () {
local url="$1"
local jar="$2"
echo "Downloading sbt launcher for $(sbt_version):"
echo " From $url"
echo " To $jar"
echoerr "Downloading sbt launcher for $sbt_version:"
echoerr " From $url"
echoerr " To $jar"
mkdir -p "${jar%/*}" && {
if which curl >/dev/null; then
curl --fail --silent "$url" --output "$jar"
curl --fail --silent --location "$url" --output "$jar"
elif which wget >/dev/null; then
wget --quiet -O "$jar" "$url"
fi
@ -207,9 +252,8 @@ download_url () {
}
acquire_sbt_jar () {
for_sbt_version="$(sbt_version)"
sbt_url="$(jar_url "$for_sbt_version")"
sbt_jar="$(jar_file "$for_sbt_version")"
sbt_url="$(jar_url "$sbt_version")"
sbt_jar="$(jar_file "$sbt_version")"
[[ -r "$sbt_jar" ]] || download_url "$sbt_url" "$sbt_jar"
}
@ -218,11 +262,23 @@ usage () {
cat <<EOM
Usage: $script_name [options]
Note that options which are passed along to sbt begin with -- whereas
options to this runner use a single dash. Any sbt command can be scheduled
to run first by prefixing the command with --, so --warn, --error and so on
are not special.
Output filtering: if there is a file in the home directory called .sbtignore
and this is not an interactive sbt session, the file is treated as a list of
bash regular expressions. Output lines which match any regex are not echoed.
One can see exactly which lines would have been suppressed by starting this
runner with the -x option.
-h | -help print this message
-v | -verbose this runner is chattier
-d | -debug set sbt log level to Debug
-q | -quiet set sbt log level to Error
-v verbose operation (this runner is chattier)
-d, -w, -q aliases for --debug, --warn, --error (q means quiet)
-x debug this script
-trace <level> display stack traces with a max of <level> frames (default: -1, traces suppressed)
-debug-inc enable debugging log for the incremental compiler
-no-colors disable ANSI color codes
-sbt-create start sbt even if current directory contains no sbt project
-sbt-dir <path> path to global settings/plugins directory (default: ~/.sbt/<version>)
@ -234,12 +290,12 @@ Usage: $script_name [options]
-batch Disable interactive mode
-prompt <expr> Set the sbt prompt; in expr, 's' is the State and 'e' is Extracted
# sbt version (default: from $buildProps if present, else latest release)
!!! The only way to accomplish this pre-0.12.0 if there is a build.properties file which
!!! contains an sbt.version property is to update the file on disk. That's what this does.
# sbt version (default: sbt.version from $buildProps if present, otherwise $sbt_release_version)
-sbt-force-latest force the use of the latest release of sbt: $sbt_release_version
-sbt-version <version> use the specified version of sbt (default: $sbt_release_version)
-sbt-dev use the latest pre-release version of sbt: $sbt_unreleased_version
-sbt-jar <path> use the specified jar as the sbt launcher
-sbt-launch-dir <path> directory to hold sbt launchers (default: $sbt_launch_dir)
-sbt-launch-dir <path> directory to hold sbt launchers (default: ~/.sbt/launchers)
-sbt-launch-repo <url> repo url for downloading sbt launcher jar (default: $sbt_launch_repo)
# scala version (default: as chosen by sbt)
@ -256,7 +312,7 @@ Usage: $script_name [options]
# passing options to the jvm - note it does NOT use JAVA_OPTS due to pollution
# The default set is used if JVM_OPTS is unset and no -jvm-opts file is found
<default> $default_jvm_opts
<default> $(default_jvm_opts)
JVM_OPTS environment variable holding either the jvm args directly, or
the reference to a file containing jvm args if given path is prepended by '@' (e.g. '@/etc/jvmopts')
Note: "@"-file is overridden by local '.jvmopts' or '-jvm-opts' argument.
@ -273,34 +329,6 @@ Usage: $script_name [options]
EOM
}
addJava () {
dlog "[addJava] arg = '$1'"
java_args=( "${java_args[@]}" "$1" )
}
addSbt () {
dlog "[addSbt] arg = '$1'"
sbt_commands=( "${sbt_commands[@]}" "$1" )
}
addScalac () {
dlog "[addScalac] arg = '$1'"
scalac_args=( "${scalac_args[@]}" "$1" )
}
addResidual () {
dlog "[residual] arg = '$1'"
residual_args=( "${residual_args[@]}" "$1" )
}
addResolver () {
addSbt "set resolvers += $1"
}
addDebugger () {
addJava "-Xdebug"
addJava "-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=$1"
}
setScalaVersion () {
[[ "$1" == *"-SNAPSHOT" ]] && addResolver 'Resolver.sonatypeRepo("snapshots")'
addSbt "++ $1"
}
process_args ()
{
require_arg () {
@ -314,45 +342,50 @@ process_args ()
}
while [[ $# -gt 0 ]]; do
case "$1" in
-h|-help) usage; exit 1 ;;
-v|-verbose) verbose=true && log_level=Info && shift ;;
-d|-debug) debug=true && log_level=Debug && shift ;;
-q|-quiet) quiet=true && log_level=Error && shift ;;
-h|-help) usage; exit 1 ;;
-v) verbose=true && shift ;;
-d) addSbt "--debug" && addSbt debug && shift ;;
-w) addSbt "--warn" && addSbt warn && shift ;;
-q) addSbt "--error" && addSbt error && shift ;;
-x) debugUs=true && shift ;;
-trace) require_arg integer "$1" "$2" && trace_level="$2" && shift 2 ;;
-ivy) require_arg path "$1" "$2" && addJava "-Dsbt.ivy.home=$2" && shift 2 ;;
-no-colors) addJava "-Dsbt.log.noformat=true" && shift ;;
-no-share) noshare=true && shift ;;
-sbt-boot) require_arg path "$1" "$2" && addJava "-Dsbt.boot.directory=$2" && shift 2 ;;
-sbt-dir) require_arg path "$1" "$2" && sbt_dir="$2" && shift 2 ;;
-debug-inc) addJava "-Dxsbt.inc.debug=true" && shift ;;
-offline) addSbt "set offline := true" && shift ;;
-jvm-debug) require_arg port "$1" "$2" && addDebugger "$2" && shift 2 ;;
-batch) batch=true && shift ;;
-prompt) require_arg "expr" "$1" "$2" && setThisBuild shellPrompt "(s => { val e = Project.extract(s) ; $2 })" && shift 2 ;;
-trace) require_arg integer "$1" "$2" && trace_level="$2" && shift 2 ;;
-ivy) require_arg path "$1" "$2" && addJava "-Dsbt.ivy.home=$2" && shift 2 ;;
-no-colors) addJava "-Dsbt.log.noformat=true" && shift ;;
-no-share) noshare=true && shift ;;
-sbt-boot) require_arg path "$1" "$2" && addJava "-Dsbt.boot.directory=$2" && shift 2 ;;
-sbt-dir) require_arg path "$1" "$2" && sbt_dir="$2" && shift 2 ;;
-debug-inc) addJava "-Dxsbt.inc.debug=true" && shift ;;
-offline) addSbt "set offline := true" && shift ;;
-jvm-debug) require_arg port "$1" "$2" && addDebugger "$2" && shift 2 ;;
-batch) batch=true && shift ;;
-prompt) require_arg "expr" "$1" "$2" && addSbt "set shellPrompt in ThisBuild := (s => { val e = Project.extract(s) ; $2 })" && shift 2 ;;
-sbt-create) sbt_create=true && shift ;;
-sbt-jar) require_arg path "$1" "$2" && sbt_jar="$2" && shift 2 ;;
-sbt-version) require_arg version "$1" "$2" && sbt_explicit_version="$2" && shift 2 ;;
-sbt-force-latest) sbt_explicit_version="$sbt_release_version" && shift ;;
-sbt-dev) sbt_explicit_version="$sbt_unreleased_version" && shift ;;
-sbt-launch-dir) require_arg path "$1" "$2" && sbt_launch_dir="$2" && shift 2 ;;
-sbt-launch-repo) require_arg path "$1" "$2" && sbt_launch_repo="$2" && shift 2 ;;
-scala-version) require_arg version "$1" "$2" && setScalaVersion "$2" && shift 2 ;;
-binary-version) require_arg version "$1" "$2" && setThisBuild scalaBinaryVersion "\"$2\"" && shift 2 ;;
-scala-home) require_arg path "$1" "$2" && setThisBuild scalaHome "Some(file(\"$2\"))" && shift 2 ;;
-java-home) require_arg path "$1" "$2" && setJavaHome "$2" && shift 2 ;;
-sbt-opts) require_arg path "$1" "$2" && sbt_opts_file="$2" && shift 2 ;;
-jvm-opts) require_arg path "$1" "$2" && jvm_opts_file="$2" && shift 2 ;;
-sbt-create) sbt_create=true && shift ;;
-sbt-jar) require_arg path "$1" "$2" && sbt_jar="$2" && shift 2 ;;
-sbt-version) require_arg version "$1" "$2" && sbt_explicit_version="$2" && shift 2 ;;
-sbt-dev) sbt_explicit_version="$sbt_unreleased_version" && shift ;;
-sbt-launch-dir) require_arg path "$1" "$2" && sbt_launch_dir="$2" && shift 2 ;;
-sbt-launch-repo) require_arg path "$1" "$2" && sbt_launch_repo="$2" && shift 2 ;;
-scala-version) require_arg version "$1" "$2" && setScalaVersion "$2" && shift 2 ;;
-binary-version) require_arg version "$1" "$2" && addSbt "set scalaBinaryVersion in ThisBuild := \"$2\"" && shift 2 ;;
-scala-home) require_arg path "$1" "$2" && addSbt "set every scalaHome := Some(file(\"$2\"))" && shift 2 ;;
-java-home) require_arg path "$1" "$2" && java_cmd="$2/bin/java" && shift 2 ;;
-sbt-opts) require_arg path "$1" "$2" && sbt_opts_file="$2" && shift 2 ;;
-jvm-opts) require_arg path "$1" "$2" && jvm_opts_file="$2" && shift 2 ;;
-D*) addJava "$1" && shift ;;
-J*) addJava "${1:2}" && shift ;;
-S*) addScalac "${1:2}" && shift ;;
-28) setScalaVersion "$latest_28" && shift ;;
-29) setScalaVersion "$latest_29" && shift ;;
-210) setScalaVersion "$latest_210" && shift ;;
-211) setScalaVersion "$latest_211" && shift ;;
-D*) addJava "$1" && shift ;;
-J*) addJava "${1:2}" && shift ;;
-S*) addScalac "${1:2}" && shift ;;
-28) setScalaVersion "$latest_28" && shift ;;
-29) setScalaVersion "$latest_29" && shift ;;
-210) setScalaVersion "$latest_210" && shift ;;
-211) setScalaVersion "$latest_211" && shift ;;
*) addResidual "$1" && shift ;;
--debug) addSbt debug && addResidual "$1" && shift ;;
--warn) addSbt warn && addResidual "$1" && shift ;;
--error) addSbt error && addResidual "$1" && shift ;;
*) addResidual "$1" && shift ;;
esac
done
}
@ -379,21 +412,20 @@ else
vlog "No extra sbt options have been defined"
fi
[[ -n "$extra_sbt_opts" ]] && process_args "${extra_sbt_opts[@]}"
[[ -n "${extra_sbt_opts[*]}" ]] && process_args "${extra_sbt_opts[@]}"
# reset "$@" to the residual args
set -- "${residual_args[@]}"
argumentCount=$#
# set sbt version
set_sbt_version
# only exists in 0.12+
setTraceLevel() {
case "$(sbt_version)" in
"0.7."* | "0.10."* | "0.11."* )
echoerr "Cannot set trace level in sbt version $(sbt_version)"
;;
*)
addSbt "set every traceLevel := $trace_level"
;;
case "$sbt_version" in
"0.7."* | "0.10."* | "0.11."* ) echoerr "Cannot set trace level in sbt version $sbt_version" ;;
*) setThisBuild traceLevel $trace_level ;;
esac
}
@ -402,9 +434,9 @@ setTraceLevel() {
# Update build.properties on disk to set explicit version - sbt gives us no choice
[[ -n "$sbt_explicit_version" ]] && update_build_props_sbt "$sbt_explicit_version"
vlog "Detected sbt version $(sbt_version)"
vlog "Detected sbt version $sbt_version"
[[ -n "$scala_version" ]] && echoerr "Overriding scala version to $scala_version"
[[ -n "$scala_version" ]] && vlog "Overriding scala version to $scala_version"
# no args - alert them there's stuff in here
(( argumentCount > 0 )) || {
@ -438,10 +470,10 @@ if [[ -n "$noshare" ]]; then
addJava "$opt"
done
else
case "$(sbt_version)" in
case "$sbt_version" in
"0.7."* | "0.10."* | "0.11."* | "0.12."* )
[[ -n "$sbt_dir" ]] || {
sbt_dir="$HOME/.sbt/$(sbt_version)"
sbt_dir="$HOME/.sbt/$sbt_version"
vlog "Using $sbt_dir as sbt dir, -sbt-dir to override."
}
;;
@ -460,22 +492,52 @@ elif [[ -n "$JVM_OPTS" && ! ("$JVM_OPTS" =~ ^@.*) ]]; then
extra_jvm_opts=( $JVM_OPTS )
else
vlog "Using default jvm options"
extra_jvm_opts=( $default_jvm_opts )
extra_jvm_opts=( $(default_jvm_opts) )
fi
# traceLevel is 0.12+
[[ -n "$trace_level" ]] && setTraceLevel
main () {
execRunner "$java_cmd" \
"${extra_jvm_opts[@]}" \
"${java_args[@]}" \
-jar "$sbt_jar" \
"${sbt_commands[@]}" \
"${residual_args[@]}"
}
if [[ -n "$log_level" ]] && [[ "$log_level" != Info ]]; then
sbt_commands=("set logLevel in Global := Level.$log_level" "${sbt_commands[@]}")
fi
# sbt inserts this string on certain lines when formatting is enabled:
# val OverwriteLine = "\r\u001BM\u001B[2K"
# ...in order not to spam the console with a million "Resolving" lines.
# Unfortunately that makes it that much harder to work with when
# we're not going to print those lines anyway. We strip that bit of
# line noise, but leave the other codes to preserve color.
mainFiltered () {
local ansiOverwrite='\r\x1BM\x1B[2K'
local excludeRegex=$(egrep -v '^#|^$' ~/.sbtignore | paste -sd'|' -)
echoLine () {
local line="$1"
local line1="$(echo "$line" | sed -r 's/\r\x1BM\x1B\[2K//g')" # This strips the OverwriteLine code.
local line2="$(echo "$line1" | sed -r 's/\x1B\[[0-9;]*[JKmsu]//g')" # This strips all codes - we test regexes against this.
if [[ $line2 =~ $excludeRegex ]]; then
[[ -n $debugUs ]] && echo "[X] $line1"
else
[[ -n $debugUs ]] && echo " $line1" || echo "$line1"
fi
}
echoLine "Starting sbt with output filtering enabled."
main | while read -r line; do echoLine "$line"; done
}
# Only filter if there's a filter file and we don't see a known interactive command.
# Obviously this is super ad hoc but I don't know how to improve on it. Testing whether
# stdin is a terminal is useless because most of my use cases for this filtering are
# exactly when I'm at a terminal, running sbt non-interactively.
shouldFilter () { [[ -f ~/.sbtignore ]] && ! egrep -q '\b(shell|console|consoleProject)\b' <<<"${residual_args[@]}"; }
# run sbt
execRunner "$java_cmd" \
"${extra_jvm_opts[@]}" \
"${java_args[@]}" \
-jar "$sbt_jar" \
"${sbt_commands[@]}" \
"${residual_args[@]}"
if shouldFilter; then mainFiltered; else main; fi

117
src/checkstyle/checks.xml Normal file
View File

@ -0,0 +1,117 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE module PUBLIC
"-//Puppy Crawl//DTD Check Configuration 1.3//EN"
"http://www.puppycrawl.com/dtds/configuration_1_3.dtd">
<module name="Checker">
<module name="FileTabCharacter"/>
<module name="NewlineAtEndOfFile">
<property name="lineSeparator" value="lf"/>
</module>
<module name="RegexpMultiline">
<property name="format" value="\r"/>
<property name="message" value="Line contains carriage return"/>
</module>
<module name="RegexpMultiline">
<property name="format" value=" \n"/>
<property name="message" value="Line has trailing whitespace"/>
</module>
<module name="RegexpMultiline">
<property name="format" value="\{\n\n"/>
<property name="message" value="Blank line after opening brace"/>
</module>
<module name="RegexpMultiline">
<property name="format" value="\n\n\}"/>
<property name="message" value="Blank line before closing brace"/>
</module>
<module name="RegexpMultiline">
<property name="format" value="\n\n\n"/>
<property name="message" value="Multiple consecutive blank lines"/>
</module>
<module name="RegexpMultiline">
<property name="format" value="\n\n\Z"/>
<property name="message" value="Blank line before end of file"/>
</module>
<module name="TreeWalker">
<module name="EmptyBlock">
<property name="option" value="text"/>
<property name="tokens" value="
LITERAL_DO, LITERAL_ELSE, LITERAL_FINALLY, LITERAL_IF,
LITERAL_FOR, LITERAL_TRY, LITERAL_WHILE, INSTANCE_INIT, STATIC_INIT"/>
</module>
<module name="EmptyStatement"/>
<module name="EmptyForInitializerPad"/>
<module name="EmptyForIteratorPad">
<property name="option" value="space"/>
</module>
<module name="MethodParamPad">
<property name="allowLineBreaks" value="true"/>
<property name="option" value="nospace"/>
</module>
<module name="ParenPad"/>
<module name="TypecastParenPad"/>
<module name="NeedBraces"/>
<module name="LeftCurly">
<property name="option" value="nl"/>
<property name="tokens" value="CLASS_DEF, CTOR_DEF, INTERFACE_DEF, METHOD_DEF"/>
</module>
<module name="LeftCurly">
<property name="option" value="eol"/>
<property name="tokens" value="
LITERAL_CATCH, LITERAL_DO, LITERAL_ELSE, LITERAL_FINALLY, LITERAL_FOR,
LITERAL_IF, LITERAL_SWITCH, LITERAL_SYNCHRONIZED, LITERAL_TRY, LITERAL_WHILE"/>
</module>
<module name="RightCurly">
<property name="option" value="alone"/>
</module>
<module name="GenericWhitespace"/>
<module name="WhitespaceAfter"/>
<module name="NoWhitespaceBefore"/>
<module name="UpperEll"/>
<module name="DefaultComesLast"/>
<module name="ArrayTypeStyle"/>
<module name="MultipleVariableDeclarations"/>
<module name="ModifierOrder"/>
<module name="OneStatementPerLine"/>
<module name="StringLiteralEquality"/>
<module name="MutableException"/>
<module name="EqualsHashCode"/>
<module name="InnerAssignment"/>
<module name="InterfaceIsType"/>
<module name="HideUtilityClassConstructor"/>
<module name="MemberName"/>
<module name="LocalVariableName"/>
<module name="LocalFinalVariableName"/>
<module name="TypeName"/>
<module name="PackageName"/>
<module name="ParameterName"/>
<module name="StaticVariableName"/>
<module name="ClassTypeParameterName">
<property name="format" value="^[A-Z][0-9]?$"/>
</module>
<module name="MethodTypeParameterName">
<property name="format" value="^[A-Z][0-9]?$"/>
</module>
<module name="AvoidStarImport"/>
<module name="RedundantImport"/>
<module name="UnusedImports"/>
<module name="WhitespaceAround">
<property name="allowEmptyConstructors" value="true"/>
<property name="allowEmptyMethods" value="true"/>
<property name="ignoreEnhancedForColon" value="false"/>
<property name="tokens" value="
ASSIGN, BAND, BAND_ASSIGN, BOR, BOR_ASSIGN, BSR, BSR_ASSIGN,
BXOR, BXOR_ASSIGN, COLON, DIV, DIV_ASSIGN, EQUAL, GE, GT, LAND, LE,
LITERAL_ASSERT, LITERAL_CATCH, LITERAL_DO, LITERAL_ELSE,
LITERAL_FINALLY, LITERAL_FOR, LITERAL_IF, LITERAL_RETURN,
LITERAL_SYNCHRONIZED, LITERAL_TRY, LITERAL_WHILE,
LOR, LT, MINUS, MINUS_ASSIGN, MOD, MOD_ASSIGN, NOT_EQUAL,
PLUS, PLUS_ASSIGN, QUESTION, SL, SLIST, SL_ASSIGN, SR, SR_ASSIGN,
STAR, STAR_ASSIGN, TYPE_EXTENSION_AND"/>
</module>
</module>
</module>

View File

@ -30,9 +30,8 @@ import java.util.Locale;
/**
* Provides OS name and architecture name.
*
*
* @author leo
*
*/
public class OSInfo
{
@ -44,6 +43,8 @@ public class OSInfo
public static final String IA64 = "ia64";
public static final String PPC = "ppc";
public static final String PPC64 = "ppc64";
public static final String IBMZ = "s390";
public static final String IBMZ_64 = "s390x";
static {
// x86 mappings
@ -76,15 +77,21 @@ public class OSInfo
archMapping.put("power_rs", PPC);
// TODO: PowerPC 64bit mappings
archMapping.put(PPC64, PPC64);
archMapping.put("power64", PPC64);
archMapping.put("powerpc64", PPC64);
archMapping.put("power_pc64", PPC64);
archMapping.put("power_rs64", PPC64);
archMapping.put(PPC64, PPC64);
archMapping.put("power64", PPC64);
archMapping.put("powerpc64", PPC64);
archMapping.put("power_pc64", PPC64);
archMapping.put("power_rs64", PPC64);
// IBM z mappings
archMapping.put(IBMZ, IBMZ);
// IBM z 64-bit mappings
archMapping.put(IBMZ_64, IBMZ_64);
}
public static void main(String[] args) {
public static void main(String[] args)
{
if (args.length >= 1) {
if ("--os".equals(args[0])) {
System.out.print(getOSName());
@ -99,27 +106,31 @@ public class OSInfo
System.out.print(getNativeLibFolderPathForCurrentOS());
}
public static String getNativeLibFolderPathForCurrentOS() {
public static String getNativeLibFolderPathForCurrentOS()
{
return getOSName() + "/" + getArchName();
}
public static String getOSName() {
public static String getOSName()
{
return translateOSNameToFolderName(System.getProperty("os.name"));
}
public static String getArchName() {
public static String getArchName()
{
// if running Linux on ARM, need to determine ABI of JVM
String osArch = System.getProperty("os.arch");
if (osArch.startsWith("arm") && System.getProperty("os.name").contains("Linux")) {
String javaHome = System.getProperty("java.home");
try {
// determine if first JVM found uses ARM hard-float ABI
String[] cmdarray = { "/bin/sh", "-c", "find '" + javaHome +
"' -name 'libjvm.so' | head -1 | xargs readelf -A | " +
"grep 'Tag_ABI_VFP_args: VFP registers'" };
String[] cmdarray = {"/bin/sh", "-c", "find '" + javaHome +
"' -name 'libjvm.so' | head -1 | xargs readelf -A | " +
"grep 'Tag_ABI_VFP_args: VFP registers'"};
int exitCode = Runtime.getRuntime().exec(cmdarray).waitFor();
if (exitCode == 0)
if (exitCode == 0) {
return "armhf";
}
}
catch (IOException e) {
// ignored: fall back to "arm" arch (soft-float ABI)
@ -127,16 +138,18 @@ public class OSInfo
catch (InterruptedException e) {
// ignored: fall back to "arm" arch (soft-float ABI)
}
}
}
else {
String lc = osArch.toLowerCase(Locale.US);
if(archMapping.containsKey(lc))
if (archMapping.containsKey(lc)) {
return archMapping.get(lc);
}
}
return translateArchNameToFolderName(osArch);
}
static String translateOSNameToFolderName(String osName) {
static String translateOSNameToFolderName(String osName)
{
if (osName.contains("Windows")) {
return "Windows";
}
@ -146,16 +159,16 @@ public class OSInfo
else if (osName.contains("Linux")) {
return "Linux";
}
else if (osName.contains("AIX")) {
return "AIX";
}
else {
else if (osName.contains("AIX")) {
return "AIX";
}
else {
return osName.replaceAll("\\W", "");
}
}
static String translateArchNameToFolderName(String archName) {
static String translateArchNameToFolderName(String archName)
{
return archName.replaceAll("\\W", "");
}
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -1,67 +1,68 @@
/*--------------------------------------------------------------------------
* 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();
}
}
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();
}
}

View File

@ -1,124 +1,134 @@
/*--------------------------------------------------------------------------
* 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/>
* <p>
* The magic header is the following 8 bytes data:
* <p/>
* <pre>
* -126, 'S', 'N', 'A', 'P', 'P', 'Y', 0
* </pre>
* <p/>
* </p>
*
* @author leo
*/
public class SnappyCodec
{
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);
static {
assert (MAGIC_HEADER_HEAD < 0);
}
public static final int DEFAULT_VERSION = 1;
public static final int MINIMUM_COMPATIBLE_VERSION = 1;
public static final SnappyCodec currentHeader = new SnappyCodec(MAGIC_HEADER, DEFAULT_VERSION, MINIMUM_COMPATIBLE_VERSION);
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();
}
public static byte[] getMagicHeader()
{
return MAGIC_HEADER.clone();
}
@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);
}
}

View File

@ -1,62 +1,65 @@
/*--------------------------------------------------------------------------
* 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());
}
}

View File

@ -1,64 +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
//
// 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();
}
}

View File

@ -1,74 +1,82 @@
/*--------------------------------------------------------------------------
* 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
*
* @author leo
* @deprecated Snappy-java now uses {@link IOException}
*/
@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());
}
}

View File

@ -1,186 +1,187 @@
/*
* 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:
* <p/>
* <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);
}
}
}
}

View File

@ -28,12 +28,15 @@ import java.util.Arrays;
* href="http://snappy.googlecode.com/svn/trunk/framing_format.txt"
* >x-snappy-framed</a> as an {@link InputStream} and
* {@link ReadableByteChannel}.
*
*
* @author Brett Okken
* @since 1.1.0
*/
public final class SnappyFramedInputStream extends InputStream implements
ReadableByteChannel {
public final class SnappyFramedInputStream
extends InputStream
implements
ReadableByteChannel
{
private final ReadableByteChannel rbc;
private final ByteBuffer frameHeader;
@ -77,51 +80,51 @@ public final class SnappyFramedInputStream extends InputStream implements
/**
* Creates a Snappy input stream to read data from the specified underlying
* input stream.
*
* @param in
* the underlying input stream. Must not be {@code null}.
*
* @param in the underlying input stream. Must not be {@code null}.
*/
public SnappyFramedInputStream(InputStream in) throws IOException {
public SnappyFramedInputStream(InputStream in)
throws IOException
{
this(in, true);
}
/**
* Creates a Snappy input stream to read data from the specified underlying
* input stream.
*
* @param in
* the underlying input stream. Must not be {@code null}.
* @param verifyChecksums
* if true, checksums in input stream will be verified
*
* @param in the underlying input stream. Must not be {@code null}.
* @param verifyChecksums if true, checksums in input stream will be verified
*/
public SnappyFramedInputStream(InputStream in, boolean verifyChecksums)
throws IOException {
throws IOException
{
this(Channels.newChannel(in), verifyChecksums);
}
/**
* Creates a Snappy input stream to read data from the specified underlying
* channel.
*
* @param in
* the underlying readable channel. Must not be {@code null}.
*
* @param in the underlying readable channel. Must not be {@code null}.
*/
public SnappyFramedInputStream(ReadableByteChannel in)
throws IOException {
throws IOException
{
this(in, true);
}
/**
* Creates a Snappy input stream to read data from the specified underlying
* channel.
*
* @param in
* the underlying readable channel. Must not be {@code null}.
* @param verifyChecksums
* if true, checksums in input stream will be verified
*
* @param in the underlying readable channel. Must not be {@code null}.
* @param verifyChecksums if true, checksums in input stream will be verified
*/
public SnappyFramedInputStream(ReadableByteChannel in,
boolean verifyChecksums) throws IOException {
boolean verifyChecksums)
throws IOException
{
if (in == null) {
throw new NullPointerException("in is null");
}
@ -150,16 +153,17 @@ public final class SnappyFramedInputStream extends InputStream implements
/**
* @param size
*/
private void allocateBuffersBasedOnSize(int size) {
private void allocateBuffersBasedOnSize(int size)
{
if (input != null) {
releaseDirectByteBuffer(input);
}
if (uncompressedDirect != null) {
releaseDirectByteBuffer(uncompressedDirect);
}
input = ByteBuffer.allocateDirect(size);
final int maxCompressedLength = Snappy.maxCompressedLength(size);
uncompressedDirect = ByteBuffer.allocateDirect(maxCompressedLength);
@ -167,7 +171,9 @@ public final class SnappyFramedInputStream extends InputStream implements
}
@Override
public int read() throws IOException {
public int read()
throws IOException
{
if (closed) {
return -1;
}
@ -178,7 +184,9 @@ public final class SnappyFramedInputStream extends InputStream implements
}
@Override
public int read(byte[] output, int offset, int length) throws IOException {
public int read(byte[] output, int offset, int length)
throws IOException
{
if (output == null) {
throw new IllegalArgumentException("output is null");
@ -207,7 +215,9 @@ public final class SnappyFramedInputStream extends InputStream implements
}
@Override
public int available() throws IOException {
public int available()
throws IOException
{
if (closed) {
return 0;
}
@ -218,7 +228,8 @@ public final class SnappyFramedInputStream extends InputStream implements
* {@inheritDoc}
*/
@Override
public boolean isOpen() {
public boolean isOpen()
{
return !closed;
}
@ -226,7 +237,9 @@ public final class SnappyFramedInputStream extends InputStream implements
* {@inheritDoc}
*/
@Override
public int read(ByteBuffer dst) throws IOException {
public int read(ByteBuffer dst)
throws IOException
{
if (dst == null) {
throw new IllegalArgumentException("dst is null");
@ -259,14 +272,15 @@ public final class SnappyFramedInputStream extends InputStream implements
* Any calls after the source has been exhausted will result in a return
* value of {@code 0}.
* </p>
*
* @param os
* The destination to write decompressed content to.
*
* @param os The destination to write decompressed content to.
* @return The number of bytes transferred.
* @throws IOException
* @since 1.1.1
*/
public long transferTo(OutputStream os) throws IOException {
public long transferTo(OutputStream os)
throws IOException
{
if (os == null) {
throw new IllegalArgumentException("os is null");
}
@ -291,21 +305,22 @@ public final class SnappyFramedInputStream extends InputStream implements
* Transfers the entire content of this {@link ReadableByteChannel} to
* <i>wbc</i>. This potentially limits the amount of buffering required to
* decompress content.
*
* <p/>
* <p>
* Unlike {@link #read(ByteBuffer)}, this method does not need to be called
* multiple times. A single call will transfer all available content. Any
* calls after the source has been exhausted will result in a return value
* of {@code 0}.
* </p>
*
* @param wbc
* The destination to write decompressed content to.
*
* @param wbc The destination to write decompressed content to.
* @return The number of bytes transferred.
* @throws IOException
* @since 1.1.1
*/
public long transferTo(WritableByteChannel wbc) throws IOException {
public long transferTo(WritableByteChannel wbc)
throws IOException
{
if (wbc == null) {
throw new IllegalArgumentException("wbc is null");
}
@ -335,10 +350,13 @@ public final class SnappyFramedInputStream extends InputStream implements
}
@Override
public void close() throws IOException {
public void close()
throws IOException
{
try {
rbc.close();
} finally {
}
finally {
if (!closed) {
closed = true;
}
@ -346,18 +364,20 @@ public final class SnappyFramedInputStream extends InputStream implements
if (input != null) {
releaseDirectByteBuffer(input);
}
if (uncompressedDirect != null) {
releaseDirectByteBuffer(uncompressedDirect);
}
}
}
static enum FrameAction {
static enum FrameAction
{
RAW, SKIP, UNCOMPRESS;
}
public static final class FrameMetaData {
public static final class FrameMetaData
{
final int length;
final FrameAction frameAction;
@ -365,14 +385,16 @@ public final class SnappyFramedInputStream extends InputStream implements
* @param frameAction
* @param length
*/
public FrameMetaData(FrameAction frameAction, int length) {
public FrameMetaData(FrameAction frameAction, int length)
{
super();
this.frameAction = frameAction;
this.length = length;
}
}
public static final class FrameData {
public static final class FrameData
{
final int checkSum;
final int offset;
@ -380,14 +402,17 @@ public final class SnappyFramedInputStream extends InputStream implements
* @param checkSum
* @param offset
*/
public FrameData(int checkSum, int offset) {
public FrameData(int checkSum, int offset)
{
super();
this.checkSum = checkSum;
this.offset = offset;
}
}
private boolean ensureBuffer() throws IOException {
private boolean ensureBuffer()
throws IOException
{
if (available() > 0) {
return true;
}
@ -435,14 +460,15 @@ public final class SnappyFramedInputStream extends InputStream implements
.allocateDirect(uncompressedLength);
buffer = new byte[Math.max(input.capacity(), uncompressedLength)];
}
uncompressedDirect.clear();
this.valid = Snappy.uncompress(input, uncompressedDirect);
uncompressedDirect.get(buffer, 0, valid);
this.position = 0;
} else {
}
else {
// we need to start reading at the offset
input.position(frameData.offset);
this.position = 0;
@ -461,7 +487,9 @@ public final class SnappyFramedInputStream extends InputStream implements
return true;
}
private boolean readBlockHeader() throws IOException {
private boolean readBlockHeader()
throws IOException
{
frameHeader.clear();
int read = readBytes(rbc, frameHeader);
@ -478,13 +506,13 @@ public final class SnappyFramedInputStream extends InputStream implements
}
/**
*
* @param frameHeader
* @return
* @throws IOException
*/
private FrameMetaData getFrameMetaData(ByteBuffer frameHeader)
throws IOException {
throws IOException
{
assert frameHeader.hasArray();
@ -537,16 +565,18 @@ public final class SnappyFramedInputStream extends InputStream implements
}
/**
*
* @param content
* @return
* @throws IOException
*/
private FrameData getFrameData(ByteBuffer content) throws IOException {
private FrameData getFrameData(ByteBuffer content)
throws IOException
{
return new FrameData(getCrc32c(content), 4);
}
private int getCrc32c(ByteBuffer content) {
private int getCrc32c(ByteBuffer content)
{
final int position = content.position();

View File

@ -24,12 +24,15 @@ import java.nio.channels.WritableByteChannel;
* href="http://snappy.googlecode.com/svn/trunk/framing_format.txt"
* >x-snappy-framed</a> as an {@link OutputStream} and
* {@link WritableByteChannel}.
*
*
* @author Brett Okken
* @since 1.1.0
*/
public final class SnappyFramedOutputStream extends OutputStream implements
WritableByteChannel {
public final class SnappyFramedOutputStream
extends OutputStream
implements
WritableByteChannel
{
/**
* The x-snappy-framed specification allows for a chunk size up to
@ -37,7 +40,7 @@ public final class SnappyFramedOutputStream extends OutputStream implements
* <p>
* <code>
* We place an additional restriction that the uncompressed data in a chunk
* must be no longer than 65536 bytes. This allows consumers to easily use
* must be no longer than 65536 bytes. This allows consumers to easily use
* small fixed-size buffers.
* </code>
* </p>
@ -69,67 +72,68 @@ public final class SnappyFramedOutputStream extends OutputStream implements
/**
* Creates a new {@link SnappyFramedOutputStream} using the {@link #DEFAULT_BLOCK_SIZE}
* and {@link #DEFAULT_MIN_COMPRESSION_RATIO}.
* @param out
* The underlying {@link OutputStream} to write to. Must not be
* {@code null}.
*
* @param out The underlying {@link OutputStream} to write to. Must not be
* {@code null}.
* @throws IOException
*/
public SnappyFramedOutputStream(OutputStream out) throws IOException {
public SnappyFramedOutputStream(OutputStream out)
throws IOException
{
this(out, DEFAULT_BLOCK_SIZE, DEFAULT_MIN_COMPRESSION_RATIO);
}
/**
* Creates a new {@link SnappyFramedOutputStream} instance.
*
* @param out
* The underlying {@link OutputStream} to write to. Must not be
* {@code null}.
* @param blockSize
* The block size (of raw data) to compress before writing frames
* to <i>out</i>. Must be in (0, 65536].
* @param minCompressionRatio
* Defines the minimum compression ratio (
* {@code compressedLength / rawLength}) that must be achieved to
* write the compressed data. This must be in (0, 1.0].
*
* @param out The underlying {@link OutputStream} to write to. Must not be
* {@code null}.
* @param blockSize The block size (of raw data) to compress before writing frames
* to <i>out</i>. Must be in (0, 65536].
* @param minCompressionRatio Defines the minimum compression ratio (
* {@code compressedLength / rawLength}) that must be achieved to
* write the compressed data. This must be in (0, 1.0].
* @throws IOException
*/
public SnappyFramedOutputStream(OutputStream out, int blockSize,
double minCompressionRatio) throws IOException {
double minCompressionRatio)
throws IOException
{
this(Channels.newChannel(out), blockSize, minCompressionRatio);
}
/**
* Creates a new {@link SnappyFramedOutputStream} using the
* {@link #DEFAULT_BLOCK_SIZE} and {@link #DEFAULT_MIN_COMPRESSION_RATIO}.
*
* @param out
* The underlying {@link WritableByteChannel} to write to. Must
* not be {@code null}.
* @since 1.1.1
*
* @param out The underlying {@link WritableByteChannel} to write to. Must
* not be {@code null}.
* @throws IOException
* @since 1.1.1
*/
public SnappyFramedOutputStream(WritableByteChannel out) throws IOException {
public SnappyFramedOutputStream(WritableByteChannel out)
throws IOException
{
this(out, DEFAULT_BLOCK_SIZE, DEFAULT_MIN_COMPRESSION_RATIO);
}
/**
* Creates a new {@link SnappyFramedOutputStream} instance.
*
* @param out
* The underlying {@link WritableByteChannel} to write to. Must
* not be {@code null}.
* @param blockSize
* The block size (of raw data) to compress before writing frames
* to <i>out</i>. Must be in (0, 65536].
* @param minCompressionRatio
* Defines the minimum compression ratio (
* {@code compressedLength / rawLength}) that must be achieved to
* write the compressed data. This must be in (0, 1.0].
* @since 1.1.1
*
* @param out The underlying {@link WritableByteChannel} to write to. Must
* not be {@code null}.
* @param blockSize The block size (of raw data) to compress before writing frames
* to <i>out</i>. Must be in (0, 65536].
* @param minCompressionRatio Defines the minimum compression ratio (
* {@code compressedLength / rawLength}) that must be achieved to
* write the compressed data. This must be in (0, 1.0].
* @throws IOException
* @since 1.1.1
*/
public SnappyFramedOutputStream(WritableByteChannel out, int blockSize,
double minCompressionRatio) throws IOException {
double minCompressionRatio)
throws IOException
{
if (out == null) {
throw new NullPointerException();
}
@ -157,12 +161,13 @@ public final class SnappyFramedOutputStream extends OutputStream implements
/**
* Writes the implementation specific header or "marker bytes" to
* <i>out</i>.
*
* @param out
* The underlying {@link OutputStream}.
*
* @param out The underlying {@link OutputStream}.
* @throws IOException
*/
private void writeHeader(WritableByteChannel out) throws IOException {
private void writeHeader(WritableByteChannel out)
throws IOException
{
out.write(ByteBuffer.wrap(HEADER_BYTES));
}
@ -170,12 +175,15 @@ public final class SnappyFramedOutputStream extends OutputStream implements
* {@inheritDoc}
*/
@Override
public boolean isOpen() {
public boolean isOpen()
{
return !closed;
}
@Override
public void write(int b) throws IOException {
public void write(int b)
throws IOException
{
if (closed) {
throw new IOException("Stream is closed");
}
@ -186,14 +194,17 @@ public final class SnappyFramedOutputStream extends OutputStream implements
}
@Override
public void write(byte[] input, int offset, int length) throws IOException {
public void write(byte[] input, int offset, int length)
throws IOException
{
if (closed) {
throw new IOException("Stream is closed");
}
if (input == null) {
throw new NullPointerException();
} else if ((offset < 0) || (offset > input.length) || (length < 0)
}
else if ((offset < 0) || (offset > input.length) || (length < 0)
|| ((offset + length) > input.length)
|| ((offset + length) < 0)) {
throw new IndexOutOfBoundsException();
@ -215,7 +226,9 @@ public final class SnappyFramedOutputStream extends OutputStream implements
* {@inheritDoc}
*/
@Override
public int write(ByteBuffer src) throws IOException {
public int write(ByteBuffer src)
throws IOException
{
if (closed) {
throw new ClosedChannelException();
}
@ -255,14 +268,15 @@ public final class SnappyFramedOutputStream extends OutputStream implements
* Transfers all the content from <i>is</i> to this {@link OutputStream}.
* This potentially limits the amount of buffering required to compress
* content.
*
* @param is
* The source of data to compress.
*
* @param is The source of data to compress.
* @return The number of bytes read from <i>is</i>.
* @throws IOException
* @since 1.1.1
*/
public long transferFrom(InputStream is) throws IOException {
public long transferFrom(InputStream is)
throws IOException
{
if (closed) {
throw new ClosedChannelException();
}
@ -299,14 +313,15 @@ public final class SnappyFramedOutputStream extends OutputStream implements
* Transfers all the content from <i>rbc</i> to this
* {@link WritableByteChannel}. This potentially limits the amount of
* buffering required to compress content.
*
* @param rbc
* The source of data to compress.
*
* @param rbc The source of data to compress.
* @return The number of bytes read from <i>rbc</i>.
* @throws IOException
* @since 1.1.1
*/
public long transferFrom(ReadableByteChannel rbc) throws IOException {
public long transferFrom(ReadableByteChannel rbc)
throws IOException
{
if (closed) {
throw new ClosedChannelException();
}
@ -333,7 +348,9 @@ public final class SnappyFramedOutputStream extends OutputStream implements
}
@Override
public final void flush() throws IOException {
public final void flush()
throws IOException
{
if (closed) {
throw new IOException("Stream is closed");
}
@ -341,16 +358,19 @@ public final class SnappyFramedOutputStream extends OutputStream implements
}
@Override
public final void close() throws IOException {
public final void close()
throws IOException
{
if (closed) {
return;
}
try {
flush();
out.close();
} finally {
}
finally {
closed = true;
releaseDirectByteBuffer(directInputBuffer);
releaseDirectByteBuffer(outputBuffer);
}
@ -359,10 +379,12 @@ public final class SnappyFramedOutputStream extends OutputStream implements
/**
* Compresses and writes out any buffered data. This does nothing if there
* is no currently buffered data.
*
*
* @throws IOException
*/
private void flushBuffer() throws IOException {
private void flushBuffer()
throws IOException
{
if (buffer.position() > 0) {
buffer.flip();
writeCompressed(buffer);
@ -375,12 +397,13 @@ public final class SnappyFramedOutputStream extends OutputStream implements
* the data, determines if the compression ratio is acceptable and calls
* {@link #writeBlock(java.nio.channels.WritableByteChannel, java.nio.ByteBuffer, boolean, int)} to
* actually write the frame.
*
*
* @param buffer
* @throws IOException
*/
private void writeCompressed(ByteBuffer buffer) throws IOException {
private void writeCompressed(ByteBuffer buffer)
throws IOException
{
final byte[] input = buffer.array();
final int length = buffer.remaining();
@ -401,7 +424,8 @@ public final class SnappyFramedOutputStream extends OutputStream implements
// minCompressonRatio
if (((double) compressedLength / (double) length) <= minCompressionRatio) {
writeBlock(out, outputBuffer, true, crc32c);
} else {
}
else {
// otherwise use the uncompressed data.
buffer.flip();
writeBlock(out, buffer, false, crc32c);
@ -410,21 +434,19 @@ public final class SnappyFramedOutputStream extends OutputStream implements
/**
* Write a frame (block) to <i>out</i>.
*
* @param out
* The {@link OutputStream} to write to.
* @param data
* The data to write.
* @param compressed
* Indicates if <i>data</i> is the compressed or raw content.
* This is based on whether the compression ratio desired is
* reached.
* @param crc32c
* The calculated checksum.
*
* @param out The {@link OutputStream} to write to.
* @param data The data to write.
* @param compressed Indicates if <i>data</i> is the compressed or raw content.
* This is based on whether the compression ratio desired is
* reached.
* @param crc32c The calculated checksum.
* @throws IOException
*/
private void writeBlock(final WritableByteChannel out, ByteBuffer data,
boolean compressed, int crc32c) throws IOException {
boolean compressed, int crc32c)
throws IOException
{
headerBuffer.clear();
headerBuffer.put((byte) (compressed ? COMPRESSED_DATA_FLAG

View File

@ -5,19 +5,22 @@ import java.io.IOException;
/**
* Enhanced IOException with SnappyErrorCode
*/
public class SnappyIOException extends IOException {
public class SnappyIOException
extends IOException
{
private final SnappyErrorCode errorCode;
public SnappyIOException(SnappyErrorCode errorCode, String message) {
public SnappyIOException(SnappyErrorCode errorCode, String message)
{
super(message);
this.errorCode = errorCode;
}
@Override
public String getMessage() {
public String getMessage()
{
return String.format("[%s] %s", errorCode.name(), super.getMessage());
}
public SnappyErrorCode getErrorCode() { return errorCode; }
}

View File

@ -30,30 +30,31 @@ import java.io.InputStream;
/**
* A stream filter for reading data compressed by {@link SnappyOutputStream}.
*
*
*
* @author leo
*
*/
public class SnappyInputStream extends InputStream
public class SnappyInputStream
extends InputStream
{
private boolean finishedReading = false;
private boolean finishedReading = false;
protected final InputStream in;
private byte[] compressed;
private byte[] uncompressed;
private int uncompressedCursor = 0;
private int uncompressedLimit = 0;
private byte[] compressed;
private byte[] uncompressed;
private int uncompressedCursor = 0;
private int uncompressedLimit = 0;
private byte[] header = new byte[SnappyCodec.headerSize()];
private byte[] header = new byte[SnappyCodec.headerSize()];
/**
* Create a filter for reading compressed data as a uncompressed stream
*
*
* @param input
* @throws IOException
*/
public SnappyInputStream(InputStream input) throws IOException {
public SnappyInputStream(InputStream input)
throws IOException
{
this.in = input;
readHeader();
}
@ -65,57 +66,63 @@ public class SnappyInputStream extends InputStream
* @see java.io.InputStream#close()
*/
@Override
public void close() throws IOException {
public void close()
throws IOException
{
compressed = null;
uncompressed = null;
if (in != null)
if (in != null) {
in.close();
}
}
protected void readHeader() throws IOException {
protected void readHeader()
throws IOException
{
int readBytes = 0;
while (readBytes < header.length) {
int ret = in.read(header, readBytes, header.length - readBytes);
if (ret == -1)
if (ret == -1) {
break;
}
readBytes += ret;
}
// Quick test of the header
if(readBytes == 0) {
if (readBytes == 0) {
// Snappy produces at least 1-byte result. So the empty input is not a valid input
throw new SnappyIOException(SnappyErrorCode.EMPTY_INPUT, "Cannot decompress empty stream");
}
if (readBytes < header.length || header[0] != SnappyCodec.MAGIC_HEADER[0]) {
// do the default uncompression
readFully(header, readBytes);
return;
}
if(!isValidHeader(header)) {
// (probably) compressed by Snappy.compress(byte[])
readFully(header, readBytes);
return;
}
}
private static boolean isValidHeader(byte[] header) throws IOException {
private static boolean isValidHeader(byte[] header)
throws IOException
{
SnappyCodec codec = SnappyCodec.readHeader(new ByteArrayInputStream(header));
if (codec.isValidMagicHeader()) {
// The input data is compressed by SnappyOutputStream
if(codec.version < SnappyCodec.MINIMUM_COMPATIBLE_VERSION) {
if (codec.version < SnappyCodec.MINIMUM_COMPATIBLE_VERSION) {
throw new SnappyIOException(SnappyErrorCode.INCOMPATIBLE_VERSION, String.format(
"Compressed with an incompatible codec version %d. At least version %d is required",
codec.version, SnappyCodec.MINIMUM_COMPATIBLE_VERSION));
"Compressed with an incompatible codec version %d. At least version %d is required",
codec.version, SnappyCodec.MINIMUM_COMPATIBLE_VERSION));
}
return true;
}
else
else {
return false;
}
}
protected void readFully(byte[] fragment, int fragmentLength) throws IOException {
if(fragmentLength == 0) {
protected void readFully(byte[] fragment, int fragmentLength)
throws IOException
{
if (fragmentLength == 0) {
finishedReading = true;
return;
}
@ -123,7 +130,7 @@ public class SnappyInputStream extends InputStream
compressed = new byte[Math.max(8 * 1024, fragmentLength)]; // 8K
System.arraycopy(fragment, 0, compressed, 0, fragmentLength);
int cursor = fragmentLength;
for (int readBytes = 0; (readBytes = in.read(compressed, cursor, compressed.length - cursor)) != -1;) {
for (int readBytes = 0; (readBytes = in.read(compressed, cursor, compressed.length - cursor)) != -1; ) {
cursor += readBytes;
if (cursor >= compressed.length) {
byte[] newBuf = new byte[(compressed.length * 2)];
@ -140,7 +147,6 @@ public class SnappyInputStream extends InputStream
Snappy.uncompress(compressed, 0, cursor, uncompressed, 0);
this.uncompressedCursor = 0;
this.uncompressedLimit = uncompressedLength;
}
/**
@ -151,26 +157,31 @@ public class SnappyInputStream extends InputStream
* @see java.io.InputStream#read(byte[], int, int)
*/
@Override
public int read(byte[] b, int off, int len) throws IOException {
public int read(byte[] b, int off, int len)
throws IOException
{
return rawRead(b, off, len);
}
/**
* Read uncompressed data into the specified array
*
*
* @param array
* @param byteOffset
* @param byteLength
* @return written bytes
* @throws IOException
*/
public int rawRead(Object array, int byteOffset, int byteLength) throws IOException {
public int rawRead(Object array, int byteOffset, int byteLength)
throws IOException
{
int writtenBytes = 0;
for (; writtenBytes < byteLength;) {
for (; writtenBytes < byteLength; ) {
if (uncompressedCursor >= uncompressedLimit) {
if (hasNextChunk())
if (hasNextChunk()) {
continue;
}
else {
return writtenBytes == 0 ? -1 : writtenBytes;
}
@ -186,157 +197,165 @@ public class SnappyInputStream extends InputStream
/**
* Read long array from the stream
*
* @param d
* input
* @param off
* offset
* @param len
* the number of long elements to read
*
* @param d input
* @param off offset
* @param len the number of long elements to read
* @return the total number of bytes read into the buffer, or -1 if there is
* no more data because the end of the stream has been reached.
* no more data because the end of the stream has been reached.
* @throws IOException
*/
public int read(long[] d, int off, int len) throws IOException {
public int read(long[] d, int off, int len)
throws IOException
{
return rawRead(d, off * 8, len * 8);
}
/**
* Read long array from the stream
*
*
* @param d
* @return the total number of bytes read into the buffer, or -1 if there is
* no more data because the end of the stream has been reached.
* no more data because the end of the stream has been reached.
* @throws IOException
*/
public int read(long[] d) throws IOException {
public int read(long[] d)
throws IOException
{
return read(d, 0, d.length);
}
/**
* Read double array from the stream
*
* @param d
* input
* @param off
* offset
* @param len
* the number of double elements to read
*
* @param d input
* @param off offset
* @param len the number of double elements to read
* @return the total number of bytes read into the buffer, or -1 if there is
* no more data because the end of the stream has been reached.
* no more data because the end of the stream has been reached.
* @throws IOException
*/
public int read(double[] d, int off, int len) throws IOException {
public int read(double[] d, int off, int len)
throws IOException
{
return rawRead(d, off * 8, len * 8);
}
/**
* Read double array from the stream
*
*
* @param d
* @return the total number of bytes read into the buffer, or -1 if there is
* no more data because the end of the stream has been reached.
* no more data because the end of the stream has been reached.
* @throws IOException
*/
public int read(double[] d) throws IOException {
public int read(double[] d)
throws IOException
{
return read(d, 0, d.length);
}
/**
* Read int array from the stream
*
*
* @param d
* @return the total number of bytes read into the buffer, or -1 if there is
* no more data because the end of the stream has been reached.
* no more data because the end of the stream has been reached.
* @throws IOException
*/
public int read(int[] d) throws IOException {
public int read(int[] d)
throws IOException
{
return read(d, 0, d.length);
}
/**
* Read int array from the stream
*
* @param d
* input
* @param off
* offset
* @param len
* the number of int elements to read
*
* @param d input
* @param off offset
* @param len the number of int elements to read
* @return the total number of bytes read into the buffer, or -1 if there is
* no more data because the end of the stream has been reached.
* no more data because the end of the stream has been reached.
* @throws IOException
*/
public int read(int[] d, int off, int len) throws IOException {
public int read(int[] d, int off, int len)
throws IOException
{
return rawRead(d, off * 4, len * 4);
}
/**
* Read float array from the stream
*
* @param d
* input
* @param off
* offset
* @param len
* the number of float elements to read
*
* @param d input
* @param off offset
* @param len the number of float elements to read
* @return the total number of bytes read into the buffer, or -1 if there is
* no more data because the end of the stream has been reached.
* no more data because the end of the stream has been reached.
* @throws IOException
*/
public int read(float[] d, int off, int len) throws IOException {
public int read(float[] d, int off, int len)
throws IOException
{
return rawRead(d, off * 4, len * 4);
}
/**
* Read float array from the stream
*
*
* @param d
* @return the total number of bytes read into the buffer, or -1 if there is
* no more data because the end of the stream has been reached.
* no more data because the end of the stream has been reached.
* @throws IOException
*/
public int read(float[] d) throws IOException {
public int read(float[] d)
throws IOException
{
return read(d, 0, d.length);
}
/**
* Read short array from the stream
*
* @param d
* input
* @param off
* offset
* @param len
* the number of short elements to read
*
* @param d input
* @param off offset
* @param len the number of short elements to read
* @return the total number of bytes read into the buffer, or -1 if there is
* no more data because the end of the stream has been reached.
* no more data because the end of the stream has been reached.
* @throws IOException
*/
public int read(short[] d, int off, int len) throws IOException {
public int read(short[] d, int off, int len)
throws IOException
{
return rawRead(d, off * 2, len * 2);
}
/**
* Read short array from the stream
*
*
* @param d
* @return the total number of bytes read into the buffer, or -1 if there is
* no more data because the end of the stream has been reached.
* no more data because the end of the stream has been reached.
* @throws IOException
*/
public int read(short[] d) throws IOException {
public int read(short[] d)
throws IOException
{
return read(d, 0, d.length);
}
/**
* Read next len bytes
*
* @param dest
* @param offset
* @param len
* @return read bytes
*/
private int readNext(byte[] dest, int offset, int len) throws IOException {
private int readNext(byte[] dest, int offset, int len)
throws IOException
{
int readBytes = 0;
while (readBytes < len) {
int ret = in.read(dest, readBytes + offset, len - readBytes);
@ -349,29 +368,36 @@ public class SnappyInputStream extends InputStream
return readBytes;
}
protected boolean hasNextChunk() throws IOException {
if (finishedReading)
protected boolean hasNextChunk()
throws IOException
{
if (finishedReading) {
return false;
}
uncompressedCursor = 0;
uncompressedLimit = 0;
int readBytes = readNext(header, 0, 4);
if(readBytes < 4)
if (readBytes < 4) {
return false;
}
int chunkSize = SnappyOutputStream.readInt(header, 0);
if(chunkSize == SnappyCodec.MAGIC_HEADER_HEAD) {
if (chunkSize == SnappyCodec.MAGIC_HEADER_HEAD) {
// Concatenated data
int remainingHeaderSize = SnappyCodec.headerSize() - 4;
readBytes = readNext(header, 4, remainingHeaderSize);
if(readBytes < remainingHeaderSize)
return false;
if(readBytes < remainingHeaderSize) {
throw new SnappyIOException(SnappyErrorCode.FAILED_TO_UNCOMPRESS, String.format("Insufficient header size in a concatenated block"));
}
if(isValidHeader(header))
if (isValidHeader(header)) {
return hasNextChunk();
else
}
else {
return false;
}
}
// extend the compressed data buffer size
@ -381,8 +407,9 @@ public class SnappyInputStream extends InputStream
readBytes = 0;
while (readBytes < chunkSize) {
int ret = in.read(compressed, readBytes, chunkSize - readBytes);
if (ret == -1)
if (ret == -1) {
break;
}
readBytes += ret;
}
if (readBytes < chunkSize) {
@ -412,15 +439,19 @@ public class SnappyInputStream extends InputStream
* @see java.io.InputStream#read()
*/
@Override
public int read() throws IOException {
public int read()
throws IOException
{
if (uncompressedCursor < uncompressedLimit) {
return uncompressed[uncompressedCursor++] & 0xFF;
}
else {
if (hasNextChunk())
if (hasNextChunk()) {
return read();
else
}
else {
return -1;
}
}
}
@ -428,7 +459,9 @@ public class SnappyInputStream extends InputStream
* @see java.io.InputStream#available()
*/
@Override
public int available() throws IOException {
public int available()
throws IOException
{
if (uncompressedCursor < uncompressedLimit) {
return uncompressedLimit - uncompressedCursor;
}

View File

@ -36,10 +36,10 @@ import java.util.UUID;
* user platform (<i>os.name</i> and <i>os.arch</i>). The natively compiled
* libraries bundled to snappy-java contain the codes of the original snappy and
* JNI programs to access Snappy.
*
* <p/>
* In default, no configuration is required to use snappy-java, but you can load
* your own native library created by 'make native' command.
*
* <p/>
* This SnappyLoader searches for native libraries (snappyjava.dll,
* libsnappy.so, etc.) in the following order:
* <ol>
@ -53,70 +53,76 @@ import java.util.UUID;
* <i>org.xerial.snappy.tempdir</i> is set, use this folder instead of
* <i>java.io.tempdir</i>.
* </ol>
*
* <p/>
* <p>
* If you do not want to use folder <i>java.io.tempdir</i>, set the System
* property <i>org.xerial.snappy.tempdir</i>. For example, to use
* <i>/tmp/leo</i> as a temporary folder to copy native libraries, use -D option
* of JVM:
*
* <p/>
* <pre>
* <code>
* java -Dorg.xerial.snappy.tempdir="/tmp/leo" ...
* </code>
* </pre>
*
* <p/>
* </p>
*
*
* @author leo
*
*/
public class SnappyLoader
{
public static final String SNAPPY_SYSTEM_PROPERTIES_FILE = "org-xerial-snappy.properties";
public static final String KEY_SNAPPY_LIB_PATH = "org.xerial.snappy.lib.path";
public static final String KEY_SNAPPY_LIB_NAME = "org.xerial.snappy.lib.name";
public static final String KEY_SNAPPY_TEMPDIR = "org.xerial.snappy.tempdir";
public static final String KEY_SNAPPY_USE_SYSTEMLIB = "org.xerial.snappy.use.systemlib";
public static final String SNAPPY_SYSTEM_PROPERTIES_FILE = "org-xerial-snappy.properties";
public static final String KEY_SNAPPY_LIB_PATH = "org.xerial.snappy.lib.path";
public static final String KEY_SNAPPY_LIB_NAME = "org.xerial.snappy.lib.name";
public static final String KEY_SNAPPY_TEMPDIR = "org.xerial.snappy.tempdir";
public static final String KEY_SNAPPY_USE_SYSTEMLIB = "org.xerial.snappy.use.systemlib";
public static final String KEY_SNAPPY_DISABLE_BUNDLED_LIBS = "org.xerial.snappy.disable.bundled.libs"; // Depreciated, but preserved for backward compatibility
private static volatile boolean isLoaded = false;
private static volatile SnappyNative api = null;
private static volatile boolean isLoaded = false;
private static volatile SnappyNative api = null;
private static File nativeLibFile = null;
static void cleanUpExtractedNativeLib() {
if(nativeLibFile != null && nativeLibFile.exists())
nativeLibFile.delete();
static void cleanUpExtractedNativeLib()
{
if (nativeLibFile != null && nativeLibFile.exists()) {
boolean deleted = nativeLibFile.delete();
if (!deleted) {
// Deleting native lib has failed, but it's not serious so simply ignore it here
}
}
}
/**
* Set the api instance.
*
*
* @param nativeCode
*/
static synchronized void setApi(SnappyNative nativeCode)
{
api = nativeCode;
api = nativeCode;
}
/**
* load system properties when configuration file of the name
* {@link #SNAPPY_SYSTEM_PROPERTIES_FILE} is found
*/
private static void loadSnappySystemProperties() {
private static void loadSnappySystemProperties()
{
try {
InputStream is = Thread.currentThread().getContextClassLoader()
.getResourceAsStream(SNAPPY_SYSTEM_PROPERTIES_FILE);
if (is == null)
return; // no configuration file is found
if (is == null) {
return; // no configuration file is found
}
// Load property file
Properties props = new Properties();
props.load(is);
is.close();
Enumeration< ? > names = props.propertyNames();
Enumeration<?> names = props.propertyNames();
while (names.hasMoreElements()) {
String name = (String) names.nextElement();
if (name.startsWith("org.xerial.snappy.")) {
@ -138,8 +144,9 @@ public class SnappyLoader
static synchronized SnappyNative load()
{
if (api != null)
if (api != null) {
return api;
}
try {
loadNativeLibrary();
@ -158,7 +165,8 @@ public class SnappyLoader
/**
* Load a native library of snappy-java
*/
private static void loadNativeLibrary() {
private static void loadNativeLibrary()
{
nativeLibFile = findNativeLibrary();
if (nativeLibFile != null) {
@ -171,34 +179,38 @@ public class SnappyLoader
}
}
private static boolean contentsEquals(InputStream in1, InputStream in2) throws IOException {
if(!(in1 instanceof BufferedInputStream)) {
in1 = new BufferedInputStream(in1);
private static boolean contentsEquals(InputStream in1, InputStream in2)
throws IOException
{
if (!(in1 instanceof BufferedInputStream)) {
in1 = new BufferedInputStream(in1);
}
if(!(in2 instanceof BufferedInputStream)) {
in2 = new BufferedInputStream(in2);
if (!(in2 instanceof BufferedInputStream)) {
in2 = new BufferedInputStream(in2);
}
int ch = in1.read();
while(ch != -1) {
int ch2 = in2.read();
if(ch != ch2)
while (ch != -1) {
int ch2 = in2.read();
if (ch != ch2) {
return false;
}
ch = in1.read();
}
int ch2 = in2.read();
return ch2 == -1;
}
/**
* Extract the specified library file to the target folder
*
*
* @param libFolderForCurrentOS
* @param libraryFileName
* @param targetFolder
* @return
*/
private static File extractLibraryFile(String libFolderForCurrentOS, String libraryFileName, String targetFolder) {
private static File extractLibraryFile(String libFolderForCurrentOS, String libraryFileName, String targetFolder)
{
String nativeLibraryFilePath = libFolderForCurrentOS + "/" + libraryFileName;
// Attach UUID to the native library file to ensure multiple class loaders can read the libsnappy-java multiple times.
@ -208,44 +220,61 @@ public class SnappyLoader
try {
// Extract a native library file into the target directory
InputStream reader = SnappyLoader.class.getResourceAsStream(nativeLibraryFilePath);
FileOutputStream writer = new FileOutputStream(extractedLibFile);
InputStream reader = null;
FileOutputStream writer = null;
try {
byte[] buffer = new byte[8192];
int bytesRead = 0;
while ((bytesRead = reader.read(buffer)) != -1) {
writer.write(buffer, 0, bytesRead);
reader = SnappyLoader.class.getResourceAsStream(nativeLibraryFilePath);
try {
writer = new FileOutputStream(extractedLibFile);
byte[] buffer = new byte[8192];
int bytesRead = 0;
while ((bytesRead = reader.read(buffer)) != -1) {
writer.write(buffer, 0, bytesRead);
}
}
finally {
if (writer != null) {
writer.close();
}
}
}
finally {
if (reader != null) {
reader.close();
}
// Delete the extracted lib file on JVM exit.
extractedLibFile.deleteOnExit();
if(writer != null)
writer.close();
if(reader != null)
reader.close();
}
// Set executable (x) flag to enable Java to load the native library
extractedLibFile.setReadable(true);
extractedLibFile.setWritable(true, true);
extractedLibFile.setExecutable(true);
boolean success = extractedLibFile.setReadable(true) &&
extractedLibFile.setWritable(true, true) &&
extractedLibFile.setExecutable(true);
if (!success) {
// Setting file flag may fail, but in this case another error will be thrown in later phase
}
// Check whether the contents are properly copied from the resource folder
{
InputStream nativeIn = SnappyLoader.class.getResourceAsStream(nativeLibraryFilePath);
InputStream extractedLibIn = new FileInputStream(extractedLibFile);
InputStream nativeIn = null;
InputStream extractedLibIn = null;
try {
if(!contentsEquals(nativeIn, extractedLibIn))
nativeIn = SnappyLoader.class.getResourceAsStream(nativeLibraryFilePath);
extractedLibIn = new FileInputStream(extractedLibFile);
if (!contentsEquals(nativeIn, extractedLibIn)) {
throw new SnappyError(SnappyErrorCode.FAILED_TO_LOAD_NATIVE_LIBRARY, String.format("Failed to write a native library file at %s", extractedLibFile));
}
}
finally {
if(nativeIn != null)
if (nativeIn != null) {
nativeIn.close();
if(extractedLibIn != null)
}
if (extractedLibIn != null) {
extractedLibIn.close();
}
}
}
@ -257,44 +286,47 @@ public class SnappyLoader
}
}
static File findNativeLibrary() {
static File findNativeLibrary()
{
boolean useSystemLib = Boolean.parseBoolean(System.getProperty(KEY_SNAPPY_USE_SYSTEMLIB, "false"));
boolean disabledBundledLibs = Boolean
.parseBoolean(System.getProperty(KEY_SNAPPY_DISABLE_BUNDLED_LIBS, "false"));
if (useSystemLib || disabledBundledLibs)
if (useSystemLib || disabledBundledLibs) {
return null; // Use a pre-installed libsnappyjava
}
// Try to load the library in org.xerial.snappy.lib.path */
String snappyNativeLibraryPath = System.getProperty(KEY_SNAPPY_LIB_PATH);
String snappyNativeLibraryName = System.getProperty(KEY_SNAPPY_LIB_NAME);
// Resolve the library file name with a suffix (e.g., dll, .so, etc.)
if (snappyNativeLibraryName == null)
if (snappyNativeLibraryName == null) {
snappyNativeLibraryName = System.mapLibraryName("snappyjava");
}
if (snappyNativeLibraryPath != null) {
File nativeLib = new File(snappyNativeLibraryPath, snappyNativeLibraryName);
if (nativeLib.exists())
if (nativeLib.exists()) {
return nativeLib;
}
}
// Load an OS-dependent native library inside a jar file
snappyNativeLibraryPath = "/org/xerial/snappy/native/" + OSInfo.getNativeLibFolderPathForCurrentOS();
boolean hasNativeLib = hasResource(snappyNativeLibraryPath + "/" + snappyNativeLibraryName);
if(!hasNativeLib) {
if(OSInfo.getOSName().equals("Mac")) {
if (!hasNativeLib) {
if (OSInfo.getOSName().equals("Mac")) {
// Fix for openjdk7 for Mac
String altName = "libsnappyjava.jnilib";
if(hasResource(snappyNativeLibraryPath + "/" + altName)) {
if (hasResource(snappyNativeLibraryPath + "/" + altName)) {
snappyNativeLibraryName = altName;
hasNativeLib = true;
}
}
}
if(!hasNativeLib) {
if (!hasNativeLib) {
String errorMessage = String.format("no native library is found for os.name=%s and os.arch=%s", OSInfo.getOSName(), OSInfo.getArchName());
throw new SnappyError(SnappyErrorCode.FAILED_TO_LOAD_NATIVE_LIBRARY, errorMessage);
}
@ -302,33 +334,36 @@ public class SnappyLoader
// Temporary folder for the native lib. Use the value of org.xerial.snappy.tempdir or java.io.tmpdir
File tempFolder = new File(System.getProperty(KEY_SNAPPY_TEMPDIR, System.getProperty("java.io.tmpdir")));
if (!tempFolder.exists()) {
tempFolder.mkdir();
boolean created = tempFolder.mkdirs();
if (!created) {
// if created == false, it will fail eventually in the later part
}
}
// Extract and load a native library inside the jar file
return extractLibraryFile(snappyNativeLibraryPath, snappyNativeLibraryName, tempFolder.getAbsolutePath());
}
private static boolean hasResource(String path) {
private static boolean hasResource(String path)
{
return SnappyLoader.class.getResource(path) != null;
}
/**
* Get the snappy-java version by reading pom.properties embedded in jar.
* This version data is used as a suffix of a dll file extracted from the
* jar.
*
*
* @return the version string
*/
public static String getVersion() {
public static String getVersion()
{
URL versionFile = SnappyLoader.class
.getResource("/META-INF/maven/org.xerial.snappy/snappy-java/pom.properties");
if (versionFile == null)
if (versionFile == null) {
versionFile = SnappyLoader.class.getResource("/org/xerial/snappy/VERSION");
}
String version = "unknown";
try {
@ -336,8 +371,9 @@ public class SnappyLoader
Properties versionData = new Properties();
versionData.load(versionFile.openStream());
version = versionData.getProperty("version", version);
if (version.equals("unknown"))
if (version.equals("unknown")) {
version = versionData.getProperty("VERSION", version);
}
version = version.trim().replaceAll("[^0-9M\\.]", "");
}
}
@ -346,5 +382,4 @@ public class SnappyLoader
}
return version;
}
}

View File

@ -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);
}

View File

@ -1,87 +1,100 @@
/*--------------------------------------------------------------------------
* 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/>
* <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));
}
}

View File

@ -55,7 +55,9 @@ import java.io.OutputStream;
*
* @author leo
*/
public class SnappyOutputStream extends OutputStream {
public class SnappyOutputStream
extends OutputStream
{
static final int MIN_BLOCK_SIZE = 1 * 1024;
static final int DEFAULT_BLOCK_SIZE = 32 * 1024; // Use 32kb for the default block size
@ -65,12 +67,15 @@ public class SnappyOutputStream extends OutputStream {
private final BufferAllocator inputBufferAllocator;
private final BufferAllocator outputBufferAllocator;
protected final byte[] inputBuffer;
protected final byte[] outputBuffer;
// The input and output buffer fields are set to null when closing this stream:
protected byte[] inputBuffer;
protected byte[] outputBuffer;
private int inputCursor = 0;
private int outputCursor = 0;
private boolean closed;
public SnappyOutputStream(OutputStream out) {
public SnappyOutputStream(OutputStream out)
{
this(out, DEFAULT_BLOCK_SIZE);
}
@ -79,11 +84,13 @@ public class SnappyOutputStream extends OutputStream {
* @param blockSize byte size of the internal buffer size
* @throws IOException
*/
public SnappyOutputStream(OutputStream out, int blockSize) {
this(out, blockSize, CachedBufferAllocator.factory);
public SnappyOutputStream(OutputStream out, int blockSize)
{
this(out, blockSize, CachedBufferAllocator.getBufferAllocatorFactory());
}
public SnappyOutputStream(OutputStream out, int blockSize, BufferAllocatorFactory bufferAllocatorFactory) {
public SnappyOutputStream(OutputStream out, int blockSize, BufferAllocatorFactory bufferAllocatorFactory)
{
this.out = out;
this.blockSize = Math.max(MIN_BLOCK_SIZE, blockSize);
int inputSize = blockSize;
@ -98,72 +105,83 @@ public class SnappyOutputStream extends OutputStream {
outputCursor = SnappyCodec.currentHeader.writeHeader(outputBuffer, 0);
}
/* (non-Javadoc)
* @see java.io.OutputStream#write(byte[], int, int)
*/
@Override
public void write(byte[] b, int off, int len) throws IOException {
public void write(byte[] b, int off, int len)
throws IOException
{
rawWrite(b, off, len);
}
/**
* Compress the input long array data
*
* @param d input array
* @param d input array
* @param off offset in the array
* @param len the number of elements in the array to copy
* @throws IOException
*/
public void write(long[] d, int off, int len) throws IOException {
public void write(long[] d, int off, int len)
throws IOException
{
rawWrite(d, off * 8, len * 8);
}
/**
* Compress the input double array data
*
* @param f input array
* @param f input array
* @param off offset in the array
* @param len the number of elements in the array to copy
* @throws IOException
*/
public void write(double[] f, int off, int len) throws IOException {
public void write(double[] f, int off, int len)
throws IOException
{
rawWrite(f, off * 8, len * 8);
}
/**
* Compress the input float array data
*
* @param f input array
* @param f input array
* @param off offset in the array
* @param len the number of elements in the array to copy
* @throws IOException
*/
public void write(float[] f, int off, int len) throws IOException {
public void write(float[] f, int off, int len)
throws IOException
{
rawWrite(f, off * 4, len * 4);
}
/**
* Compress the input int array data
*
* @param f input array
* @param f input array
* @param off offset in the array
* @param len the number of elements in the array to copy
* @throws IOException
*/
public void write(int[] f, int off, int len) throws IOException {
public void write(int[] f, int off, int len)
throws IOException
{
rawWrite(f, off * 4, len * 4);
}
/**
* Compress the input short array data
*
* @param f input array
* @param f input array
* @param off offset in the array
* @param len the number of elements in the array to copy
* @throws IOException
*/
public void write(short[] f, int off, int len) throws IOException {
public void write(short[] f, int off, int len)
throws IOException
{
rawWrite(f, off * 2, len * 2);
}
@ -173,7 +191,9 @@ public class SnappyOutputStream extends OutputStream {
* @param d
* @throws IOException
*/
public void write(long[] d) throws IOException {
public void write(long[] d)
throws IOException
{
write(d, 0, d.length);
}
@ -183,7 +203,9 @@ public class SnappyOutputStream extends OutputStream {
* @param f
* @throws IOException
*/
public void write(double[] f) throws IOException {
public void write(double[] f)
throws IOException
{
write(f, 0, f.length);
}
@ -193,7 +215,9 @@ public class SnappyOutputStream extends OutputStream {
* @param f
* @throws IOException
*/
public void write(float[] f) throws IOException {
public void write(float[] f)
throws IOException
{
write(f, 0, f.length);
}
@ -203,7 +227,9 @@ public class SnappyOutputStream extends OutputStream {
* @param f
* @throws IOException
*/
public void write(int[] f) throws IOException {
public void write(int[] f)
throws IOException
{
write(f, 0, f.length);
}
@ -213,11 +239,14 @@ public class SnappyOutputStream extends OutputStream {
* @param f
* @throws IOException
*/
public void write(short[] f) throws IOException {
public void write(short[] f)
throws IOException
{
write(f, 0, f.length);
}
private boolean hasSufficientOutputBufferFor(int inputSize) {
private boolean hasSufficientOutputBufferFor(int inputSize)
{
int maxCompressedSize = Snappy.maxCompressedLength(inputSize);
return maxCompressedSize < outputBuffer.length - outputCursor - 4;
}
@ -225,22 +254,28 @@ public class SnappyOutputStream extends OutputStream {
/**
* Compress the raw byte array data.
*
* @param array array data of any type (e.g., byte[], float[], long[], ...)
* @param array array data of any type (e.g., byte[], float[], long[], ...)
* @param byteOffset
* @param byteLength
* @throws IOException
*/
public void rawWrite(Object array, int byteOffset, int byteLength) throws IOException {
public void rawWrite(Object array, int byteOffset, int byteLength)
throws IOException
{
if (closed) {
throw new IOException("Stream is closed");
}
int cursor = 0;
while(cursor < byteLength) {
while (cursor < byteLength) {
int readLen = Math.min(byteLength - cursor, blockSize - inputCursor);
// copy the input data to uncompressed buffer
if(readLen > 0) {
if (readLen > 0) {
Snappy.arrayCopy(array, byteOffset + cursor, readLen, inputBuffer, inputCursor);
inputCursor += readLen;
}
if(inputCursor < blockSize)
if (inputCursor < blockSize) {
return;
}
compressInput();
cursor += readLen;
@ -257,8 +292,13 @@ public class SnappyOutputStream extends OutputStream {
* @see java.io.OutputStream#write(int)
*/
@Override
public void write(int b) throws IOException {
if(inputCursor >= inputBuffer.length) {
public void write(int b)
throws IOException
{
if (closed) {
throw new IOException("Stream is closed");
}
if (inputCursor >= inputBuffer.length) {
compressInput();
}
inputBuffer[inputCursor++] = (byte) b;
@ -268,20 +308,27 @@ public class SnappyOutputStream extends OutputStream {
* @see java.io.OutputStream#flush()
*/
@Override
public void flush() throws IOException {
public void flush()
throws IOException
{
if (closed) {
throw new IOException("Stream is closed");
}
compressInput();
dumpOutput();
out.flush();
}
static void writeInt(byte[] dst, int offset, int v) {
static void writeInt(byte[] dst, int offset, int v)
{
dst[offset] = (byte) ((v >> 24) & 0xFF);
dst[offset + 1] = (byte) ((v >> 16) & 0xFF);
dst[offset + 2] = (byte) ((v >> 8) & 0xFF);
dst[offset + 3] = (byte) ((v >> 0) & 0xFF);
}
static int readInt(byte[] buffer, int pos) {
static int readInt(byte[] buffer, int pos)
{
int b1 = (buffer[pos] & 0xFF) << 24;
int b2 = (buffer[pos + 1] & 0xFF) << 16;
int b3 = (buffer[pos + 2] & 0xFF) << 8;
@ -289,20 +336,24 @@ public class SnappyOutputStream extends OutputStream {
return b1 | b2 | b3 | b4;
}
protected void dumpOutput() throws IOException {
if(outputCursor > 0) {
protected void dumpOutput()
throws IOException
{
if (outputCursor > 0) {
out.write(outputBuffer, 0, outputCursor);
outputCursor = 0;
}
}
protected void compressInput() throws IOException {
if(inputCursor <= 0) {
protected void compressInput()
throws IOException
{
if (inputCursor <= 0) {
return; // no need to dump
}
// Compress and dump the buffer content
if(!hasSufficientOutputBufferFor(inputCursor)) {
if (!hasSufficientOutputBufferFor(inputCursor)) {
dumpOutput();
}
int compressedSize = Snappy.compress(inputBuffer, 0, inputCursor, outputBuffer, outputCursor + 4);
@ -319,14 +370,22 @@ public class SnappyOutputStream extends OutputStream {
* @see java.io.OutputStream#close()
*/
@Override
public void close() throws IOException {
public void close()
throws IOException
{
if (closed) {
return;
}
try {
flush();
out.close();
} finally {
}
finally {
closed = true;
inputBufferAllocator.release(inputBuffer);
outputBufferAllocator.release(outputBuffer);
inputBuffer = null;
outputBuffer = null;
}
}
}

View File

@ -3,9 +3,10 @@ package org.xerial.snappy.buffer;
/**
* BufferAllocator interface. The implementation of this interface must be thread-safe
*/
public interface BufferAllocator {
public interface BufferAllocator
{
public byte[] allocate(int size);
public void release(byte[] buffer);
public void release(byte[] buffer);
}

View File

@ -3,7 +3,8 @@ package org.xerial.snappy.buffer;
/**
*
*/
public interface BufferAllocatorFactory {
public interface BufferAllocatorFactory
{
BufferAllocator getBufferAllocator(int minSize);
}

View File

@ -6,29 +6,45 @@ import java.util.*;
/**
* Cached buffer
*/
public class CachedBufferAllocator implements BufferAllocator {
public static BufferAllocatorFactory factory = new BufferAllocatorFactory() {
public class CachedBufferAllocator
implements BufferAllocator
{
private static BufferAllocatorFactory factory = new BufferAllocatorFactory()
{
@Override
public BufferAllocator getBufferAllocator(int bufferSize) {
public BufferAllocator getBufferAllocator(int bufferSize)
{
return CachedBufferAllocator.getAllocator(bufferSize);
}
};
public static void setBufferAllocatorFactory(BufferAllocatorFactory factory)
{
assert (factory != null);
CachedBufferAllocator.factory = factory;
}
public static BufferAllocatorFactory getBufferAllocatorFactory()
{
return factory;
}
/**
* Use SoftReference so that having this queueTable does not prevent the GC of CachedBufferAllocator instances
*/
public static Map<Integer, SoftReference<CachedBufferAllocator>> queueTable = new HashMap<Integer, SoftReference<CachedBufferAllocator>>();
private static final Map<Integer, SoftReference<CachedBufferAllocator>> queueTable = new HashMap<Integer, SoftReference<CachedBufferAllocator>>();
private final int bufferSize;
private final Deque<byte[]> bufferQueue;
public CachedBufferAllocator(int bufferSize) {
public CachedBufferAllocator(int bufferSize)
{
this.bufferSize = bufferSize;
this.bufferQueue = new ArrayDeque<byte[]>();
}
public static synchronized CachedBufferAllocator getAllocator(int bufferSize) {
public static synchronized CachedBufferAllocator getAllocator(int bufferSize)
{
CachedBufferAllocator result = null;
if (queueTable.containsKey(bufferSize)) {
@ -42,9 +58,10 @@ public class CachedBufferAllocator implements BufferAllocator {
}
@Override
public byte[] allocate(int size) {
synchronized(this) {
if(bufferQueue.isEmpty()) {
public byte[] allocate(int size)
{
synchronized (this) {
if (bufferQueue.isEmpty()) {
return new byte[size];
}
else {
@ -52,9 +69,11 @@ public class CachedBufferAllocator implements BufferAllocator {
}
}
}
@Override
public void release(byte[] buffer) {
synchronized(this) {
public void release(byte[] buffer)
{
synchronized (this) {
bufferQueue.addLast(buffer);
}
}

View File

@ -3,24 +3,30 @@ package org.xerial.snappy.buffer;
/**
* Simple buffer allocator, which does not reuse the allocated buffer
*/
public class DefaultBufferAllocator implements BufferAllocator {
public class DefaultBufferAllocator
implements BufferAllocator
{
public static BufferAllocatorFactory factory = new BufferAllocatorFactory() {
public static BufferAllocatorFactory factory = new BufferAllocatorFactory()
{
public BufferAllocator singleton = new DefaultBufferAllocator();
@Override
public BufferAllocator getBufferAllocator(int bufferSize) {
public BufferAllocator getBufferAllocator(int bufferSize)
{
return singleton;
}
};
@Override
public byte[] allocate(int size) {
public byte[] allocate(int size)
{
return new byte[size];
}
@Override
public void release(byte[] buffer) {
public void release(byte[] buffer)
{
// do nothing
}
}

View File

@ -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.
* <p/>
* 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/>
* <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>
* <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;

View File

@ -1 +1 @@
VERSION=1.1.2
VERSION=1.1.3

View File

@ -1,228 +1,238 @@
/*--------------------------------------------------------------------------
* 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;
}
}

View File

@ -34,7 +34,8 @@ import org.junit.Test;
public class OSInfoTest
{
@Test
public void osName() {
public void osName()
{
assertEquals("Windows", OSInfo.translateOSNameToFolderName("Windows XP"));
assertEquals("Windows", OSInfo.translateOSNameToFolderName("Windows 2000"));
assertEquals("Windows", OSInfo.translateOSNameToFolderName("Windows Vista"));
@ -53,7 +54,8 @@ public class OSInfoTest
}
@Test
public void archName() {
public void archName()
{
assertEquals("i386", OSInfo.translateArchNameToFolderName("i386"));
assertEquals("x86", OSInfo.translateArchNameToFolderName("x86"));
assertEquals("ppc", OSInfo.translateArchNameToFolderName("ppc"));
@ -61,7 +63,8 @@ public class OSInfoTest
}
@Test
public void folderPath() {
public void folderPath()
{
String[] component = OSInfo.getNativeLibFolderPathForCurrentOS().split("/");
assertEquals(2, component.length);
assertEquals(OSInfo.getOSName(), component[0]);
@ -69,7 +72,9 @@ public class OSInfoTest
}
@Test
public void testMainForOSName() throws Exception {
public void testMainForOSName()
throws Exception
{
// preserve the current System.out
PrintStream out = System.out;
@ -78,18 +83,19 @@ public class OSInfoTest
ByteArrayOutputStream buf = new ByteArrayOutputStream();
PrintStream tmpOut = new PrintStream(buf);
System.setOut(tmpOut);
OSInfo.main(new String[] { "--os" });
OSInfo.main(new String[] {"--os"});
assertEquals(OSInfo.getOSName(), buf.toString());
}
finally {
// reset STDOUT
System.setOut(out);
}
}
@Test
public void testMainForArchName() throws Exception {
public void testMainForArchName()
throws Exception
{
// preserver the current System.out
PrintStream out = System.out;
@ -98,7 +104,7 @@ public class OSInfoTest
ByteArrayOutputStream buf = new ByteArrayOutputStream();
PrintStream tmpOut = new PrintStream(buf);
System.setOut(tmpOut);
OSInfo.main(new String[] { "--arch" });
OSInfo.main(new String[] {"--arch"});
assertEquals(OSInfo.getArchName(), buf.toString());
}
finally {
@ -106,5 +112,4 @@ public class OSInfoTest
System.setOut(out);
}
}
}

View File

@ -1,68 +1,73 @@
/*
* 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/>
* <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;
}
}

View File

@ -26,35 +26,42 @@ import org.junit.Test;
/**
* Tests the functionality of {@link org.xerial.snappy.SnappyFramedInputStream}
* and {@link org.xerial.snappy.SnappyFramedOutputStream}.
*
*
* @author Brett Okken
*/
public class SnappyFramedStreamTest {
public class SnappyFramedStreamTest
{
/**
* @throws IOException
*/
protected OutputStream createOutputStream(OutputStream target)
throws IOException {
throws IOException
{
return new SnappyFramedOutputStream(target);
}
/**
* {@inheritDoc}
*
*
* @throws IOException
*/
protected InputStream createInputStream(InputStream source,
boolean verifyCheckSums) throws IOException {
boolean verifyCheckSums)
throws IOException
{
return new SnappyFramedInputStream(source, verifyCheckSums);
}
protected byte[] getMarkerFrame() {
protected byte[] getMarkerFrame()
{
return HEADER_BYTES;
}
@Test
public void testSimple() throws Exception {
public void testSimple()
throws Exception
{
byte[] original = "aaaaaaaaaaaabbbbbbbaaaaaa".getBytes("utf-8");
byte[] compressed = compress(original);
@ -83,7 +90,9 @@ public class SnappyFramedStreamTest {
}
@Test
public void testUncompressable() throws Exception {
public void testUncompressable()
throws Exception
{
byte[] random = getRandom(1, 5000);
int crc32c = maskedCrc32c(random);
@ -103,71 +112,90 @@ public class SnappyFramedStreamTest {
}
@Test
public void testEmptyCompression() throws Exception {
public void testEmptyCompression()
throws Exception
{
byte[] empty = new byte[0];
assertArrayEquals(compress(empty), HEADER_BYTES);
assertArrayEquals(uncompress(HEADER_BYTES), empty);
}
@Test(expected = EOFException.class)
public void testShortBlockHeader() throws Exception {
uncompressBlock(new byte[] { 0 });
public void testShortBlockHeader()
throws Exception
{
uncompressBlock(new byte[] {0});
}
@Test(expected = EOFException.class)
public void testShortBlockData() throws Exception {
public void testShortBlockData()
throws Exception
{
// flag = 0, size = 8, crc32c = 0, block data= [x, x]
uncompressBlock(new byte[] { 1, 8, 0, 0, 0, 0, 0, 0, 'x', 'x' });
uncompressBlock(new byte[] {1, 8, 0, 0, 0, 0, 0, 0, 'x', 'x'});
}
@Test
public void testUnskippableChunkFlags() throws Exception {
public void testUnskippableChunkFlags()
throws Exception
{
for (int i = 2; i <= 0x7f; i++) {
try {
uncompressBlock(new byte[] { (byte) i, 5, 0, 0, 0, 0, 0, 0, 0 });
uncompressBlock(new byte[] {(byte) i, 5, 0, 0, 0, 0, 0, 0, 0});
fail("no exception thrown with flag: " + Integer.toHexString(i));
} catch (IOException e) {
}
catch (IOException e) {
}
}
}
@Test
public void testSkippableChunkFlags() throws Exception {
public void testSkippableChunkFlags()
throws Exception
{
for (int i = 0x80; i <= 0xfe; i++) {
try {
uncompressBlock(new byte[] { (byte) i, 5, 0, 0, 0, 0, 0, 0, 0 });
} catch (IOException e) {
uncompressBlock(new byte[] {(byte) i, 5, 0, 0, 0, 0, 0, 0, 0});
}
catch (IOException e) {
fail("exception thrown with flag: " + Integer.toHexString(i));
}
}
}
@Test(expected = IOException.class)
public void testInvalidBlockSizeZero() throws Exception {
public void testInvalidBlockSizeZero()
throws Exception
{
// flag = '0', block size = 4, crc32c = 0
uncompressBlock(new byte[] { 1, 4, 0, 0, 0, 0, 0, 0 });
uncompressBlock(new byte[] {1, 4, 0, 0, 0, 0, 0, 0});
}
@Test(expected = IOException.class)
public void testInvalidChecksum() throws Exception {
public void testInvalidChecksum()
throws Exception
{
// flag = 0, size = 5, crc32c = 0, block data = [a]
uncompressBlock(new byte[] { 1, 5, 0, 0, 0, 0, 0, 0, 'a' });
uncompressBlock(new byte[] {1, 5, 0, 0, 0, 0, 0, 0, 'a'});
}
@Test
public void testInvalidChecksumIgnoredWhenVerificationDisabled()
throws Exception {
throws Exception
{
// flag = 0, size = 4, crc32c = 0, block data = [a]
byte[] block = { 1, 5, 0, 0, 0, 0, 0, 0, 'a' };
byte[] block = {1, 5, 0, 0, 0, 0, 0, 0, 'a'};
ByteArrayInputStream inputData = new ByteArrayInputStream(
blockToStream(block));
assertArrayEquals(toByteArray(createInputStream(inputData, false)),
new byte[] { 'a' });
new byte[] {'a'});
}
@Test
public void testTransferFrom_InputStream() throws IOException {
public void testTransferFrom_InputStream()
throws IOException
{
final byte[] random = getRandom(0.5, 100000);
final ByteArrayOutputStream baos = new ByteArrayOutputStream(
@ -184,7 +212,9 @@ public class SnappyFramedStreamTest {
}
@Test
public void testTransferFrom_ReadableByteChannel() throws IOException {
public void testTransferFrom_ReadableByteChannel()
throws IOException
{
final byte[] random = getRandom(0.5, 100000);
final ByteArrayOutputStream baos = new ByteArrayOutputStream(
@ -201,7 +231,9 @@ public class SnappyFramedStreamTest {
}
@Test
public void testTransferTo_OutputStream() throws IOException {
public void testTransferTo_OutputStream()
throws IOException
{
final byte[] random = getRandom(0.5, 100000);
final byte[] compressed = compress(random);
@ -216,7 +248,9 @@ public class SnappyFramedStreamTest {
}
@Test
public void testTransferTo_WritableByteChannel() throws IOException {
public void testTransferTo_WritableByteChannel()
throws IOException
{
final byte[] random = getRandom(0.5, 100000);
final byte[] compressed = compress(random);
@ -233,14 +267,16 @@ public class SnappyFramedStreamTest {
}
@Test
public void testLargerFrames_raw_() throws IOException {
public void testLargerFrames_raw_()
throws IOException
{
final byte[] random = getRandom(0.5, 100000);
final byte[] stream = new byte[HEADER_BYTES.length + 8 + random.length];
System.arraycopy(HEADER_BYTES, 0, stream, 0, HEADER_BYTES.length);
stream[10] = UNCOMPRESSED_DATA_FLAG;
int length = random.length + 4;
stream[11] = (byte) length;
stream[12] = (byte) (length >>> 8);
@ -260,16 +296,18 @@ public class SnappyFramedStreamTest {
}
@Test
public void testLargerFrames_compressed_() throws IOException {
public void testLargerFrames_compressed_()
throws IOException
{
final byte[] random = getRandom(0.5, 500000);
final byte[] compressed = Snappy.compress(random);
final byte[] stream = new byte[HEADER_BYTES.length + 8 + compressed.length];
System.arraycopy(HEADER_BYTES, 0, stream, 0, HEADER_BYTES.length);
stream[10] = COMPRESSED_DATA_FLAG;
int length = compressed.length + 4;
stream[11] = (byte) length;
stream[12] = (byte) (length >>> 8);
@ -290,7 +328,8 @@ public class SnappyFramedStreamTest {
@Test
public void testLargerFrames_compressed_smaller_raw_larger()
throws IOException {
throws IOException
{
final byte[] random = getRandom(0.5, 100000);
final byte[] compressed = Snappy.compress(random);
@ -300,7 +339,7 @@ public class SnappyFramedStreamTest {
System.arraycopy(HEADER_BYTES, 0, stream, 0, HEADER_BYTES.length);
stream[10] = COMPRESSED_DATA_FLAG;
int length = compressed.length + 4;
stream[11] = (byte) length;
stream[12] = (byte) (length >>> 8);
@ -319,19 +358,23 @@ public class SnappyFramedStreamTest {
assertArrayEquals(random, uncompressed);
}
private byte[] uncompressBlock(byte[] block) throws IOException {
private byte[] uncompressBlock(byte[] block)
throws IOException
{
return uncompress(blockToStream(block));
}
private static byte[] blockToStream(byte[] block) {
private static byte[] blockToStream(byte[] block)
{
byte[] stream = new byte[HEADER_BYTES.length + block.length];
System.arraycopy(HEADER_BYTES, 0, stream, 0, HEADER_BYTES.length);
System.arraycopy(block, 0, stream, HEADER_BYTES.length, block.length);
return stream;
}
protected byte[] compress(byte[] original) throws IOException {
protected byte[] compress(byte[] original)
throws IOException
{
ByteArrayOutputStream out = new ByteArrayOutputStream();
OutputStream snappyOut = createOutputStream(out);
snappyOut.write(original);
@ -339,29 +382,35 @@ public class SnappyFramedStreamTest {
return out.toByteArray();
}
protected byte[] uncompress(byte[] compressed) throws IOException {
return toByteArray(createInputStream(new ByteArrayInputStream(
protected byte[] uncompress(byte[] compressed)
throws IOException
{
return toByteArray(createInputStream(new ByteArrayInputStream(
compressed), true));
}
private static byte[] toByteArray(InputStream createInputStream) throws IOException {
private static byte[] toByteArray(InputStream createInputStream)
throws IOException
{
final ByteArrayOutputStream baos = new ByteArrayOutputStream(64 * 1024);
final byte[] buffer = new byte[8 * 1024];
int read;
while((read = createInputStream.read(buffer)) > 0) {
while ((read = createInputStream.read(buffer)) > 0) {
baos.write(buffer, 0, read);
}
return baos.toByteArray();
}
static int toInt(byte value) {
static int toInt(byte value)
{
return value & 0xFF;
}
private byte[] getRandom(double compressionRatio, int length) {
private byte[] getRandom(double compressionRatio, int length)
{
RandomGenerator gen = new RandomGenerator(
compressionRatio);
gen.getNextPosition(length);
@ -369,5 +418,4 @@ public class SnappyFramedStreamTest {
assertEquals(random.length, length);
return random;
}
}

View File

@ -1,179 +1,207 @@
/*--------------------------------------------------------------------------
* 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;
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);
}
@Test
public void readSnappyCompressResult()
throws Exception
{
byte[] orig = readResourceFile("alice29.txt");
byte[] compressed = Snappy.compress(orig);
SnappyInputStream in = new SnappyInputStream(new ByteArrayInputStream(compressed));
byte[] uncompressed = readFully(in);
assertArrayEquals(orig, uncompressed);
}
}

View File

@ -1,125 +1,137 @@
/*--------------------------------------------------------------------------
* 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));
}
}

View File

@ -29,8 +29,14 @@ import static org.junit.Assert.*;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.nio.ByteOrder;
import org.junit.Test;
import org.xerial.snappy.buffer.BufferAllocatorFactory;
import org.xerial.snappy.buffer.CachedBufferAllocator;
import org.xerial.snappy.buffer.DefaultBufferAllocator;
import org.xerial.util.FileResource;
import org.xerial.util.log.Logger;
@ -39,7 +45,9 @@ public class SnappyOutputStreamTest
private static Logger _logger = Logger.getLogger(SnappyOutputStreamTest.class);
@Test
public void test() throws Exception {
public void test()
throws Exception
{
ByteArrayOutputStream buf = new ByteArrayOutputStream();
SnappyOutputStream sout = new SnappyOutputStream(buf);
@ -49,7 +57,7 @@ public class SnappyOutputStreamTest
ByteArrayOutputStream orig = new ByteArrayOutputStream();
byte[] tmp = new byte[1024];
for (int readBytes = 0; (readBytes = input.read(tmp)) != -1;) {
for (int readBytes = 0; (readBytes = input.read(tmp)) != -1; ) {
sout.write(tmp, 0, readBytes);
orig.write(tmp, 0, readBytes); // preserve the original data
}
@ -63,7 +71,7 @@ public class SnappyOutputStreamTest
ByteArrayOutputStream decompressed = new ByteArrayOutputStream();
byte[] compressed = buf.toByteArray();
// decompress
for (int cursor = SnappyCodec.headerSize(); cursor < compressed.length;) {
for (int cursor = SnappyCodec.headerSize(); cursor < compressed.length; ) {
int chunkSize = SnappyOutputStream.readInt(compressed, cursor);
cursor += 4;
byte[] tmpOut = new byte[Snappy.uncompressedLength(compressed, cursor, chunkSize)];
@ -78,7 +86,9 @@ public class SnappyOutputStreamTest
}
@Test
public void bufferSize() throws Exception {
public void bufferSize()
throws Exception
{
ByteArrayOutputStream b = new ByteArrayOutputStream();
SnappyOutputStream os = new SnappyOutputStream(b, 1500);
final int bytesToWrite = 5000;
@ -91,18 +101,21 @@ public class SnappyOutputStreamTest
os.close();
SnappyInputStream is = new SnappyInputStream(new ByteArrayInputStream(b.toByteArray()));
byte[] buf = new byte[bytesToWrite / 101];
while (is.read(buf) != -1) {}
while (is.read(buf) != -1) {
}
is.close();
}
@Test
public void smallWrites() throws Exception {
public void smallWrites()
throws Exception
{
byte[] orig = CalgaryTest.readFile("alice29.txt");
ByteArrayOutputStream b = new ByteArrayOutputStream();
SnappyOutputStream out = new SnappyOutputStream(b);
for(byte c : orig) {
for (byte c : orig) {
out.write(c);
}
out.close();
@ -111,7 +124,7 @@ public class SnappyOutputStreamTest
byte[] decompressed = new byte[orig.length];
int cursor = 0;
int readLen = 0;
for(int i=0; i < decompressed.length && (readLen = is.read(decompressed, i, decompressed.length-i)) != -1; ) {
for (int i = 0; i < decompressed.length && (readLen = is.read(decompressed, i, decompressed.length - i)) != -1; ) {
i += readLen;
}
is.close();
@ -120,11 +133,14 @@ public class SnappyOutputStreamTest
/**
* Compress the input array by passing it chunk-by-chunk to a SnappyOutputStream.
*
* @param orig the data to compress
* @param maxChunkSize the maximum chunk size, in bytes.
* @return the compressed bytes
*/
private static byte[] compressAsChunks(byte[] orig, int maxChunkSize) throws Exception {
private static byte[] compressAsChunks(byte[] orig, int maxChunkSize)
throws Exception
{
ByteArrayOutputStream b = new ByteArrayOutputStream();
SnappyOutputStream out = new SnappyOutputStream(b);
@ -138,7 +154,9 @@ public class SnappyOutputStreamTest
}
@Test
public void batchingOfWritesShouldNotAffectCompressedDataSize() throws Exception {
public void batchingOfWritesShouldNotAffectCompressedDataSize()
throws Exception
{
// Regression test for issue #100, a bug where the size of compressed data could be affected
// by the batching of writes to the SnappyOutputStream rather than the total amount of data
// written to the stream.
@ -147,9 +165,12 @@ public class SnappyOutputStreamTest
byte[] expectedCompressedData = compressAsChunks(orig, Integer.MAX_VALUE);
// Hardcoding an expected compressed size here will catch regressions that lower the
// compression quality:
assertEquals(91013, expectedCompressedData.length);
if (ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN)
assertEquals(90943, expectedCompressedData.length);
else
assertEquals(91013, expectedCompressedData.length);
// The chunk size should not affect the size of the compressed output:
int[] chunkSizes = new int[] { 1, 100, 1023, 1024, 10000};
int[] chunkSizes = new int[] {1, 100, 1023, 1024, 10000};
for (int chunkSize : chunkSizes) {
byte[] compressedData = compressAsChunks(orig, chunkSize);
assertEquals(String.format("when chunk size = %,d", chunkSize), expectedCompressedData.length, compressedData.length);
@ -158,7 +179,97 @@ public class SnappyOutputStreamTest
}
@Test
public void longArrayCompress() throws Exception {
public void closeShouldBeIdempotent()
throws Exception
{
// Regression test for issue #107, a bug where close() was non-idempotent and would release
// its buffers to the allocator multiple times, which could cause scenarios where two open
// SnappyOutputStreams could share the same buffers, leading to stream corruption issues.
final BufferAllocatorFactory bufferAllocatorFactory = CachedBufferAllocator.getBufferAllocatorFactory();
final int BLOCK_SIZE = 4096;
// Create a stream, use it, then close it once:
ByteArrayOutputStream ba1 = new ByteArrayOutputStream();
SnappyOutputStream os1 = new SnappyOutputStream(ba1, BLOCK_SIZE, bufferAllocatorFactory);
os1.write(42);
os1.close();
// Create a new output stream, which should end up re-using the first stream's freed buffers
ByteArrayOutputStream ba2 = new ByteArrayOutputStream();
SnappyOutputStream os2 = new SnappyOutputStream(ba2, BLOCK_SIZE, bufferAllocatorFactory);
// Close the first stream a second time, which is supposed to be safe due to idempotency:
os1.close();
// Allocate a third output stream, which is supposed to get its own fresh set of buffers:
ByteArrayOutputStream ba3 = new ByteArrayOutputStream();
SnappyOutputStream os3 = new SnappyOutputStream(ba3, BLOCK_SIZE, bufferAllocatorFactory);
// Since the second and third streams should have distinct sets of buffers, writes to these
// streams should not interfere with one another:
os2.write(2);
os3.write(3);
os2.close();
os3.close();
SnappyInputStream in2 = new SnappyInputStream(new ByteArrayInputStream(ba2.toByteArray()));
assertEquals(2, in2.read());
in2.close();
SnappyInputStream in3 = new SnappyInputStream(new ByteArrayInputStream(ba3.toByteArray()));
assertEquals(3, in3.read());
in3.close();
}
@Test
public void writingToClosedStreamShouldThrowIOException()
throws IOException
{
ByteArrayOutputStream b = new ByteArrayOutputStream();
SnappyOutputStream os = new SnappyOutputStream(b);
os.close();
try {
os.write(4);
fail("Expected write() to throw IOException");
}
catch (IOException e) {
// Expected exception
}
try {
os.write(new int[] {1, 2, 3, 4});
fail("Expected write() to throw IOException");
}
catch (IOException e) {
// Expected exception
}
}
@Test
public void flushingClosedStreamShouldThrowIOException()
throws IOException
{
ByteArrayOutputStream b = new ByteArrayOutputStream();
SnappyOutputStream os = new SnappyOutputStream(b);
os.close();
try {
os.flush();
}
catch (IOException e) {
// Expected exception
}
}
@Test
public void closingStreamShouldMakeBuffersEligibleForGarbageCollection()
throws IOException
{
ByteArrayOutputStream b = new ByteArrayOutputStream();
SnappyOutputStream os = new SnappyOutputStream(b, 4095, DefaultBufferAllocator.factory);
WeakReference<byte[]> inputBuffer = new WeakReference<byte[]>(os.inputBuffer);
WeakReference<byte[]> outputBuffer = new WeakReference<byte[]>(os.inputBuffer);
os.close();
System.gc();
assertNull(inputBuffer.get());
assertNull(outputBuffer.get());
}
@Test
public void longArrayCompress()
throws Exception
{
long[] l = new long[10];
for (int i = 0; i < l.length; ++i) {
l[i] = i % 3 + i * 11;
@ -176,15 +287,16 @@ public class SnappyOutputStreamTest
assertEquals(10 * 8, readBytes);
assertArrayEquals(l, l2);
}
@Test
public void writeDoubleArray() throws Exception {
public void writeDoubleArray()
throws Exception
{
ByteArrayOutputStream b = new ByteArrayOutputStream();
SnappyOutputStream os = new SnappyOutputStream(b);
double[] orig = new double[] { 1.0, 2.0, 1.4, 0.00343430014, -4.4, 4e-20 };
double[] orig = new double[] {1.0, 2.0, 1.4, 0.00343430014, -4.4, 4e-20};
os.write(orig);
os.close();
@ -197,11 +309,13 @@ public class SnappyOutputStreamTest
}
@Test
public void writeFloatArray() throws Exception {
public void writeFloatArray()
throws Exception
{
ByteArrayOutputStream b = new ByteArrayOutputStream();
SnappyOutputStream os = new SnappyOutputStream(b);
float[] orig = new float[] { 1.0f, 2.0f, 1.4f, 0.00343430014f, -4.4f, 4e-20f };
float[] orig = new float[] {1.0f, 2.0f, 1.4f, 0.00343430014f, -4.4f, 4e-20f};
os.write(orig);
os.close();
@ -214,11 +328,13 @@ public class SnappyOutputStreamTest
}
@Test
public void writeIntArray() throws Exception {
public void writeIntArray()
throws Exception
{
ByteArrayOutputStream b = new ByteArrayOutputStream();
SnappyOutputStream os = new SnappyOutputStream(b);
int[] orig = new int[] { 0, -1, -34, 43, 234, 34324, -234 };
int[] orig = new int[] {0, -1, -34, 43, 234, 34324, -234};
os.write(orig);
os.close();
@ -231,11 +347,13 @@ public class SnappyOutputStreamTest
}
@Test
public void writeShortArray() throws Exception {
public void writeShortArray()
throws Exception
{
ByteArrayOutputStream b = new ByteArrayOutputStream();
SnappyOutputStream os = new SnappyOutputStream(b);
short[] orig = new short[] { 0, -1, -34, 43, 234, 324, -234 };
short[] orig = new short[] {0, -1, -34, 43, 234, 324, -234};
os.write(orig);
os.close();
@ -246,5 +364,4 @@ public class SnappyOutputStreamTest
assertArrayEquals(orig, uncompressed);
}
}

View File

@ -1,304 +1,332 @@
/*--------------------------------------------------------------------------
* 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);
}
}
}

View File

@ -1 +1 @@
version in ThisBuild := "1.1.2-SNAPSHOT"
version in ThisBuild := "1.1.3-SNAPSHOT"