source: josm/trunk/src/org/tukaani/xz/index/IndexEncoder.java@ 15731

Last change on this file since 15731 was 13350, checked in by stoecker, 7 years ago

see #15816 - add XZ support

File size: 1.7 KB
Line 
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
10package org.tukaani.xz.index;
11
12import java.io.OutputStream;
13import java.io.IOException;
14import java.util.ArrayList;
15import java.util.Iterator;
16import java.util.zip.CheckedOutputStream;
17import org.tukaani.xz.common.EncoderUtil;
18import org.tukaani.xz.XZIOException;
19
20public class IndexEncoder extends IndexBase {
21 private final ArrayList<IndexRecord> records
22 = new ArrayList<IndexRecord>();
23
24 public IndexEncoder() {
25 super(new XZIOException("XZ Stream or its Index has grown too big"));
26 }
27
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
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}
Note: See TracBrowser for help on using the repository browser.