Search in sources :

Example 1 with LzmaOutputStream

use of net.contrapunctus.lzma.LzmaOutputStream in project disunity by ata4.

the class BundleWriter method write.

public void write(Bundle bundle, Progress progress) throws IOException {
    this.bundle = bundle;
    // add offset placeholders
    levelOffsetMap.clear();
    bundle.entries().stream().filter(entry -> {
        if (bundle.entries().size() == 1) {
            return true;
        }
        String name = entry.name();
        return name.equals("mainData") || name.startsWith("level");
    }).forEach(entry -> levelOffsetMap.put(entry, new MutablePair<>(0L, 0L)));
    BundleHeader header = bundle.header();
    header.levelByteEnd().clear();
    header.levelByteEnd().addAll(levelOffsetMap.values());
    header.numberOfLevelsToDownload(levelOffsetMap.size());
    // write header
    out.writeStruct(header);
    header.headerSize((int) out.position());
    // write bundle data
    if (header.compressed()) {
        // write data to temporary file
        try (DataWriter outData = DataWriters.forFile(dataFile, CREATE, WRITE, TRUNCATE_EXISTING)) {
            writeData(outData, progress);
        }
        // configure LZMA encoder
        LzmaEncoderProps props = new LzmaEncoderProps();
        // 8 MiB
        props.setDictionarySize(1 << 23);
        // maximum
        props.setNumFastBytes(273);
        props.setUncompressedSize(Files.size(dataFile));
        props.setEndMarkerMode(true);
        // stream the temporary bundle data compressed into the bundle file
        try (OutputStream os = new LzmaOutputStream(new BufferedOutputStream(out.stream()), props)) {
            Files.copy(dataFile, os);
        }
        for (MutablePair<Long, Long> levelOffset : levelOffsetMap.values()) {
            levelOffset.setLeft(out.size());
        }
    } else {
        // write data directly to file
        writeData(out, progress);
    }
    // update header
    int fileSize = (int) out.size();
    header.completeFileSize(fileSize);
    header.minimumStreamedBytes(fileSize);
    out.position(0);
    out.writeStruct(header);
}
Also used : DataWriters(info.ata4.io.DataWriters) OutputStream(java.io.OutputStream) LzmaOutputStream(net.contrapunctus.lzma.LzmaOutputStream) Progress(info.ata4.junity.progress.Progress) DataWriter(info.ata4.io.DataWriter) Files(java.nio.file.Files) StandardOpenOption(java.nio.file.StandardOpenOption) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) MutablePair(org.apache.commons.lang3.tuple.MutablePair) Closeable(java.io.Closeable) Map(java.util.Map) Optional(java.util.Optional) LzmaEncoderProps(info.ata4.io.lzma.LzmaEncoderProps) Path(java.nio.file.Path) InputStream(java.io.InputStream) MutablePair(org.apache.commons.lang3.tuple.MutablePair) OutputStream(java.io.OutputStream) LzmaOutputStream(net.contrapunctus.lzma.LzmaOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) LzmaEncoderProps(info.ata4.io.lzma.LzmaEncoderProps) LzmaOutputStream(net.contrapunctus.lzma.LzmaOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) DataWriter(info.ata4.io.DataWriter)

Aggregations

DataWriter (info.ata4.io.DataWriter)1 DataWriters (info.ata4.io.DataWriters)1 LzmaEncoderProps (info.ata4.io.lzma.LzmaEncoderProps)1 Progress (info.ata4.junity.progress.Progress)1 BufferedOutputStream (java.io.BufferedOutputStream)1 Closeable (java.io.Closeable)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 StandardOpenOption (java.nio.file.StandardOpenOption)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 LzmaOutputStream (net.contrapunctus.lzma.LzmaOutputStream)1 IOUtils (org.apache.commons.io.IOUtils)1 MutablePair (org.apache.commons.lang3.tuple.MutablePair)1