Debian Codegraph @

src/org/tukaani/xz/index/IndexEncoder.java

raw symbols SCIP
1 // SPDX-License-Identifier: 0BSD
2 // SPDX-FileCopyrightText: The XZ for Java authors and contributors
3 // SPDX-FileContributor: Lasse Collin <[email protected]>
4
5 package org.tukaani.xz.index;
6
7 import java.io.OutputStream;
8 import java.io.IOException;
9 import java.util.ArrayList;
10 import java.util.zip.CheckedOutputStream;
11 import org.tukaani.xz.common.EncoderUtil;
12 import org.tukaani.xz.XZIOException;
13
14 public class IndexEncoder extends IndexBase {
15 private final ArrayList<IndexRecord> records
16 = new ArrayList<IndexRecord>();
17
18 public IndexEncoder() {
19 super(new XZIOException("XZ Stream or its Index has grown too big"));
20 }
21
22 @Override
23 public void add(long unpaddedSize, long uncompressedSize)
24 throws XZIOException {
25 super.add(unpaddedSize, uncompressedSize);
26 records.add(new IndexRecord(unpaddedSize, uncompressedSize));
27 }
28
29 public void encode(OutputStream out) throws IOException {
30 java.util.zip.CRC32 crc32 = new java.util.zip.CRC32();
31 CheckedOutputStream outChecked = new CheckedOutputStream(out, crc32);
32
33 // Index Indicator
34 outChecked.write(0x00);
35
36 // Number of Records
37 EncoderUtil.encodeVLI(outChecked, recordCount);
38
39 // List of Records
40 for (IndexRecord record : records) {
41 EncoderUtil.encodeVLI(outChecked, record.unpadded);
42 EncoderUtil.encodeVLI(outChecked, record.uncompressed);
43 }
44
45 // Index Padding
46 for (int i = getIndexPaddingSize(); i > 0; --i)
47 outChecked.write(0x00);
48
49 // CRC32
50 long value = crc32.getValue();
51 for (int i = 0; i < 4; ++i)
52 out.write((byte)(value >>> (i * 8)));
53 }
54 }