I have the following test code to implement the sending of XMLs with compressed byte arrays in them:
byte[] data = null;
String prova="";
try {
System.out.println("in: " + byteBuffer.array().length / 1024 + " Kb");
data=CompressionUtils.compress(byteBuffer.array());
prova=new String(data,"UTF-8");
System.out.println("out: " + data.length / 1024 + " Kb");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte[] dataDec = null;
try {
System.out.println("in: " + data.length / 1024 + " Kb");
dataDec=CompressionUtils.decompress(data);
System.out.println("out: " + dataDec.length / 1024 + " Kb");
System.out.println("UTF step");
System.out.println("in: " + prova.getBytes("UTF-8").length / 1024 + " Kb");
dataDec=CompressionUtils.decompress(prova.getBytes("UTF-8"));
System.out.println("out: " + dataDec.length / 1024 + " Kb");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DataFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
where data is something I need to compress and send as xml..
While compressing and decompressing the raw byte[] data, if I convert de above compressed data in utf-8 for later usage. The moment I try to get my byte[] data back, when decompressed I get:
in: 1250 Kb
Original: 1250 Kb
Compressed: 4 Kb
out: 4 Kb
in: 4 Kb
Original: 4393b
DeCompressed: 1280000b
out: 1250 Kb
UTF step
in: 10 Kb
java.util.zip.DataFormatException: incorrect header check
at java.util.zip.Inflater.inflateBytes(Native Method)
at java.util.zip.Inflater.inflate(Unknown Source)
at java.util.zip.Inflater.inflate(Unknown Source)
at ygg.commons.compression.CompressionUtils.decompress(CompressionUtils.java:52)
at ygg.view.video.display.main(display.java:193)
for complete reference:
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); // returns the generated code... index
outputStream.write(buffer, 0, count);
}
outputStream.close();
byte[] output = outputStream.toByteArray();
System.out.println("Original: " + data.length / 1024 + " Kb");
System.out.println("Compressed: " + output.length / 1024 + " Kb");
return output;
}
public static byte[] decompress(byte[] data) throws IOException, DataFormatException {
Inflater inflater = new Inflater();
inflater.setInput(data);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
byte[] buffer = new byte[1024];
while (!inflater.finished()) {
int count = inflater.inflate(buffer);
outputStream.write(buffer, 0, count);
}
outputStream.close();
byte[] output = outputStream.toByteArray();
System.out.println("Original: " + data.length);
System.out.println("DeCompressed: " + output.length);
return output;
}
My question is the transformation
byte[]
=>deflated byte[]
=>UTF-8 String
=>byte[] (as UTF-8)
=>inflated (original) byte[] is possible?
What did I do wrong? Also now that I notice as a UTF-8 String the size is more that doubled, any room for improvement?