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

org.testifyproject.tukaani.xz.index.IndexEncoder Maven / Gradle / Ivy

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

package org.testifyproject.tukaani.xz.index;

import java.org.testifyproject.testifyproject.OutputStream;
import java.org.testifyproject.testifyproject.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.zip.CheckedOutputStream;
import org.testifyproject.tukaani.xz.org.testifyproject.testifyprojectmon.EncoderUtil;
import org.testifyproject.tukaani.xz.XZIOException;

public class IndexEncoder extends IndexBase {
    private final ArrayList records = new ArrayList();

    public IndexEncoder() {
        super(new XZIOException("XZ Stream or its Index has grown too big"));
    }

    public void add(long unpaddedSize, long uncompressedSize)
            throws XZIOException {
        super.add(unpaddedSize, uncompressedSize);
        records.add(new IndexRecord(unpaddedSize, uncompressedSize));
    }

    public void encode(OutputStream out) throws IOException {
        java.util.zip.CRC32 crc32 = new java.util.zip.CRC32();
        CheckedOutputStream outChecked = new CheckedOutputStream(out, crc32);

        // Index Indicator
        outChecked.write(0x00);

        // Number of Records
        EncoderUtil.encodeVLI(outChecked, recordCount);

        // List of Records
        for (Iterator i = records.iterator(); i.hasNext(); ) {
            IndexRecord record = (IndexRecord)i.next();
            EncoderUtil.encodeVLI(outChecked, record.unpadded);
            EncoderUtil.encodeVLI(outChecked, record.uncompressed);
        }

        // Index Padding
        for (int i = getIndexPaddingSize(); i > 0; --i)
            outChecked.write(0x00);

        // CRC32
        long value = crc32.getValue();
        for (int i = 0; i < 4; ++i)
            out.write((byte)(value >>> (i * 8)));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy