Author: mturk Date: Tue Aug 30 07:17:59 2011 New Revision: 1163110 URL: http://svn.apache.org/viewvc?rev=1163110&view=rev Log: Add base zlib api
Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/zlib/ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/zlib/Local.java (with props) commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/zlib/LocalStrings.properties (with props) commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/zlib/Zlib.java (with props) commons/sandbox/runtime/trunk/src/main/native/shared/zlib.c (with props) commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestZlib.java (with props) Modified: commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in commons/sandbox/runtime/trunk/src/main/native/Makefile.unx.in Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/zlib/Local.java URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/zlib/Local.java?rev=1163110&view=auto ============================================================================== --- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/zlib/Local.java (added) +++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/zlib/Local.java Tue Aug 30 07:17:59 2011 @@ -0,0 +1,34 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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. + */ + + +package org.apache.commons.runtime.util.zlib; +import org.apache.commons.runtime.util.StringManager; + +/** Util package private constants + */ +class Local +{ + + public static final String Package = "org.apache.commons.runtime.util.zlib"; + public static final StringManager sm; + + static { + sm = StringManager.getManager(Package); + } +} + Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/zlib/Local.java ------------------------------------------------------------------------------ svn:eol-style = native Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/zlib/LocalStrings.properties URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/zlib/LocalStrings.properties?rev=1163110&view=auto ============================================================================== --- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/zlib/LocalStrings.properties (added) +++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/zlib/LocalStrings.properties Tue Aug 30 07:17:59 2011 @@ -0,0 +1,18 @@ +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You 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. + +Z_PARAM_ERROR=Invalid parameter +Z_DATA_ERROR=Data integrity error is detected in the compressed stream + Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/zlib/LocalStrings.properties ------------------------------------------------------------------------------ svn:eol-style = native Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/zlib/Zlib.java URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/zlib/Zlib.java?rev=1163110&view=auto ============================================================================== --- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/zlib/Zlib.java (added) +++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/zlib/Zlib.java Tue Aug 30 07:17:59 2011 @@ -0,0 +1,406 @@ +/* Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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. + */ + +package org.apache.commons.runtime.util.zlib; + +import java.io.IOException; +import java.nio.ByteBuffer; +import org.apache.commons.runtime.InvalidArgumentException; +import org.apache.commons.runtime.InvalidDataException; +import org.apache.commons.runtime.InvalidRangeException; +import org.apache.commons.runtime.OperationNotPermittedException; +import org.apache.commons.runtime.OverflowException; +import org.apache.commons.runtime.Pointer; +import org.apache.commons.runtime.util.StringManager; +import org.apache.commons.runtime.util.Utils; + +/** + * Zlib api. + * + * @author Mladen Turk + * @since Runtime 1.0 + */ +public final class Zlib +{ + /** + * Zlib library version string. + */ + public static final String VERSION; + + private static native String version0(); + + /* Error codes */ + private static final int Z_ERRNO = -1; + private static final int Z_STREAM_ERROR = -2; + private static final int Z_DATA_ERROR = -3; + private static final int Z_MEM_ERROR = -4; + private static final int Z_BUF_ERROR = -5; + private static final int Z_VERSION_ERROR = -6; + + /* Indicates the stream direction */ + private boolean isCompressing = false; + + static { + VERSION = version0(); + } + + private Zlib() + { + // No instance + } + + private static native int deflate0(byte[] src, int spos, + byte[] dst, int dpos, int dlen, + int len, int level) + throws InvalidArgumentException, + OverflowException; + private static native int deflate1(ByteBuffer src, int spos, + ByteBuffer dst, int dpos, int dlen, + int len, int level) + throws InvalidArgumentException, + OverflowException; + private static native int deflate2(long src, long dst, int dlen, + int len, int level) + throws InvalidArgumentException, + InvalidRangeException, + OverflowException; + private static native int inflate0(byte[] src, int spos, + byte[] dst, int dpos, int dlen, + int len) + throws InvalidArgumentException, + InvalidDataException, + OverflowException; + private static native int inflate1(ByteBuffer src, int spos, + ByteBuffer dst, int dpos, int dlen, + int len) + throws InvalidArgumentException, + InvalidDataException, + OverflowException; + private static native int inflate2(long src,long dst, int dlen, + int len) + throws InvalidArgumentException, + InvalidDataException, + OverflowException; + + + /** + * Compress data buffer. + * <p> + * Compression in this manner is a one-shot event, done with a single call + * to this function. There is no mechanism for making additional calls + * to provide extra input data. If you want that kind of mechanism, use + * the low-level interface. + * </p> + * + * @param src the source data buffer. + * @param srcPos starting position in the source buffer. + * @param dest destination buffer that will hold compressed data. + * @param destPos stating position in the destination buffer. + * @param length number of bytes to compress. + * @param level compression between {@code 1} and {@code 9} inclusive. + * + * @return the size of the compressed data. + * + * @throws InvalidArgumentException if any of the argumets is invalid. + * @throws OverflowException if the size of the compressed data exceeds + * the size of the destination buffer. + */ + public static int buffToBuffCompress(byte[] src, + int srcPos, + byte[] dest, + int destPos, + int length, + int level) + throws InvalidArgumentException, + OverflowException + { + if (level < 1 || level > 9) + throw new InvalidArgumentException(); + int destLen = dest.length - destPos; + if (srcPos < 0 || destPos < 0 || destLen < 1 || length < 1 || + srcPos + length > src.length) + throw new ArrayIndexOutOfBoundsException(); + return deflate0(src, srcPos, dest, destPos, destLen, length, level); + } + + /** + * Compress data buffer. + * <p> + * Compression in this manner is a one-shot event, done with a single call + * to this function. There is no mechanism for making additional calls + * to provide extra input data. If you want that kind of mechanism, use + * the low-level interface. + * </p> + * + * @param src the source data buffer. + * @param dst destination buffer that will hold compressed data. + * @param level compression between {@code 1} and {@code 9} inclusive. + * + * @throws InvalidArgumentException if any of the argumets is invalid. + * @throws OverflowException if the size of the compressed data exceeds + * the size of the destination buffer. + */ + public static void buffToBuffCompress(ByteBuffer src, + ByteBuffer dst, + int level) + throws InvalidArgumentException, + OverflowException + { + if (level < 1 || level > 9) + throw new InvalidArgumentException(); + int dstLen = dst.remaining(); + int length = src.remaining(); + if (dstLen < 1 || length < 1) + throw new ArrayIndexOutOfBoundsException(); + int s = deflate1(src, src.position(), dst, dst.position(), + dstLen, length, level); + dst.position(s); + } + + /** + * Compress data buffer. + * <p> + * Compression in this manner is a one-shot event, done with a single call + * to this function. There is no mechanism for making additional calls + * to provide extra input data. If you want that kind of mechanism, use + * the low-level interface. + * </p> + * + * @param src the source data buffer. + * @param srcPos starting position in the source buffer. + * @param dst destination buffer that will hold compressed data. + * @param dstPos stating position in the destination buffer. + * @param length number of bytes to compress. + * @param level compression between {@code 1} and {@code 9} inclusive. + * + * @return the size of the compressed data. + * + * @throws InvalidArgumentException if any of the argumets is invalid. + * @throws OverflowException if the size of the compressed data exceeds + * the size of the destination buffer. + */ + public static long buffToBuffCompress(long src, long dst, + int dstLen, + int length, + int level) + throws InvalidArgumentException, + InvalidRangeException, + OverflowException + { + if (level < 1 || level > 9) + throw new InvalidArgumentException(); + if (src == 0L || dst == 0L) + throw new NullPointerException(); + if (length < 1 || dstLen < 1) + throw new IndexOutOfBoundsException(); + return deflate2(src, dst, dstLen, length, level); + } + + /** + * Compress data buffer. + * <p> + * Compression in this manner is a one-shot event, done with a single call + * to this function. The resulting compressed data is a complete bzip2 + * format data stream. There is no mechanism for making additional calls + * to provide extra input data. If you want that kind of mechanism, use + * the low-level interface. + * </p> + * + * @param src the source data buffer. + * @param srcPos starting position in the source buffer. + * @param dst destination buffer that will hold compressed data. + * @param dstPos stating position in the destination buffer. + * @param length number of bytes to compress. + * @param level compression between {@code 1} and {@code 9} inclusive. + * @return the size of the compressed data. + * + * @throws InvalidArgumentException if any of the argumets is invalid. + * @throws OverflowException if the size of the compressed data exceeds + * the size of the destination buffer. + */ + public static long buffToBuffCompress(Pointer src, + long srcPos, + Pointer dst, + long dstPos, + int length, + int level) + throws InvalidArgumentException, + InvalidRangeException, + OverflowException + { + if (src.isNull() || dst.isNull()) + throw new NullPointerException(); + if (srcPos < 0L || dstPos < 0L) + throw new IndexOutOfBoundsException(); + long srca = src.address() + srcPos; + long dsta = dst.address() + dstPos; + int dlen = Utils.toInteger(dst.sizeof() - dstPos); + return buffToBuffCompress(srca, dsta, dlen, length, level); + } + + /** + * Decompress data buffer. + * <p> + * Because the compression ratio of the compressed data cannot be known + * in advance, there is no easy way to guarantee that the output buffer + * will be big enough. You may of course make arrangements in your code + * to record the size of the uncompressed data, but such a mechanism is + * beyond the scope of this library. + * </p> + * + * @param src the source data buffer. + * @param srcPos starting position in the source buffer. + * @param dest destination buffer that will hold uncompressed data. + * @param destPos stating position in the destination buffer. + * @param length number of bytes to uncompress. + * @return the size of the uncompressed data. + * + * @throws InvalidArgumentException if any of the argumets is invalid. + * @throws InvalidDataException if provided data is corrupt or contains + * invalid zlib data. + * @throws OverflowException if the size of the uncompressed data exceeds + * the size of the destination buffer. + */ + public static int buffToBuffDecompress(byte[] src, + int srcPos, + byte[] dest, + int destPos, + int length) + throws InvalidArgumentException, + InvalidDataException, + OverflowException + { + int destLen = dest.length - destPos; + if (srcPos < 0 || destPos < 0 || destLen < 1 || length < 1 || + srcPos + length > src.length) + throw new ArrayIndexOutOfBoundsException(); + return inflate0(src, srcPos, dest, destPos, destLen, length); + } + + /** + * Decompress data buffer. + * <p> + * Because the compression ratio of the compressed data cannot be known + * in advance, there is no easy way to guarantee that the output buffer + * will be big enough. You may of course make arrangements in your code + * to record the size of the uncompressed data, but such a mechanism is + * beyond the scope of this library. + * </p> + * + * @param src the source data buffer. + * @param dst destination buffer that will hold uncompressed data. + * + * @throws InvalidArgumentException if any of the argumets is invalid. + * @throws InvalidDataException if provided data is corrupt or contains + * invalid compression data. + * @throws OverflowException if the size of the uncompressed data exceeds + * the size of the destination buffer. + */ + public static void buffToBuffDecompress(ByteBuffer src, + ByteBuffer dst) + throws InvalidArgumentException, + InvalidDataException, + OverflowException + { + int dstLen = dst.remaining(); + int length = src.remaining(); + if (dstLen < 1 || length < 1) + throw new ArrayIndexOutOfBoundsException(); + int p = inflate1(src, src.position(), dst, dst.position(), + dstLen, length); + dst.position(p); + } + + /** + * Decompress data buffer. + * <p> + * Because the compression ratio of the compressed data cannot be known + * in advance, there is no easy way to guarantee that the output buffer + * will be big enough. You may of course make arrangements in your code + * to record the size of the uncompressed data, but such a mechanism is + * beyond the scope of this library. + * </p> + * + * @param src the source data buffer. + * @param dst destination buffer that will hold uncompressed data. + * @param dstLen length of the destination buffer. + * @param length number of bytes to uncompress. + * @return the size of the uncompressed data. + * + * @throws InvalidArgumentException if any of the argumets is invalid. + * @throws InvalidDataException if provided data is corrupt or contains + * invalid compressed data. + * @throws OverflowException if the size of the uncompressed data exceeds + * the size of the destination buffer. + */ + public static long buffToBuffDecompress(long src, + long dst, + int dstLen, + int length) + throws InvalidArgumentException, + InvalidDataException, + OverflowException + { + if (src == 0L || dst == 0L) + throw new NullPointerException(); + if (length < 1 || dstLen < 1) + throw new IndexOutOfBoundsException(); + return inflate2(src, dst, dstLen, length); + } + + /** + * Decompress data buffer. + * <p> + * Because the compression ratio of the compressed data cannot be known + * in advance, there is no easy way to guarantee that the output buffer + * will be big enough. You may of course make arrangements in your code + * to record the size of the uncompressed data, but such a mechanism is + * beyond the scope of this library. + * </p> + * + * @param src the source data buffer. + * @param srcPos starting position in the source buffer. + * @param dst destination buffer that will hold uncompressed data. + * @param dstPos stating position in the destination buffer. + * @param length number of bytes to uncompress. + * @return the size of the uncompressed data. + * + * @throws InvalidArgumentException if any of the argumets is invalid. + * @throws InvalidDataException if provided data is corrupt or contains + * invalid compressed data. + * @throws OverflowException if the size of the uncompressed data exceeds + * the size of the destination buffer. + */ + public static long buffToBuffDecompress(Pointer src, + long srcPos, + Pointer dst, + long dstPos, + int length) + throws InvalidArgumentException, + InvalidDataException, + OverflowException + { + if (src.isNull() || dst.isNull()) + throw new NullPointerException(); + if (srcPos < 0L || dstPos < 0L) + throw new IndexOutOfBoundsException(); + long srca = src.address() + srcPos; + long dsta = dst.address() + dstPos; + int dlen = Utils.toInteger(dst.sizeof() - dstPos); + return buffToBuffDecompress(srca, dsta, dlen, length); + } + +} Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/zlib/Zlib.java ------------------------------------------------------------------------------ svn:eol-style = native Modified: commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in?rev=1163110&r1=1163109&r2=1163110&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in (original) +++ commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in Tue Aug 30 07:17:59 2011 @@ -147,6 +147,7 @@ LIBSOURCES=\ $(TOPDIR)\shared\utime.c \ $(TOPDIR)\shared\uuid.c \ $(TOPDIR)\shared\version.c \ + $(TOPDIR)\shared\zlib.c \ $(TOPDIR)\shared\buildmark.c SSLSOURCES=\ Modified: commons/sandbox/runtime/trunk/src/main/native/Makefile.unx.in URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/Makefile.unx.in?rev=1163110&r1=1163109&r2=1163110&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/Makefile.unx.in (original) +++ commons/sandbox/runtime/trunk/src/main/native/Makefile.unx.in Tue Aug 30 07:17:59 2011 @@ -142,6 +142,7 @@ LIBSOURCES=\ $(TOPDIR)/shared/utime.c \ $(TOPDIR)/shared/uuid.c \ $(TOPDIR)/shared/version.c \ + $(TOPDIR)/shared/zlib.c \ $(TOPDIR)/shared/buildmark.c SSLSOURCES=\ Added: commons/sandbox/runtime/trunk/src/main/native/shared/zlib.c URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/zlib.c?rev=1163110&view=auto ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/shared/zlib.c (added) +++ commons/sandbox/runtime/trunk/src/main/native/shared/zlib.c Tue Aug 30 07:17:59 2011 @@ -0,0 +1,327 @@ +/* Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 "acr/api.h" +#include "acr/jniapi.h" +#include "acr/memory.h" +#include "acr/string.h" + +#include "zlib/zlib.h" + +#define ACR_ZLIB_EXPORT(RT, CL, MN) \ + ACR_JNIEXPORT RT JNICALL Java_org_apache_commons_runtime_util_zlib_##CL##_##MN +#define ACR_ZBUFF(S) ((char *)(S) + ACR_ZSIZE) + +static int ACR_ZSIZE; + +typedef struct acr_zstream { + z_stream zs; + unsigned int zlen; + int state; + jboolean eos; + void *next_array; + char *next_data; +} acr_zstream; + + +ACR_ZLIB_EXPORT(jstring, Zlib, version0)(JNI_STDARGS) +{ + return AcrNewJavaStringU(env, zlibVersion()); +} + +ACR_ZLIB_EXPORT(jint, Zlib, deflate0)(JNI_STDARGS, jbyteArray src, jint spos, + jbyteArray dst, jint dpos, jint dlen, + jint len, jint level) +{ + uLongf dstLen; + Bytef *scp; + Bytef *dcp; + int rc; + + dstLen = dlen; + scp = JARRAY_CRITICAL(Bytef, src); + dcp = JARRAY_CRITICAL(Bytef, dst); + if (dcp == 0 || scp == 0) { + RELEASE_CRITICAL(src, scp); + RELEASE_CRITICAL(dst, dcp); + ACR_THROW(ACR_EX_EINVAL, 0); + return 0; + } + + rc = compress2(dcp + dpos, &dstLen, scp + spos, len, level); + RELEASE_CRITICAL(src, scp); + RELEASE_CRITICAL(dst, dcp); + if (rc == Z_OK) + return (jint)dstLen; + else if (rc == Z_BUF_ERROR) + ACR_THROW(ACR_EX_EOVERFLOW, 0); + else + ACR_THROW(ACR_EX_EINVAL, 0); + return 0; +} + +ACR_ZLIB_EXPORT(jlong, Zlib, deflate1)(JNI_STDARGS, jobject src, jint spos, + jobject dst, jint dpos, jint dlen, + jint len, jint level) +{ + uLongf dstLen; + Bytef *scp; + Bytef *dcp; + int rc; + + dstLen = dlen; + if ((scp = (Bytef *)(*env)->GetDirectBufferAddress(env, src)) == 0) { + ACR_THROW(ACR_EX_ENULL, 0); + return 0; + } + if ((dcp = (Bytef *)(*env)->GetDirectBufferAddress(env, dst)) == 0) { + ACR_THROW(ACR_EX_ENULL, 0); + return 0; + } + rc = compress2(dcp + dpos, &dstLen, scp + spos, len, level); + if (rc == Z_OK) + return (jint)dstLen; + else if (rc == Z_BUF_ERROR) + ACR_THROW(ACR_EX_EOVERFLOW, 0); + else + ACR_THROW(ACR_EX_EINVAL, 0); + return 0; +} + +ACR_ZLIB_EXPORT(jint, Zlib, deflate2)(JNI_STDARGS, jlong src, jlong dst, + jint dlen, jint len, jint level) +{ + uLongf dstLen; + Bytef *scp = J2P(src, Bytef *); + Bytef *dcp = J2P(dst, Bytef *); + int rc; + + dstLen = dlen; + rc = compress2(dcp, &dstLen, scp, len, level); + if (rc == Z_OK) + return (jint)dstLen; + else if (rc == Z_BUF_ERROR) + ACR_THROW(ACR_EX_EOVERFLOW, 0); + else + ACR_THROW(ACR_EX_EINVAL, 0); + return 0; +} + +ACR_ZLIB_EXPORT(jint, Zlib, inflate0)(JNI_STDARGS, jbyteArray src, jint spos, + jbyteArray dst, jint dpos, jint dlen, + jint len) +{ + uLongf dstLen; + Bytef *scp; + Bytef *dcp; + int rc; + + dstLen = dlen; + scp = JARRAY_CRITICAL(Bytef, src); + dcp = JARRAY_CRITICAL(Bytef, dst); + if (dcp == 0 || scp == 0) { + RELEASE_CRITICAL(src, scp); + RELEASE_CRITICAL(dst, dcp); + ACR_THROW(ACR_EX_EINVAL, 0); + return 0; + } + + rc = uncompress(dcp + dpos, &dstLen, scp + spos, len); + RELEASE_CRITICAL(src, scp); + RELEASE_CRITICAL(dst, dcp); + if (rc == Z_OK) + return (jint)dstLen; + else if (rc == Z_BUF_ERROR) + ACR_THROW(ACR_EX_EOVERFLOW, 0); + else if (rc == Z_MEM_ERROR) + ACR_THROW(ACR_EX_ENOMEM, 0); + else + ACR_THROW_MSG(ACR_EX_EILSEQ, zError(rc)); + return 0; +} + +ACR_ZLIB_EXPORT(jint, Zlib, inflate1)(JNI_STDARGS, jobject src, jint spos, + jobject dst, jint dpos, jint dlen, + jint len) +{ + uLongf dstLen; + Bytef *scp; + Bytef *dcp; + int rc; + + dstLen = dlen; + if ((scp = (Bytef *)(*env)->GetDirectBufferAddress(env, src)) == 0) { + ACR_THROW(ACR_EX_ENULL, 0); + return 0; + } + if ((dcp = (Bytef *)(*env)->GetDirectBufferAddress(env, dst)) == 0) { + ACR_THROW(ACR_EX_ENULL, 0); + return 0; + } + + rc = uncompress(dcp + dpos, &dstLen, scp + spos, len); + if (rc == Z_OK) + return (jint)dstLen; + else if (rc == Z_BUF_ERROR) + ACR_THROW(ACR_EX_EOVERFLOW, 0); + else if (rc == Z_MEM_ERROR) + ACR_THROW(ACR_EX_ENOMEM, 0); + else + ACR_THROW_MSG(ACR_EX_EILSEQ, zError(rc)); + return 0; +} + +ACR_ZLIB_EXPORT(jlong, Zlib, inflate2)(JNI_STDARGS, jlong src, + jlong dst, jint dlen, + jint len) +{ + uLongf dstLen; + Bytef *scp = J2P(src, Bytef *); + Bytef *dcp = J2P(dst, Bytef *); + int rc; + + dstLen = dlen; + rc = uncompress(dcp, &dstLen, scp, len); + if (rc == Z_OK) + return (jint)dstLen; + else if (rc == Z_BUF_ERROR) + ACR_THROW(ACR_EX_EOVERFLOW, 0); + else if (rc == Z_MEM_ERROR) + ACR_THROW(ACR_EX_ENOMEM, 0); + else + ACR_THROW_MSG(ACR_EX_EILSEQ, zError(rc)); + return 0; +} + +ACR_ZLIB_EXPORT(jint, ZlibImpl, init0)(JNI_STDARGS) +{ + int size = ISIZEOF(acr_zstream); + ACR_ZSIZE = ACR_ALIGN_DEFAULT(size); + return ACR_ZSIZE; +} + +ACR_ZLIB_EXPORT(jlong, ZlibImpl, newHandle)(JNI_STDARGS, jint zsize) +{ + acr_zstream *s; + s = ACR_EALLOC(acr_zstream, zsize); + if (s != 0) + s->zlen = zsize; + return P2J(s); +} + +ACR_ZLIB_EXPORT(jint, ZlibImpl, inflateInit2)(JNI_STDARGS, + jlong stream, + jint windowBits, + jint streamSize) +{ + int rc; + acr_zstream *s = J2P(stream, acr_zstream *); + + rc = inflateInit2((z_streamp)s, windowBits); + s->state = 0; + s->eos = JNI_FALSE; + + return rc; +} + +ACR_ZLIB_EXPORT(jlong, ZlibImpl, deflateInit2)(JNI_STDARGS, jlong stream, + jint level, + jint method, + jint windowBits, + jint memLevel, + jint strategy) +{ + int rc; + acr_zstream *s = J2P(stream, acr_zstream *); + + rc = deflateInit2((z_streamp)s, level, method, windowBits, memLevel, strategy); + s->state = 0; + s->eos = JNI_FALSE; + + return rc; +} + + +ACR_ZLIB_EXPORT(jlong, ZlibImpl, getTotalIn)(JNI_STDARGS, jlong stream) +{ + acr_zstream *s = J2P(stream, acr_zstream *); + jlong rv = (jlong)s->zs.total_in; + + return rv; +} + +ACR_ZLIB_EXPORT(jlong, ZlibImpl, getTotalOut)(JNI_STDARGS, jlong stream) +{ + acr_zstream *s = J2P(stream, acr_zstream *); + return (jlong)s->zs.total_out; +} + +ACR_ZLIB_EXPORT(jint, ZlibImpl, getAvailIn)(JNI_STDARGS, jlong stream) +{ + acr_zstream *s = J2P(stream, acr_zstream *); + return s->zs.avail_in; +} + +ACR_ZLIB_EXPORT(jlong, ZlibImpl, getAvailOut)(JNI_STDARGS, jlong stream) +{ + acr_zstream *s = J2P(stream, acr_zstream *); + return (jlong)s->zs.avail_out; +} + +ACR_ZLIB_EXPORT(jboolean, ZlibImpl, needsInput)(JNI_STDARGS, jlong stream) +{ + acr_zstream *s = J2P(stream, acr_zstream *); + + if (s->state == 0 && s->zs.avail_in == 0) + return JNI_TRUE; + else + return JNI_FALSE; +} + +ACR_ZLIB_EXPORT(jboolean, ZlibImpl, finish)(JNI_STDARGS, jlong stream) +{ + jboolean rv = JNI_FALSE; + acr_zstream *s = J2P(stream, acr_zstream *); + + if (s->state == 0) { + s->state = Z_FINISH; + rv = JNI_TRUE; + } + return rv; +} + +ACR_ZLIB_EXPORT(jboolean, ZlibImpl, finished)(JNI_STDARGS, jlong stream) +{ + acr_zstream *s = J2P(stream, acr_zstream *); + return s->eos; +} + +ACR_ZLIB_EXPORT(void, ZlibDeflater, close0)(JNI_STDARGS, jlong stream) +{ + acr_zstream *s = J2P(stream, acr_zstream *); + deflateEnd((z_streamp)s); + AcrFree(s->next_array); + AcrFree(s); +} + +ACR_ZLIB_EXPORT(void, ZlibInflater, close0)(JNI_STDARGS, jlong stream) +{ + acr_zstream *s = J2P(stream, acr_zstream *); + + inflateEnd((z_streamp)s); + AcrFree(s->next_array); + AcrFree(s); +} Propchange: commons/sandbox/runtime/trunk/src/main/native/shared/zlib.c ------------------------------------------------------------------------------ svn:eol-style = native Added: commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestZlib.java URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestZlib.java?rev=1163110&view=auto ============================================================================== --- commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestZlib.java (added) +++ commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestZlib.java Tue Aug 30 07:17:59 2011 @@ -0,0 +1,51 @@ +/* Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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. + */ + +package org.apache.commons.runtime.util.zlib; + +import java.io.IOException; +import java.io.File; +import org.testng.annotations.*; +import org.testng.Assert; + +public class TestZlib extends Assert +{ + private static final String loremIpsum = + "Lorem ipsum dolor sit amet, consectetur adipisicing elit, " + + "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. " + + "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris " + + "nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in " + + "reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla " + + "pariatur. Excepteur sint occaecat cupidatat non proident, sunt in " + + "culpa qui officia deserunt mollit anim id est laborum."; + + + @Test(groups = { "core" }) + public void bufToBuffCompressing() + throws Exception + { + byte[] cd = loremIpsum.getBytes(); + byte[] dd = new byte[500]; + byte[] dc = new byte[loremIpsum.length()]; + + int sc = Zlib.buffToBuffCompress(cd, 0, dd, 0, cd.length, 9); + // 270 is pre-computed length. + assertEquals(sc, 270); + int sd = Zlib.buffToBuffDecompress(dd, 0, dc, 0, sc); + assertEquals(sd, loremIpsum.length()); + } + +} Propchange: commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestZlib.java ------------------------------------------------------------------------------ svn:eol-style = native