Path: blob/main/epkcompiler/src/com/jcraft/jzlib/ZOutputStream.java
8645 views
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */1/*2Copyright (c) 2011 ymnk, JCraft,Inc. All rights reserved.34Redistribution and use in source and binary forms, with or without5modification, are permitted provided that the following conditions are met:671. Redistributions of source code must retain the above copyright notice,8this list of conditions and the following disclaimer.9102. Redistributions in binary form must reproduce the above copyright11notice, this list of conditions and the following disclaimer in12the documentation and/or other materials provided with the distribution.13143. The names of the authors may not be used to endorse or promote products15derived from this software without specific prior written permission.1617THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,18INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND19FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,20INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,21INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT22LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,23OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF24LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING25NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,26EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.27*/2829package com.jcraft.jzlib;30import java.io.*;3132/**33* ZOutputStream34*35* @deprecated use DeflaterOutputStream or InflaterInputStream36*/37@Deprecated38public class ZOutputStream extends FilterOutputStream {3940protected int bufsize=512;41protected int flush=JZlib.Z_NO_FLUSH;42protected byte[] buf=new byte[bufsize];43protected boolean compress;4445protected OutputStream out;46private boolean end=false;4748private DeflaterOutputStream dos;49private Inflater inflater;5051public ZOutputStream(OutputStream out) throws IOException {52super(out);53this.out=out;54inflater = new Inflater();55inflater.init();56compress=false;57}5859public ZOutputStream(OutputStream out, int level) throws IOException {60this(out, level, false);61}6263public ZOutputStream(OutputStream out, int level, boolean nowrap) throws IOException {64super(out);65this.out=out;66Deflater deflater = new Deflater(level, nowrap);67dos = new DeflaterOutputStream(out, deflater);68compress=true;69}7071private byte[] buf1 = new byte[1];72public void write(int b) throws IOException {73buf1[0]=(byte)b;74write(buf1, 0, 1);75}7677public void write(byte b[], int off, int len) throws IOException {78if(len==0) return;79if(compress){80dos.write(b, off, len);81}82else {83inflater.setInput(b, off, len, true);84int err = JZlib.Z_OK;85while(inflater.avail_in>0){86inflater.setOutput(buf, 0, buf.length);87err = inflater.inflate(flush);88if(inflater.next_out_index>0)89out.write(buf, 0, inflater.next_out_index);90if(err != JZlib.Z_OK)91break;92}93if(err != JZlib.Z_OK)94throw new ZStreamException("inflating: "+inflater.msg);95return;96}97}9899public int getFlushMode() {100return flush;101}102103public void setFlushMode(int flush) {104this.flush=flush;105}106107public void finish() throws IOException {108int err;109if(compress){110int tmp = flush;111int flush = JZlib.Z_FINISH;112try{113write("".getBytes(), 0, 0);114}115finally { flush = tmp; }116}117else{118dos.finish();119}120flush();121}122public synchronized void end() {123if(end) return;124if(compress){125try { dos.finish(); } catch(Exception e){}126}127else{128inflater.end();129}130end=true;131}132public void close() throws IOException {133try{134try{finish();}135catch (IOException ignored) {}136}137finally{138end();139out.close();140out=null;141}142}143144public long getTotalIn() {145if(compress) return dos.getTotalIn();146else return inflater.total_in;147}148149public long getTotalOut() {150if(compress) return dos.getTotalOut();151else return inflater.total_out;152}153154public void flush() throws IOException {155out.flush();156}157158}159160161