use of java.util.zip.Deflater in project robovm by robovm.
the class OldAndroidDeflateTest method bigTest.
/*
* "step" determines how compressible the data is.
*
* Note we must set "nowrap" to false, or the Adler-32 doesn't get
* computed.
*/
private void bigTest(int step, int expectedAdler) throws UnsupportedEncodingException, DataFormatException {
byte[] input = new byte[128 * 1024];
byte[] comp = new byte[128 * 1024 + 512];
byte[] output = new byte[128 * 1024 + 512];
Inflater inflater = new Inflater(false);
Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION, false);
createSample(input, step);
compress(deflater, input, comp);
expand(inflater, comp, (int) deflater.getBytesWritten(), output);
assertEquals(inflater.getBytesWritten(), input.length);
assertEquals(deflater.getAdler(), inflater.getAdler());
assertEquals(deflater.getAdler(), expectedAdler);
}
use of java.util.zip.Deflater in project robovm by robovm.
the class OldAndroidDeflateTest method simpleTest.
/*
* Simple inflate/deflate test, taken from the reference docs for the
* Inflater/Deflater classes.
*/
private void simpleTest() throws UnsupportedEncodingException, DataFormatException {
// Encode a String into bytes
String inputString = "blahblahblah??";
byte[] input = inputString.getBytes("UTF-8");
// Compress the bytes
byte[] output = new byte[100];
Deflater compresser = new Deflater();
compresser.setInput(input);
compresser.finish();
int compressedDataLength = compresser.deflate(output);
// Decompress the bytes
Inflater decompresser = new Inflater();
decompresser.setInput(output, 0, compressedDataLength);
byte[] result = new byte[100];
int resultLength = decompresser.inflate(result);
// Decode the bytes into a String
String outputString = new String(result, 0, resultLength, "UTF-8");
assertEquals(inputString, outputString);
assertEquals(compresser.getAdler(), decompresser.getAdler());
decompresser.end();
}
use of java.util.zip.Deflater in project trex-stateless-gui by cisco-system-traffic-generator.
the class CompressionUtils method compress.
public static byte[] compress(byte[] data) throws IOException {
Deflater deflater = new Deflater();
deflater.setInput(data);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
deflater.finish();
byte[] buffer = new byte[1024];
while (!deflater.finished()) {
int count = deflater.deflate(buffer);
outputStream.write(buffer, 0, count);
}
outputStream.close();
byte[] output = outputStream.toByteArray();
return output;
}
use of java.util.zip.Deflater in project ddf by codice.
the class RestSecurity method deflateAndBase64Encode.
/**
* Deflates a value and Base64 encodes the result.
*
* @param value value to deflate and Base64 encode
* @return String
* @throws IOException if the value cannot be converted
*/
public static String deflateAndBase64Encode(String value) throws IOException {
ByteArrayOutputStream valueBytes = new ByteArrayOutputStream();
try (OutputStream tokenStream = new DeflaterOutputStream(valueBytes, new Deflater(Deflater.DEFLATED, GZIP_COMPATIBLE))) {
tokenStream.write(value.getBytes(StandardCharsets.UTF_8));
tokenStream.close();
return Base64.getEncoder().encodeToString(valueBytes.toByteArray());
}
}
use of java.util.zip.Deflater in project geode by apache.
the class CliUtil method compressBytes.
public static DeflaterInflaterData compressBytes(byte[] input) {
Deflater compresser = new Deflater();
compresser.setInput(input);
compresser.finish();
byte[] buffer = new byte[100];
byte[] result = new byte[0];
int compressedDataLength = 0;
int totalCompressedDataLength = 0;
do {
byte[] newResult = new byte[result.length + buffer.length];
System.arraycopy(result, 0, newResult, 0, result.length);
compressedDataLength = compresser.deflate(buffer);
totalCompressedDataLength += compressedDataLength;
System.arraycopy(buffer, 0, newResult, result.length, buffer.length);
result = newResult;
} while (compressedDataLength != 0);
return new DeflaterInflaterData(totalCompressedDataLength, result);
}
Aggregations