michael@0: /* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.mozglue; michael@0: michael@0: import org.mozilla.gecko.mozglue.generatorannotations.WrapElementForJNI; michael@0: michael@0: import java.io.InputStream; michael@0: import java.nio.ByteBuffer; michael@0: import java.util.zip.Inflater; michael@0: import java.util.zip.InflaterInputStream; michael@0: michael@0: public class NativeZip implements NativeReference { michael@0: private static final int DEFLATE = 8; michael@0: private static final int STORE = 0; michael@0: michael@0: private volatile long mObj; michael@0: private InputStream mInput; michael@0: michael@0: public NativeZip(String path) { michael@0: mObj = getZip(path); michael@0: mInput = null; michael@0: } michael@0: michael@0: public NativeZip(InputStream input) { michael@0: if (!(input instanceof ByteBufferInputStream)) { michael@0: throw new IllegalArgumentException("Got " + input.getClass() michael@0: + ", but expected ByteBufferInputStream!"); michael@0: } michael@0: ByteBufferInputStream bbinput = (ByteBufferInputStream)input; michael@0: mObj = getZipFromByteBuffer(bbinput.mBuf); michael@0: mInput = input; michael@0: } michael@0: michael@0: @Override michael@0: public void finalize() { michael@0: release(); michael@0: } michael@0: michael@0: public void close() { michael@0: release(); michael@0: } michael@0: michael@0: @Override michael@0: public void release() { michael@0: if (mObj != 0) { michael@0: _release(mObj); michael@0: mObj = 0; michael@0: } michael@0: mInput = null; michael@0: } michael@0: michael@0: @Override michael@0: public boolean isReleased() { michael@0: return (mObj == 0); michael@0: } michael@0: michael@0: public InputStream getInputStream(String path) { michael@0: if (isReleased()) { michael@0: throw new IllegalStateException("Can't get path \"" + path michael@0: + "\" because NativeZip is closed!"); michael@0: } michael@0: return _getInputStream(mObj, path); michael@0: } michael@0: michael@0: private static native long getZip(String path); michael@0: private static native long getZipFromByteBuffer(ByteBuffer buffer); michael@0: private static native void _release(long obj); michael@0: private native InputStream _getInputStream(long obj, String path); michael@0: michael@0: @WrapElementForJNI michael@0: private InputStream createInputStream(ByteBuffer buffer, int compression) { michael@0: if (compression != STORE && compression != DEFLATE) { michael@0: throw new IllegalArgumentException("Unexpected compression: " + compression); michael@0: } michael@0: michael@0: InputStream input = new ByteBufferInputStream(buffer, this); michael@0: if (compression == DEFLATE) { michael@0: Inflater inflater = new Inflater(true); michael@0: input = new InflaterInputStream(input, inflater); michael@0: } michael@0: michael@0: return input; michael@0: } michael@0: }