Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Stream of Bytes.  RSS feed

 
Santosh Kumar Nayak
Ranch Hand
Posts: 193
Eclipse IDE IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I am compressing a string into stream of bytes using Zlib package.

Later when I try to convert the compressed bytes into String using below:-


String s = new String(bytesCompressed);
System.out.println("String in compressed form="+s);

Then in this case I am getting non-readable format.

I want to get the readable format for the String.
 
Paul Clapham
Sheriff
Posts: 22374
42
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's true, the array of bytes you get isn't an array of bytes which correspond to printable characters. So converting it to a String that way isn't a good idea.

You might consider Base64-encoding the array of bytes to convert it to text. Or you could convert each byte to its hexadecimal representation and build the string from those.
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
You might consider Base64-encoding the array of bytes to convert it to text. Or you could convert each byte to its hexadecimal representation and build the string from those.


Since the OP is trying to compress the string content this may be self defeating. Base64 encoding will inflate the compressed bytes by approximately 4/3 and hex encoding will inflate by a factor of 2. ASCII85 encoding without the 2 character prefix will likely be the best encoding but even that inflates by approximately 5/4 .

 
Paul Clapham
Sheriff
Posts: 22374
42
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that's true. I should have asked why the OP was trying to convert the array of bytes to a String.
 
Santosh Kumar Nayak
Ranch Hand
Posts: 193
Eclipse IDE IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Is there a way out such that the data compressed using GZIP and Zlib are same(identical) in the compressed form.

Earlier we had used the GZIP and now we are using Zlib and hence we wanted to check for backward compatibility issues.


 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Santosh Kumar Nayak wrote:Hi,

Is there a way out such that the data compressed using GZIP and Zlib are same(identical) in the compressed form.

Earlier we had used the GZIP and now we are using Zlib and hence we wanted to check for backward compatibility issues.




Seems highly unlikely but to be sure you should compare https://www.repository.cam.ac.uk/bitstream/handle/1810/3486/rfc-zlib.html?sequence=5 with http://www.gzip.org/zlib/rfc-gzip.html .

Your best bet for backwards compatibility is to look at the first few bytes of each compressed item to see whether they conform to the GZ|IP or Zlib specifications and then decompress accordingly.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!