This is the mail archive of the java@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Possible bug with java.lang.String?



I'm not sure if it's a known bug(or a feature), or perhaps i'm just doing
something wrong here. In anycase I couldn't find  anything about it in the
archives.

I'm writing a cache application that does in-memory compression for large
Strings.

I've used Deflater/Inflater from java.util.zip package to compress data
from the string, and push it back to the string.

When decompression is needed, the compressed data is extracted from the
String, uncompressed, and moved back.

In J2SE 1.3 it worked just fine, however with gcj-3.0 exception was
thrown, regarding corrupted header.

I have built a test class(attached), and it seems that:
A) compression/decompression without conversion to String work great with
gcj.
B) When the compressed byte array is converted to String, some of the
information get currupted, so it cannot be used for the decompression.

Another option is that I'm a stupid troll and I did something silly in my
code:)


Idan
import java.io.*;
import java.util.zip.*;



public class ziptest
{
   public static String defaultString="Hello World";
   public static void main(String[] argv) 
     {
	String in;
	try
	  {
	     in=argv[0];
	  }
	catch(ArrayIndexOutOfBoundsException e)
	  {
	     in=defaultString;
	  }	
	String comp;
	System.out.println("Compress/Decompress using byte arrays only:");
	System.out.println("Result: "+ZipUtils.byteOnly(in));
	System.out.println("");
	System.out.println("Compress/Decompress using String as storage:");
	System.out.println("Source: "+in);
	comp=ZipUtils.compress(in);
	System.out.println("Compressed: "+comp);
	in=ZipUtils.decompress(comp);
	System.out.println("Result: "+in);
     }
   
}


class ZipUtils
{
   
   public static String byteOnly(String trg)
     {
	try
	  {
	     byte[] buf;
	     int l;
	     Deflater comp=new Deflater();	     
	     comp.setInput(trg.getBytes());
	     comp.finish();
	     
	     
	     buf=new byte[1024];
	     l=comp.deflate(buf);
	     Inflater decomp=new Inflater();	     
	     decomp.setInput(buf,0,l);
	     
	     
	     buf=new byte[1024];
	     l=decomp.inflate(buf);
	     return new String(buf);
	  }
	catch(Exception e)
	  {
	     System.out.println(e);
	     return "";
	  }	
     }
   
   
   public static String compress(String trg)
     {
	try
	  {
	     byte[] buf;
	     int l;
	     Deflater comp=new Deflater();	     
	     comp.setInput(trg.getBytes());
	     comp.finish();
	     
	     
	     buf=new byte[1024];
	     l=comp.deflate(buf);
	     System.out.print("Byte array: ");
	     for(int i=0;i<buf.length;++i)
	       {
		  System.out.write(buf[i]);
	       }
	     System.out.print("\n");
	     
	     return new String(buf,0,l);
	  }
	catch(Exception e)
	  {
	     System.out.println(e);
	     return "";
	  }	
     }
   
   
   
   public static String decompress(String trg)
     {
	try
	  {
	     byte[] buf;
	     int l;
	     Inflater decomp=new Inflater();	     
	     decomp.setInput(trg.getBytes());
	     
	     
	     buf=new byte[1024];
	     l=decomp.inflate(buf);
	     return new String(buf,0,l);
	  }
	catch(Exception e)
	  {
	     System.out.println(e);
	     return "";
	  }	
     }
}

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]