1 /* 2 * IndexEncoder 3 * 4 * Author: Lasse Collin <lasse.collin@tukaani.org> 5 * 6 * This file has been put into the public domain. 7 * You can do whatever you want with this file. 8 */ 9 10 package org.tukaani.xz.index; 11 12 import java.io.OutputStream; 13 import java.io.IOException; 14 import java.util.ArrayList; 15 import java.util.Iterator; 16 import java.util.zip.CheckedOutputStream; 17 import org.tukaani.xz.common.EncoderUtil; 18 import org.tukaani.xz.XZIOException; 19 20 public class IndexEncoder extends IndexBase { 21 private final ArrayList<IndexRecord> records 22 = new ArrayList<IndexRecord>(); 23 IndexEncoder()24 public IndexEncoder() { 25 super(new XZIOException("XZ Stream or its Index has grown too big")); 26 } 27 add(long unpaddedSize, long uncompressedSize)28 public void add(long unpaddedSize, long uncompressedSize) 29 throws XZIOException { 30 super.add(unpaddedSize, uncompressedSize); 31 records.add(new IndexRecord(unpaddedSize, uncompressedSize)); 32 } 33 encode(OutputStream out)34 public void encode(OutputStream out) throws IOException { 35 java.util.zip.CRC32 crc32 = new java.util.zip.CRC32(); 36 CheckedOutputStream outChecked = new CheckedOutputStream(out, crc32); 37 38 // Index Indicator 39 outChecked.write(0x00); 40 41 // Number of Records 42 EncoderUtil.encodeVLI(outChecked, recordCount); 43 44 // List of Records 45 for (IndexRecord record : records) { 46 EncoderUtil.encodeVLI(outChecked, record.unpadded); 47 EncoderUtil.encodeVLI(outChecked, record.uncompressed); 48 } 49 50 // Index Padding 51 for (int i = getIndexPaddingSize(); i > 0; --i) 52 outChecked.write(0x00); 53 54 // CRC32 55 long value = crc32.getValue(); 56 for (int i = 0; i < 4; ++i) 57 out.write((byte)(value >>> (i * 8))); 58 } 59 } 60