Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Decompression of strings received from socket connection

 
vinita Kh
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My java program communicates with a VB Program using socket connection. I'm using Zlib.dll for compression at VB side & Inflator for decompression at java side. At java side if I define byte[] buff = new byte[256], then if I send a string of size more than 256 bytes, althought compressed size less than 256, then on java side while decompressing, I get only first 256 bytesof the original string, thats true because buffer size is 256.
So to resolve this I tried to increase the buffer size, So now I'm getting decompressed size as 0.
Can nebody explain how to solve this problem.
Here is my decompression routine in Java:
public static String decompress(String inStr){
byte[] b2 = new byte[inStr.length()];
//b2 = inStr.getBytes();
System.out.println(inStr);
try {
b2 = inStr.getBytes("ISO-8859-1");
} catch (java.io.UnsupportedEncodingException e)
{
System.err.println(e);
}

Inflater decompresser = new Inflater(true);
decompresser.setInput(b2,0,b2.length);
byte[] b3 = new byte[4096];
int xxxxx =0;
try {
xxxxx = decompresser.inflate(b3);
}
catch (Exception e) {
System.out.println("decompress exception ...");
e.printStackTrace();
}
System.out.println("decompress ... " + inStr.length() + " out: " + xxxxx);
decompresser.end();
String outStr = new String(b3,0,xxxxx);
outStr.trim();
return outStr;
}

java.util.zip.DataFormatException:Invalid stored block lenght java.util.zip.Inflater.InflateBytes<NativeBytes>
Can anybody please sort this out, whats happening?
Is it because string compression in VB using Zlib.dll resulting different encoding than what java is expecting? It is working fine for a normal string(uncompressed string). But if I send compressed string than this exception is shown.
Any help would be greatly appreciated.
 
raimondas zemaitis
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you receive compressed stuff why your decompress method takes in a String ? I suppose you should operate on an array of bytes, you even try to apply encoding to a input String prior to unzipping it. Unfortunately you do not post code which reads stuff out of socket and it is not clear what happens with it next.
 
vinita Kh
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My VB Program sends compressed data as string from a VB Socket.
Java Socket receivs this data as String.This is the Code for that:
public static void main(String args[]) {
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(12345);
System.out.println("Listening on port: 12345");
} catch (IOException e) {
System.err.println("Could not listen on port:12345");
System.exit(1);
}
Socket clientSocket = null;
try {
clientSocket = serverSocket.accept();
System.out.println("VB Client accepted");
} catch (IOException e) {
System.err.println("Accept failed.");
System.exit(1);
}
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(
clientSocket.getInputStream()));
String inputLine,data,inMsg;

while ((inputLine = in.readLine()) != null) {
data = "";
data = data + inputLine;
while (true) {
//to mark end of line received
if ( data.indexOf(';') == -1 ) {
break;
}
//to empty data in inMsg, & get it ready for nextInput
inMsg = data.substring(0,data.indexOf(";") + 1 );
data = data.substring(inMsg.length());
System.out.println(decompress(inMsg));
}
}
out.close();
in.close();
clientSocket.close();
serverSocket.close();
}
I'm using java 1.4.1-b21 and zlib 1.4.1.
I'm using above decompress routine to decompress it, with slight modification in Encoding instead of "ISO-8859-1", I'm using "Cp1252".
Now This works with small strings, but with large strings say 100 bytes, it throws exception as:
DataFormatException : Too many length or distance symbols
at java.util.zip.InflateBytes<Native Method>
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic