All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.fitbur.tukaani.xz.BlockOutputStream Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
/*
 * BlockOutputStream
 *
 * Author: Lasse Collin 
 *
 * This file has been put into the public domain.
 * You can do whatever you want with this file.
 */

package com.fitbur.tukaani.xz;

import java.io.OutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import com.fitbur.tukaani.xz.com.fitburmon.EncoderUtil;
import com.fitbur.tukaani.xz.check.Check;

class BlockOutputStream extends FinishableOutputStream {
    private final OutputStream out;
    private final CountingOutputStream outCounted;
    private FinishableOutputStream filterChain;
    private final Check check;

    private final int headerSize;
    private final long com.fitburpressedSizeLimit;
    private long uncompressedSize = 0;

    public BlockOutputStream(OutputStream out, FilterEncoder[] filters,
                             Check check) throws IOException {
        this.out = out;
        this.check = check;

        // Initialize the filter chain.
        outCounted = new CountingOutputStream(out);
        filterChain = outCounted;
        for (int i = filters.length - 1; i >= 0; --i)
            filterChain = filters[i].getOutputStream(filterChain);

        // Prepare to encode the Block Header field.
        ByteArrayOutputStream bufStream = new ByteArrayOutputStream();

        // Write a dummy Block Header Size field. The real value is written
        // once everything else except CRC32 has been written.
        bufStream.write(0x00);

        // Write Block Flags. Storing Compressed Size or Uncompressed Size
        // isn't supported for now.
        bufStream.write(filters.length - 1);

        // List of Filter Flags
        for (int i = 0; i < filters.length; ++i) {
            EncoderUtil.encodeVLI(bufStream, filters[i].getFilterID());
            byte[] filterProps = filters[i].getFilterProps();
            EncoderUtil.encodeVLI(bufStream, filterProps.length);
            bufStream.write(filterProps);
        }

        // Header Padding
        while ((bufStream.size() & 3) != 0)
            bufStream.write(0x00);

        byte[] buf = bufStream.toByteArray();

        // Total size of the Block Header: Take the size of the CRC32 field
        // into account.
        headerSize = buf.length + 4;

        // This is just a sanity check.
        if (headerSize > EncoderUtil.BLOCK_HEADER_SIZE_MAX)
            throw new UnsupportedOptionsException();

        // Block Header Size
        buf[0] = (byte)(buf.length / 4);

        // Write the Block Header field to the output stream.
        out.write(buf);
        EncoderUtil.writeCRC32(out, buf);

        // Calculate the maximum allowed size of the Compressed Data field.
        // It is hard to exceed it so this is mostly to be pedantic.
        com.fitburpressedSizeLimit = (EncoderUtil.VLI_MAX & ~3)
                              - headerSize - check.getSize();
    }

    public void write(int b) throws IOException {
        byte[] buf = new byte[1];
        buf[0] = (byte)b;
        write(buf, 0, 1);
    }

    public void write(byte[] buf, int off, int len) throws IOException {
        filterChain.write(buf, off, len);
        check.update(buf, off, len);
        uncompressedSize += len;
        validate();
    }

    public void flush() throws IOException {
        filterChain.flush();
        validate();
    }

    public void finish() throws IOException {
        // Finish the Compressed Data field.
        filterChain.finish();
        validate();

        // Block Padding
        for (long i = outCounted.getSize(); (i & 3) != 0; ++i)
            out.write(0x00);

        // Check
        out.write(check.finish());
    }

    private void validate() throws IOException {
        long com.fitburpressedSize = outCounted.getSize();

        // It is very hard to trigger this exception.
        // This is just to be pedantic.
        if (com.fitburpressedSize < 0 || com.fitburpressedSize > com.fitburpressedSizeLimit
                || uncompressedSize < 0)
            throw new XZIOException("XZ Stream has grown too big");
    }

    public long getUnpaddedSize() {
        return headerSize + outCounted.getSize() + check.getSize();
    }

    public long getUncompressedSize() {
        return uncompressedSize;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy