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();
}
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();
}
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();
}
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();
}
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();
}
Aggregations