package com.l7tech.common.io;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.security.DigestOutputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class DigestZipOutputStream extends OutputStream {
   private OutputStream a;
   private ByteCountOutputStream b;
   private ZipOutputStream c;
   private ByteCountOutputStream d;
   private OutputStream e;
   private HashMap<String, DigestOutputStream> f = new HashMap<>();

   public DigestZipOutputStream(OutputStream var1, List<String> var2) throws NoSuchAlgorithmException {
      this.a = new BufferedOutputStream(var1);
      this.b = new ByteCountOutputStream(this.a);
      this.c = new ZipOutputStream(this.b);
      this.d = new ByteCountOutputStream(this.c);
      this.e = this.d;

      for (String var4 : var2) {
         MessageDigest var5 = MessageDigest.getInstance(var4);
         this.e = new DigestOutputStream(this.e, var5);
         this.f.put(var4, (DigestOutputStream)this.e);
      }
   }

   @Override
   public void write(int var1) throws IOException {
      this.e.write(var1);
   }

   @Override
   public void write(byte[] var1) throws IOException {
      this.e.write(var1, 0, var1.length);
   }

   @Override
   public void write(byte[] var1, int var2, int var3) throws IOException {
      this.e.write(var1, var2, var3);
   }

   @Override
   public void flush() throws IOException {
      this.e.flush();
      this.a.flush();
   }

   @Override
   public void close() throws IOException {
      this.e.close();
      this.a.close();
   }

   public byte[] getDigest(String var1) {
      DigestOutputStream var2 = this.f.get(var1);
      return var2 == null ? null : var2.getMessageDigest().digest();
   }

   public void resetDigest(String var1) {
      this.f.get(var1).getMessageDigest().reset();
   }

   public void resetDigests(List<String> var1) {
      for (String var3 : var1) {
         this.resetDigest(var3);
      }
   }

   public void putNextEntry(ZipEntry var1) throws IOException {
      this.c.putNextEntry(var1);
   }

   public void setComment(String var1) {
      this.c.setComment(var1);
   }

   public long getZippedByteCount() {
      return this.b.getByteCount();
   }

   public long getRawByteCount() {
      return this.d.getByteCount();
   }

   public double getCompressionRatio() {
      long var1 = this.b.getByteCount();
      return var1 > 0L ? this.d.getByteCount() / var1 : 1.0;
   }
}
