Reputation: 8828
I have a java code which uncompresses (zlib compression) bytes received in the socket connection ... the system is able to uncompress when the Server PC is 32 bit and uses zlib.dll but the same code throws DataFormatException when the Server is changed to 64bit processor & OS - and the Server system uses zlib64.dll to compress.
(This is what the Server-side System Company is saying to us)
According to Server-Side System - it will check whether the Server processor & OS is 32bit or 64bit and then compress the data packets accordingly and send it. My Code:
Inflater inflater = new Inflater();
inflater.setInput(message);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(compHeader.MsgLen);
byte[] buffer = new byte[1024];
while (!inflater.finished()) {
int count = 0;
try {
count = inflater.inflate(buffer); // ERROR LINE - THROWS DATAFORMATEXCEPTION of java.util.zip java package
} catch (DataFormatException ex) {
Logger.getLogger(BroadCastManager.class.getName()).log(Level.SEVERE, null, ex);
}
outputStream.write(buffer, 0, count);
}
try {
outputStream.close();
} catch (IOException ex) {
Logger.getLogger(BroadCastManager.class.getName()).log(Level.SEVERE, null, ex);
}
Can you provide a code alternate which can handle both - 32 bit and 64 bit ?? or say what could be the actual reason of this problem ?
Upvotes: 0
Views: 605
Reputation: 22973
Could it be that the data are not compressed with zlib but rather with gzip?
Take following as example
echo "hello zlib" > /tmp/in
gzip /tmp/in
# will result in /tmp/in.gz
If you try to decompress it like
byte[] buffer = new byte[10000];
byte[] message = Files.readAllBytes(Paths.get("/tmp/in.gz"));
Inflater inflater = new Inflater();
inflater.setInput(message);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
System.out.println("read input as zlib");
try {
outputStream.write(buffer, 0, inflater.inflate(buffer));
Files.write(Paths.get("/tmp/out.txt"), outputStream.toByteArray(),
StandardOpenOption.CREATE);
} catch (DataFormatException ex) {
System.out.println("ex = " + ex);
}
... an exception java.util.zip.DataFormatException: incorrect header check
is thrown.
If you decompress it as gzip like
byte[] buffer = new byte[10000];
Path gzipPath = Paths.get("/tmp/in.gz");
System.out.println("read input as gzip");
try (GZIPInputStream gzip = new GZIPInputStream(Files.newInputStream(gzipPath))){
int read = gzip.read(buffer);
buffer = Arrays.copyOfRange(buffer, 0, read);
Files.write(Paths.get("/tmp/gzip.inflated"), buffer,
StandardOpenOption.CREATE);
}
... the file /tmp/gzip.inflated
will be created.
Upvotes: 1