1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/mozglue/NativeZip.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,87 @@ 1.4 +/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*- 1.5 + * This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +package org.mozilla.gecko.mozglue; 1.10 + 1.11 +import org.mozilla.gecko.mozglue.generatorannotations.WrapElementForJNI; 1.12 + 1.13 +import java.io.InputStream; 1.14 +import java.nio.ByteBuffer; 1.15 +import java.util.zip.Inflater; 1.16 +import java.util.zip.InflaterInputStream; 1.17 + 1.18 +public class NativeZip implements NativeReference { 1.19 + private static final int DEFLATE = 8; 1.20 + private static final int STORE = 0; 1.21 + 1.22 + private volatile long mObj; 1.23 + private InputStream mInput; 1.24 + 1.25 + public NativeZip(String path) { 1.26 + mObj = getZip(path); 1.27 + mInput = null; 1.28 + } 1.29 + 1.30 + public NativeZip(InputStream input) { 1.31 + if (!(input instanceof ByteBufferInputStream)) { 1.32 + throw new IllegalArgumentException("Got " + input.getClass() 1.33 + + ", but expected ByteBufferInputStream!"); 1.34 + } 1.35 + ByteBufferInputStream bbinput = (ByteBufferInputStream)input; 1.36 + mObj = getZipFromByteBuffer(bbinput.mBuf); 1.37 + mInput = input; 1.38 + } 1.39 + 1.40 + @Override 1.41 + public void finalize() { 1.42 + release(); 1.43 + } 1.44 + 1.45 + public void close() { 1.46 + release(); 1.47 + } 1.48 + 1.49 + @Override 1.50 + public void release() { 1.51 + if (mObj != 0) { 1.52 + _release(mObj); 1.53 + mObj = 0; 1.54 + } 1.55 + mInput = null; 1.56 + } 1.57 + 1.58 + @Override 1.59 + public boolean isReleased() { 1.60 + return (mObj == 0); 1.61 + } 1.62 + 1.63 + public InputStream getInputStream(String path) { 1.64 + if (isReleased()) { 1.65 + throw new IllegalStateException("Can't get path \"" + path 1.66 + + "\" because NativeZip is closed!"); 1.67 + } 1.68 + return _getInputStream(mObj, path); 1.69 + } 1.70 + 1.71 + private static native long getZip(String path); 1.72 + private static native long getZipFromByteBuffer(ByteBuffer buffer); 1.73 + private static native void _release(long obj); 1.74 + private native InputStream _getInputStream(long obj, String path); 1.75 + 1.76 + @WrapElementForJNI 1.77 + private InputStream createInputStream(ByteBuffer buffer, int compression) { 1.78 + if (compression != STORE && compression != DEFLATE) { 1.79 + throw new IllegalArgumentException("Unexpected compression: " + compression); 1.80 + } 1.81 + 1.82 + InputStream input = new ByteBufferInputStream(buffer, this); 1.83 + if (compression == DEFLATE) { 1.84 + Inflater inflater = new Inflater(true); 1.85 + input = new InflaterInputStream(input, inflater); 1.86 + } 1.87 + 1.88 + return input; 1.89 + } 1.90 +}