Search in sources :

Example 6 with ByteArrayOutputStream

use of com.codeforces.commons.io.ByteArrayOutputStream in project codeforces-commons by Codeforces.

the class LzmaUtil method compress.

public static byte[] compress(byte[] plainBytes) throws IOException {
    InputStream in = new ByteArrayInputStream(plainBytes);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    Encoder encoder = new Encoder();
    encoder.setDictionarySize(1 << 23);
    encoder.setEndMarkerMode(true);
    encoder.setMatchFinder(Encoder.EMatchFinderTypeBT4);
    encoder.setNumFastBytes(0x20);
    encoder.writeCoderProperties(out);
    long fileSize = plainBytes.length;
    for (int i = 0; i < 8; ++i) {
        // noinspection NumericCastThatLosesPrecision
        out.write((int) (fileSize >>> (8 * i)) & 0xFF);
    }
    encoder.code(in, out, -1, -1, null);
    in.close();
    return out.toByteArray();
}
Also used : Encoder(lzma.sdk.lzma.Encoder) ByteArrayOutputStream(com.codeforces.commons.io.ByteArrayOutputStream)

Example 7 with ByteArrayOutputStream

use of com.codeforces.commons.io.ByteArrayOutputStream in project codeforces-commons by Codeforces.

the class LzmaUtil method decompress.

public static byte[] decompress(byte[] compressedBytes) throws IOException {
    InputStream in = new ByteArrayInputStream(compressedBytes);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    int propertiesSize = 5;
    byte[] properties = new byte[propertiesSize];
    if (in.read(properties, 0, propertiesSize) != propertiesSize) {
        throw new IOException("LZMA-input is too short.");
    }
    Decoder decoder = new Decoder();
    if (!decoder.setDecoderProperties(properties)) {
        throw new IOException("Incorrect stream properties.");
    }
    long outSize = 0;
    for (int i = 0; i < 8; i++) {
        int v = in.read();
        if (v < 0) {
            throw new IOException("Can't read stream size.");
        }
        // noinspection IntegerMultiplicationImplicitCastToLong
        outSize |= (long) v << (8 * i);
    }
    if (!decoder.code(in, out, outSize)) {
        throw new IOException("Error in data stream.");
    }
    in.close();
    return out.toByteArray();
}
Also used : ByteArrayOutputStream(com.codeforces.commons.io.ByteArrayOutputStream) Decoder(lzma.sdk.lzma.Decoder)

Example 8 with ByteArrayOutputStream

use of com.codeforces.commons.io.ByteArrayOutputStream in project codeforces-commons by Codeforces.

the class ZipUtil method compress.

public static byte[] compress(byte[] bytes, int level) {
    Deflater deflater = new Deflater();
    deflater.setLevel(level);
    deflater.setInput(bytes);
    deflater.finish();
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(bytes.length);
    byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
    while (!deflater.finished()) {
        outputStream.write(buffer, 0, deflater.deflate(buffer));
    }
    return outputStream.toByteArray();
}
Also used : ByteArrayOutputStream(com.codeforces.commons.io.ByteArrayOutputStream)

Example 9 with ByteArrayOutputStream

use of com.codeforces.commons.io.ByteArrayOutputStream in project codeforces-commons by Codeforces.

the class ZipUtil method decompress.

public static byte[] decompress(byte[] bytes) throws DataFormatException {
    if (bytes.length == 0) {
        return bytes;
    }
    Inflater inflater = new Inflater();
    inflater.setInput(bytes);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
    while (!inflater.finished()) {
        outputStream.write(buffer, 0, inflater.inflate(buffer));
    }
    inflater.end();
    return outputStream.toByteArray();
}
Also used : ByteArrayOutputStream(com.codeforces.commons.io.ByteArrayOutputStream)

Example 10 with ByteArrayOutputStream

use of com.codeforces.commons.io.ByteArrayOutputStream in project codeforces-commons by Codeforces.

the class ZipUtil method getZipEntryBytes.

@Nonnull
public static byte[] getZipEntryBytes(File zipFile, String zipEntryPath) throws IOException {
    ByteArrayOutputStream zipEntryOutputStream = new ByteArrayOutputStream();
    writeZipEntryBytes(zipFile, zipEntryPath, zipEntryOutputStream);
    return zipEntryOutputStream.toByteArray();
}
Also used : ByteArrayOutputStream(com.codeforces.commons.io.ByteArrayOutputStream) Nonnull(javax.annotation.Nonnull)

Aggregations

ByteArrayOutputStream (com.codeforces.commons.io.ByteArrayOutputStream)10 Nonnull (javax.annotation.Nonnull)2 Nullable (javax.annotation.Nullable)2 com.codeforces.commons.io (com.codeforces.commons.io)1 Math.max (com.codeforces.commons.math.Math.max)1 Patterns (com.codeforces.commons.text.Patterns)1 StringUtil (com.codeforces.commons.text.StringUtil)1 de.schlichtherle.truezip.file (de.schlichtherle.truezip.file)1 FsSyncException (de.schlichtherle.truezip.fs.FsSyncException)1 java.io (java.io)1 UnknownHostException (java.net.UnknownHostException)1 Charset (java.nio.charset.Charset)1 StandardCharsets (java.nio.charset.StandardCharsets)1 java.util (java.util)1 java.util.zip (java.util.zip)1 GZIPInputStream (java.util.zip.GZIPInputStream)1 GZIPOutputStream (java.util.zip.GZIPOutputStream)1 Decoder (lzma.sdk.lzma.Decoder)1 Encoder (lzma.sdk.lzma.Encoder)1 ZipFile (net.lingala.zip4j.core.ZipFile)1