Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*- |
michael@0 | 2 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | package org.mozilla.gecko.mozglue; |
michael@0 | 7 | |
michael@0 | 8 | import org.mozilla.gecko.mozglue.generatorannotations.WrapElementForJNI; |
michael@0 | 9 | |
michael@0 | 10 | import java.io.InputStream; |
michael@0 | 11 | import java.nio.ByteBuffer; |
michael@0 | 12 | import java.util.zip.Inflater; |
michael@0 | 13 | import java.util.zip.InflaterInputStream; |
michael@0 | 14 | |
michael@0 | 15 | public class NativeZip implements NativeReference { |
michael@0 | 16 | private static final int DEFLATE = 8; |
michael@0 | 17 | private static final int STORE = 0; |
michael@0 | 18 | |
michael@0 | 19 | private volatile long mObj; |
michael@0 | 20 | private InputStream mInput; |
michael@0 | 21 | |
michael@0 | 22 | public NativeZip(String path) { |
michael@0 | 23 | mObj = getZip(path); |
michael@0 | 24 | mInput = null; |
michael@0 | 25 | } |
michael@0 | 26 | |
michael@0 | 27 | public NativeZip(InputStream input) { |
michael@0 | 28 | if (!(input instanceof ByteBufferInputStream)) { |
michael@0 | 29 | throw new IllegalArgumentException("Got " + input.getClass() |
michael@0 | 30 | + ", but expected ByteBufferInputStream!"); |
michael@0 | 31 | } |
michael@0 | 32 | ByteBufferInputStream bbinput = (ByteBufferInputStream)input; |
michael@0 | 33 | mObj = getZipFromByteBuffer(bbinput.mBuf); |
michael@0 | 34 | mInput = input; |
michael@0 | 35 | } |
michael@0 | 36 | |
michael@0 | 37 | @Override |
michael@0 | 38 | public void finalize() { |
michael@0 | 39 | release(); |
michael@0 | 40 | } |
michael@0 | 41 | |
michael@0 | 42 | public void close() { |
michael@0 | 43 | release(); |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | @Override |
michael@0 | 47 | public void release() { |
michael@0 | 48 | if (mObj != 0) { |
michael@0 | 49 | _release(mObj); |
michael@0 | 50 | mObj = 0; |
michael@0 | 51 | } |
michael@0 | 52 | mInput = null; |
michael@0 | 53 | } |
michael@0 | 54 | |
michael@0 | 55 | @Override |
michael@0 | 56 | public boolean isReleased() { |
michael@0 | 57 | return (mObj == 0); |
michael@0 | 58 | } |
michael@0 | 59 | |
michael@0 | 60 | public InputStream getInputStream(String path) { |
michael@0 | 61 | if (isReleased()) { |
michael@0 | 62 | throw new IllegalStateException("Can't get path \"" + path |
michael@0 | 63 | + "\" because NativeZip is closed!"); |
michael@0 | 64 | } |
michael@0 | 65 | return _getInputStream(mObj, path); |
michael@0 | 66 | } |
michael@0 | 67 | |
michael@0 | 68 | private static native long getZip(String path); |
michael@0 | 69 | private static native long getZipFromByteBuffer(ByteBuffer buffer); |
michael@0 | 70 | private static native void _release(long obj); |
michael@0 | 71 | private native InputStream _getInputStream(long obj, String path); |
michael@0 | 72 | |
michael@0 | 73 | @WrapElementForJNI |
michael@0 | 74 | private InputStream createInputStream(ByteBuffer buffer, int compression) { |
michael@0 | 75 | if (compression != STORE && compression != DEFLATE) { |
michael@0 | 76 | throw new IllegalArgumentException("Unexpected compression: " + compression); |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | InputStream input = new ByteBufferInputStream(buffer, this); |
michael@0 | 80 | if (compression == DEFLATE) { |
michael@0 | 81 | Inflater inflater = new Inflater(true); |
michael@0 | 82 | input = new InflaterInputStream(input, inflater); |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | return input; |
michael@0 | 86 | } |
michael@0 | 87 | } |