🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.api
Article #2734

Re: packing and unpacking with zlib

#2734
From: John P. McGrath
Date: Thu, 04 May 2000 00:00
95 lines
3636 bytes
I'm not sure, but I think your problem is due to conversions between byte 
arrays and Java Strings.  Your Deflate() method properly compresses its 
argument into an array of bytes, but then it converts it to a string.  

This conversion will assume that the input is encoded using the default 
encoding for your locale (which is probably UTF-8).  Then in Inflate(), 
you re-encode that string back into a byte array.  These two conversions 
would result in the same byte array if the input were a valid UTF-8 byte 
array, but I am not sure that will work for any arbitrary bytes.

I see one other potential problem with this code.  In the Inflate() 
method, the code reads the UTF-8 byte sequence in chunks of 1024 bytes, 
and decodes each chunk separately.  Since the UTF-8 encoding can result 
in up to 4 bytes per character, there is no guarantee that the encoded 
characters will be aligned on 1024 byte boundaries.  This could result in 
corruption at the boundary positions.

-- 
John P. McGrath
<JPMcGrath@fast.net>
<jpm@nscs.fast.net>


On Wed, 3 May 2000 16:00:16 -0700, Mike Evteev wrote:

> Hello All!
> 
> I must rewrite code wich pack/unpack data with zlib (Delphi version);
> 
> I spend a lot of time with no results, becouse it my firs project on java
> 
> My code is here.
> 
> import java.lang.String;
> import java.io.*;
> import java.util.zip.*;
> 
> 
> public class Compressor {
> 
>   public static void main (String args[]){
>     System.out.println(Inflate(Deflate(args[0])));
>   };
> 
>   public static String Deflate(String InBuffer) {
>   ByteArrayOutputStream FOut = new ByteArrayOutputStream(InBuffer.length());
>   try {
>     DeflaterOutputStream myStream = new DeflaterOutputStream(FOut, new
> Deflater(Deflater.BEST_COMPRESSION, true));
>     myStream.write(InBuffer.getBytes(), 0, InBuffer.length());
>     myStream.finish();
>     myStream.close();
>     FOut.close();
>   }
>   catch (java.io.IOException e){
>     System.out.println(e);
>   }
>   return FOut.toString();
> } // Deflate
> 
>   public static String Inflate(String InBuffer) {
>   StringBuffer Res = new StringBuffer(0);
>   byte[] Tmp = new byte[1024];
>   ByteArrayInputStream FIn = new ByteArrayInputStream(InBuffer.getBytes());
>   try {
>     InflaterInputStream myStream = new InflaterInputStream(FIn, new
> Inflater(true), InBuffer.length() + 1);
>     while (myStream.read(Tmp, 0, 1024) != -1) {
>       Res = Res.append(Tmp.toString());
>     }
>   }
>   catch (java.io.IOException e){
>     System.out.println(e);
>   }
>   return Res.toString();
> } // Inflate
> 
> } file://Compressor
> 
> If I use DeflaterOutputStream myStream = new DeflaterOutputStream(FOut, new
> Deflater(Deflater.BEST_COMPRESSION, false)) on packing and
>  InflaterInputStream myStream = new InflaterInputStream(FIn, new
> Inflater(true), InBuffer.length() + 1);
> on unpacking I get -
> 
> java.util.zip.ZipException: invalid stored block lengths
> 
> If I use true and true I get
> 
> java.io.EOFException: Unexpected end of ZLIB input stream
> 
> Help me please! It's wery urgent!
> 
> Mike
Technical Details
Message-ID: <MPG.137ae3ebb209c8a19898d9@forums.borland.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <8eqafu$sps8@bornews.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 1089d0,c79989b112f3b01f
X-Google-Attributes: gid1089d0,public
From: John P. McGrath <JPMcGrath@fast.net>
Subject: Re: packing and unpacking with zlib
Date: 2000/05/04
Message-ID: <MPG.137ae3ebb209c8a19898d9@forums.borland.com>
X-Deja-AN: 619039268
References: <8eqafu$sps8@bornews.borland.com>
Organization: Another Netscape Collabra Server User
Newsgroups: borland.public.jbuilder.java.api