package com.jscape.util.io;

import com.jscape.util.IOTools;

import java.io.IOException;
import java.io.OutputStream;
import java.util.zip.DataFormatException;
import java.util.zip.Inflater;

import static com.jscape.util.Parameters.assertNotNull;

/**
 * @author Alex Usun
 */
public class InflaterOutputStream
        extends OutputStream {

    private static final int DEFAULT_BUFFER_SIZE = 512;
    private static final int INITIAL_BUFFER_SIZE = 1;

    private final OutputStream base;
    private final Inflater inflater;
    private final byte[] inflaterBuffer;
    private final byte[] buffer;
    private boolean closed;

    public InflaterOutputStream(OutputStream base, Inflater inflater, int bufferSize) {
        assertNotNull(base);
        this.base = base;

        assertNotNull(inflater);
        this.inflater = inflater;

        this.inflaterBuffer = new byte[bufferSize];
        this.buffer = new byte[INITIAL_BUFFER_SIZE];
    }

    public InflaterOutputStream(OutputStream base, Inflater inflater) {
        this(base, inflater, DEFAULT_BUFFER_SIZE);
    }

    public InflaterOutputStream(OutputStream base) {
        this(base, new Inflater());
    }

    public void close()
            throws IOException {
        if (this.closed) {
            return;
        }

        try {
            flush();
            this.inflater.end();
        } finally {
            IOTools.close(this.base);
            this.closed = true;
        }
    }

    public void flush()
            throws IOException {
        ensureOpen();

        if (!this.inflater.finished()) {
            try {
                while (!this.inflater.finished() && !this.inflater.needsInput()) {
                    int n = this.inflater.inflate(this.inflaterBuffer, 0, this.inflaterBuffer.length);

                    if (n < 1) {
                        break;
                    }

                    this.base.write(this.inflaterBuffer, 0, n);
                }
            } catch (DataFormatException e) {
                throw IOTools.ioException(e);
            }
        }

        this.base.flush();
    }

    public void write(int b)
            throws IOException {
        this.buffer[0] = (byte) b;
        write(this.buffer, 0, 1);
    }

    public void write(byte[] b, int off, int len)
            throws IOException {
        ensureOpen();

        try {
            for ( ;; ) {
                if (this.inflater.needsInput()) {
                    if (len < 1) {
                        break;
                    }

                    int part = Math.min(len, DEFAULT_BUFFER_SIZE);
                    this.inflater.setInput(b, off, part);
                    off += part;
                    len -= part;
                }

                int decompressedDataLength;

                do {
                    decompressedDataLength = this.inflater.inflate(this.inflaterBuffer, 0, this.inflaterBuffer.length);
                    if (decompressedDataLength > 0) {
                        this.base.write(this.inflaterBuffer, 0, decompressedDataLength);
                    }
                } while (decompressedDataLength > 0);

                if (this.inflater.finished()) {
                    break;
                }

                if (this.inflater.needsDictionary()) {
                    throw new IOException("Missing ZLIB dictionary.");
                }
            }
        } catch (DataFormatException e) {
            throw IOTools.ioException(e);
        }
    }

    private void ensureOpen()
            throws IOException {
        if (this.closed) {
            throw new IOException("Stream closed.");
        }
    }

}
