Deflater/Inflater Anomaly

Wrote a small class to provide deflate and inflate functionality to other classes, ran a few files through it and found that the uncompressed file was one byte longer than the input file. That byte showed up as a "?" at the end of the utf-8 file. Why is this?
Code:
package com.sra.pipeline.compression;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.zip.Deflater;
import java.util.zip.DeflaterInputStream;
import java.util.zip.Inflater;
import java.util.zip.InflaterInputStream;
public class Compressor {
     private String input = "input/aabqirL4Ob.html";
     private String output = "output/aabqirL4Ob.html";
     private String input1 = "input/aabpKgL4Ob.html";
     private String output1 = "output/aabpKgL4Ob.html";
     public Compressor() {
          byte[] bytes = readFile();
          System.out.println("File len " + bytes.length);
          byte[] cbytes = compress(bytes);
          System.out.println("Compressed len " + cbytes.length);
          byte[] fbytes = inflate(cbytes);
          System.out.println("UNCompressed len " + fbytes.length);
          writeFile(fbytes);
     public byte[] readFile() {
          byte[] bytes = null;
          try {
               File file = new File(input1);
               bytes = new byte[(int)file.length()];
               FileInputStream fis = new FileInputStream(file);
               fis.read(bytes);
          } catch (FileNotFoundException e) {
               e.printStackTrace();
          } catch (IOException e) {
               e.printStackTrace();
          return(bytes);
     public byte[] compress(byte[] bytes) {
          ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
          DeflaterInputStream dis = new DeflaterInputStream(bais);
          ByteArrayOutputStream baos = new ByteArrayOutputStream();
          try {
               while(dis.available() == 1) {
                    baos.write(dis.read());
          } catch (IOException e) {
               e.printStackTrace();
          return(baos.toByteArray());
     public byte[] inflate(byte[] cbytes) {
          ByteArrayInputStream bais = new ByteArrayInputStream(cbytes);
          InflaterInputStream iis = new InflaterInputStream(bais);
          ByteArrayOutputStream baos = new ByteArrayOutputStream();
          try {
               while(iis.available() == 1) {
                    baos.write(iis.read());
          } catch (IOException e) {
               e.printStackTrace();
          return(baos.toByteArray());
     public void writeFile(byte[] out) {
          try {
               FileWriter fw = new FileWriter(new File(output1));
               fw.write(new String(out, "utf-8"));
               fw.close();
          } catch (IOException e) {
               e.printStackTrace();
     public static void main(String[] args) {
          new Compressor();
}Thanks for any ideas,
Jim

     public Compressor() {
          byte[] bytes = readFile();Here you are reading the entire file into memory which is already a bad idea.
          byte[] cbytes = compress(bytes);At this point you have both the plaintext and the compressed data in memory at the same time which is now twice as bad.
               bytes = new byte[(int)file.length()];Here you are assuming the file size fits into an int.
               fis.read(bytes);Here you are ignoring the result of the read() method.
          } catch (FileNotFoundException e) {
               e.printStackTrace();
          } catch (IOException e) {
               e.printStackTrace();
          }Here you are essentially ignoring these exceptions and returning a byte array full of nothing to the caller. It would make more sense just to let the exceptions be thrown out of the method and let the caller see and deal with them.
          ByteArrayInputStream bais = new ByteArrayInputStream(bytes);Here you are constructing an InputStream around the bytes you read from a FileInputStream. Why not just read directly from the FileInputStream here?
          DeflaterInputStream dis = new DeflaterInputStream(bais);
          ByteArrayOutputStream baos = new ByteArrayOutputStream();And here you are writing to a ByteArrayOutputStream from which you are going to extract a byte array and write it to a FileOutputStream. Why not write directly to the FileOutputStream?
          try {
               while(dis.available() == 1) {Here you are misusing the available() function. It is never guaranteed to return 1 and it is not intended to tell you when you've reached the end of the input. read() returns a sentinel value for that purpose. Use it. There are few if any correct uses of available() and this is certainly not one of them.
                    baos.write(dis.read());Here you are ignoring the possibility that the return value is the sentinel, so you're writing junk to the file.
          } catch (IOException e) {
               e.printStackTrace();
          }See above re exception handling.
All these remarks also apply to your inflate method.
               FileWriter fw = new FileWriter(new File(output1));
               fw.write(new String(out, "utf-8"));And here you are committing the worst error of all: you are using a Writer to write binary data.
But the bottom line is that you haven't added any value to what the inflater/deflater input and output streams already provide. So throw this all away and use those. All this stuff can be reduced to new InflaterInputStream(new FileInputStream(...)) and new DeflaterOutputStream(new FileOutputStream(...)) and a couple of copy loops, which should look like this:
int count;
byte[] buffer = new byte[8192];
while ((count = in.read(buffer)) > 0)
  out.write(buffer, 0, count);
out.close();
in.close();

Similar Messages

  • Inconsistent compression with Deflater/Inflater

    I try to (un)compress some data using the (In)Deflater class. I want to have GZIP compression, so I set nowrap to true. The javadoc for the Inflater class says something mysterious like "Note: When using the 'nowrap' option it is also necessary to provide an extra "dummy" byte as input. This is required by the ZLIB native library in order to support certain optimizations." What does that mean?
    I have written a test program in which I check if the original data and the uncompressed data is the same. When I run this program I get about 2 errors per run (Inflater not finished).
    Any ideas?
    import java.util.zip.DataFormatException;
    import java.util.zip.Deflater;
    import java.util.zip.Inflater;
    public class ZipTest
        private static byte[] createTestBytes(int size)
            byte[] result = new byte[size];
            for (int i = 0; i < result.length; i++)
                // result[i] = (byte)(i % 256);
                // result[i] = 0;
                result[i] = (byte)(Math.random() * 256);
            return result;
        public static void testGZIP()
            byte[] data = createTestBytes(DATA_SIZE);
            Deflater def = new Deflater(5, true);
            def.reset();
            def.setInput(data);
            def.finish();
            byte[] comp = new byte[MAX_DATA_SIZE_COMP];
            int compBytes = def.deflate(comp);
            byte[] decompData = new byte[DATA_SIZE];
            Inflater inf = new Inflater(true);
            inf.reset();
            inf.setInput(comp, 0, compBytes);
            int decompBytes = 0;
            try
                decompBytes = inf.inflate(decompData);
            catch (DataFormatException exp)
                System.err.println(exp);
            if (inf.finished() == false)
                System.err.println("Inflater not finished "+DATA_SIZE+" "+inf.getRemaining());
            int nDiffs = 0;
            for (int i = 0; i < DATA_SIZE; i++)
                if (data[i] != decompData)
    nDiffs++;
    if (nDiffs > 0)
    System.err.println(nDiffs + " diffs");
    public static int DATA_SIZE = 16080;
    public static int MAX_DATA_SIZE_COMP = (int)(DATA_SIZE * 1.5d);
    public static Deflater def = new Deflater(5, true);
    public static Inflater inf = new Inflater(true);
    public static void main(String[] args)
    for (int i = 0; i < 5000; i++)
    DATA_SIZE = (int)(Math.random()*(1024*64));
    MAX_DATA_SIZE_COMP = (int)(DATA_SIZE * 1.5d)+1024;
    testGZIP();
    System.out.println("Finished.");

    Has nobody an idea why data compressed with Deflater(5,true) cannot always be decompressed by Inflater(true)? What am I doing wrong?
    Thanx
    Ulrich

  • What is WAN compression on AOS 6.4.3.0 and it`s use case?

    Q: What is WAN compression on AOS 6.4.3.0 and it`s use case?
    A: This feature is supported from AOS 6.4.3.0 and above.
    The 7000 Series Controllers contain the Compression/Decompression Engine (CDE) that compresses the raw IP payload data and also decompresses the compressed payload data
    Deflation & Inflation
    The CDE compression process is called Deflation; the decompression process is called Inflation.
    XLP 4xx and XLP2xx Packet Processor Card is a high-performance network processor PCI Express card designed for use in PCI Express compliant systems.
    It features the latest Broadcom XLP 4xx series processor with up to 2.5 Gbps per CDE.
    This processor is ideally suited to both data-plane applications, which are inherently sensitive to memory latencies, and control-plane applications, which will help best-in-class processing performance.
    Advantages
    Four CDE channels on the XLP4XX processor and one CDE channel on the XLP2XX processor.
     2.5 GBps per CDE (Deflation process, Inflation process, or combination of both)
     Deflation context save and restore (at block boundaries)
     Inflation context save and restore (at arbitrary file position)
     Load balancing the input messages to all CDEs
    The Compression/Decompression Engine feature is enabled by default. However, the packets are compressed only if the IP Payload Compression Protocol (IPComp) is successfully negotiated via the Internet Key Exchange (IKE) protocol.
    Use-case
    Data compression reduces the size of data frames that are transmitted over a network link, thereby reducing the time required to transmit the frame across the network. IP payload compression is one of the key features of the WAN bandwidth optimization solution, which is comprised of the following elements:
    You can split a file or data into blocks, and each block can use the mode of compression that suits it best. In this case, it is packet data and there will be only one block.
    IP Payload Compression
    Traffic Management and QoS
    Caching
    Recommendation
    Boc (Branch office controller)can have traffic to destinations other than HQ on the same link, the preferred method is to enable payload compression on the IPsec tunnel between the branch controller and the master controller.
    IP Payload needs to be enabled only between Aruba devices.
    Notes
    When this hardware-based compression feature is enabled, the quality of unencrypted traffic (such as Lync or Voice traffic) is not compromised through increased latency or decreased throughput.

    They didn't do the NVIDIA test on it? At this point I guess it really doesn't matter. It sounds like it needs a new logic board, which has the GPU soldered on it. Price-wise, it's not worth having Apple put a new LB in it, given its age. If you're handy inside a laptop, you might consider installing a used LB yourself if you can find one on eBay, etc. Not good news, I know.

  • Differences on Windows and Linux JVM about java.util.zip package

    Hello, there!
    I need some help if someone else has already face this problem.
    I have a Java server that process the bytes of a SWF File stored in the server and ouptut it to the user through a Servlet. The file was decompressed and re-compressed using the java.util.zip package (Deflater/Inflater).
    Everything works fine on Windows Server 2008 server. But now we need to migrate the server to Linux. The problem is that when I test the website now, the file seens to be corrupted.
    What really intrigues me is that everything runs normal on Windows Server configuration, when changed to Linux, the final file seens to be corrupeted... what could possible be the cause? Is there any difference between java.util.zip package on Window and Linux JVM?
    My Windows Server is:
    . Windows Server 2008 (6.0 - x86)
    . Apache 2.2.11
    . Tomcat 6.0.16.0
    . Java JDK 1.6.0_12-b04
    My CentOS Server is
    . CentOS 5.4 (2.6.18-164.15.1.el5 - i386)
    . Apache 2.2.3
    . Tomcat 6.0.16.0
    . Java JDK 1.6.0_12-b04
    Please, if someone could give me a lead, I would appreciate very much!
    Thank you all in advance,
    CaioToOn!

    ejp wrote:
    Thank you for the answer, but no. The path is correct.That's not what he meant. Zip file/directory entries are supposed to use / as the path separator. It is possible to use \ but such Zip files will only work under Windows. You may have erred here.Ohhh, I had really missunderstood what Ray said. But, I still think that this is not the problem, since the ZIP is a single SWF file generated by Flex SDK 3.4 and compressed in the ZLIB open standard (as in page 13, at [http://www.adobe.com/devnet/swf/pdf/swf_file_format_spec_v9.pdf|http://www.adobe.com/devnet/swf/pdf/swf_file_format_spec_v9.pdf] ). This is how Flash Compiler compress the files.
    jschell wrote:
    If the above suggestions do not solve the problem...Specify in detail with the exact steps used how you determined that it was corrupted.The reason why I believe the SWF is getting corrupted is that when it is loaded by Flash Player (in the client-side) the Player throws a VerifyError: Error # 1033. The [documentation says (see error 1033)|http://help.adobe.com/en_US/AS3LCR/Flash_10.0/runtimeErrors.html] that this error means that the SWF file is corrupted.
    As I said, what intrigues me is that this work perfectly in a Windows Server 2008 server. I just had setup a CentOS server and deployed the application. The client-remains unchanged, so why could the result change?
    raychen wrote:
    I would remove the side effect you are doing and send the file straight through, with decompress and compress, the servlet. It is more likely that you have a bug in your swf processor than the zip library.
    I had already tried it when first coding, in Windows Server 2008, it had not worked.
    Thank you all for the help.
    CaioToOn!

  • How to get extracting progress?

    i extract a file from gzip but when i get the extracting progress, the progress can more then 100%.
    here my code
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import java.util.zip.GZIPInputStream;
    public class CompressFile {
        private final int UNCOMPRESS = 1;
        int status;
        public void uncompress(String Sourcefile){
            try {
                FileInputStream File = new FileInputStream(Sourcefile);
                GZIPInputStream gzip = new GZIPInputStream(File);
                FileOutputStream outFile = new FileOutputStream(Sourcefile.substring(0, Sourcefile.length()-3));
                int MAX_BUFFER_SIZE = 1024;
                byte[] buf = new byte[MAX_BUFFER_SIZE];  //size can be
                int len;
                int Uncompressed = 0;
                int proses = 0;
                int filesize = File.available();
                System.out.println("filesize = "+ filesize);
                status = UNCOMPRESS;
                while (status == UNCOMPRESS) {
                  len = gzip.read(buf);
                  if (len < 1 ) break;
                      Uncompressed += len;
                      outFile.write(buf, 0, len);
                      proses = (int)Math.ceil(((double)Uncompressed/(double)filesize) * 100.0);
                      System.out.println("Progress = "+ proses);
                gzip.close();
                outFile.close();
                File.close();
            } catch (FileNotFoundException ex) {
                     Logger.getLogger(CompressFile.class.getName()).log(Level.SEVERE, null, ex);
            }catch (IOException ex) {
                    Logger.getLogger(CompressFile.class.getName()).log(Level.SEVERE, null, ex);
        public static void main(String args[]){
            CompressFile compress = new CompressFile();
            compress.uncompress("C:/testFolder/a.exe.gz"); // testFile
    }thx..

    Chicon wrote:
    Also, with GZIPInputStream, there's no way to know the initial size of the file before compression. Strictly speaking, there is, if you have a seekable stream. But you can't find it out using GZIPInputStream, I'll give you that.
    To deflate/inflate files, you should use the ZipInputStream, ZipOutputStream, ZipFile and ZipEntry API's GZIP is perfectly fine for that.
    Indeed, the ZipEntry API allows the user to keep information about a file before compression (file length, checksum, file name, ...).GZIP stores the original size and the checksum, too. But it does so after the compressed data (it's a stream-based compression).

  • File inflate and deflate problem

    i have a code that deflates a file "first.txt".But when run it is not printing the proper message written after deflating code.However the deflated file is generated "first.txt.dfl"
    import java.io.*;
    import java.util.zip.*;
    public class DirectDeflector
    public final static String DEFLATE_SUFFIX=".dfl";
    public static void main(String args[])
         Deflater def=new Deflater();
         byte[] input=new byte[1024];
         byte[] output=new byte[1024];
         try
         FileInputStream fin=new FileInputStream("first.txt");
         FileOutputStream fout=new FileOutputStream("first.txt"+DEFLATE_SUFFIX);
         while(true)
              int numRead=fin.read(input);
              if(numRead==-1)
    def.finish();
    while(!def.finished())
         int numCompressedBytes=def.deflate(output,0,output.length);
         if(numCompressedBytes>0)
              fout.write(output,0,numRead);
    break;
              else
                   def.setInput(input,0,numRead);
                   while(!def.needsInput())
                        int numCompressedBytes=def.deflate(output,0,output.length);
                        if(numCompressedBytes>0)
                             fout.write(output,0,numCompressedBytes);
         fin.close();
         fout.flush();
         fout.close();
         def.reset();
         System.out.println("File deflated!!");
    }catch(Exception e )
         System.out.println(e.getMessage());
    **But instead of printing "File deflated!!",it is printing null.
    Again when i inflate the and write it to another file it is not working..Here is the code..
    import java.io.*;
    import java.util.zip.*;
    public class DirectInflator
    public static void main(String args[])
         Inflater inf=new Inflater();
         byte[] input=new byte[1024];
         byte[] output=new byte[1024];
         try
              FileInputStream fin=new FileInputStream("first.txt.dfl");
              FileOutputStream fout=new FileOutputStream("firstread.txt");          
              while(true)
                   int numRead=fin.read(input);
                   if(numRead!=-1)
                        inf.setInput(input,0,numRead);                    
                   int numDecompressed=0;
                   while((numDecompressed=inf.inflate(output,0,output.length))!=0)
                   fout.write(output,0,numDecompressed);     
                   if(inf.finished())
                        break;
                   else if(inf.needsDictionary())
                        System.err.println("Dictionary required!!");
                        break;
                   else if(inf.needsInput())
                        continue;
              fin.close();
              fout.flush();
              fout.close();
              inf.reset();
              System.out.print("Inflation successful!!");
         }catch(DataFormatException e)
              e.getMessage();
         catch(IOException e)
              e.getMessage();
         catch(Exception e)
              e.getMessage();
    **plz help

                int numRead=fin.read(input);
                if(numRead==-1)
    def.finish();Seems to be an 'else' missing here.
          int numCompressedBytes=def.deflate(output,0,output.length);int numCompressedBytes=def.deflate(output,0,numRead);
          if(numCompressedBytes>0)
               fout.write(output,0,numRead);
    fout.write(output,0,numCompressedBytes);
                elseWhat is supposed to be happening in the following block I have no idea.

  • 3d sphere inflating and deflating

    I am trying to build a simple VI which when given a duration, will display a sphere increasing in size/scale and then decreasing in size/scale for the same duration.
    Imagine this to be like a baloon that is inflating and deflating continuously. The inflatrion shuld start from 0.5 scale to 1 and then it should deflate in equal number of steps from1 down to 0.5
    I have tried to implement this  using simple logic but the 3d display doesnt work correctly . Not sure if my logic is right.
    Also, I have only managed to do a 0 to 1 scale increase instead of a 0.5 to 1
    Code attached.
    Attachments:
    3d display.vi ‏18 KB

    Hi Jaspal,
    I attached a version of your VI in 8.6 that does what I think you'd like.
    There were 2 problems with your VI that I saw, the first was you wired "N" from the loop instead of "i".  So you were always comparing 20>9 and never getting to your other case.
    The other is it looks like you were splitting the 3D Picture Control reference and expecting it to treat all of your 3D Picture Controls independently.  With a reference any rotations/scales/translations will apply to the scene on that reference and all controls wired to it.
    Attachments:
    3d display.vi ‏14 KB

  • Using Deflater and Inflater with JDK 1.1

    Does anyone have an example using the classes Deflater and Inflater to decompress and compress data?
    I get an "EOFException" when using the following code:
    // compression
    public Packet zipData(Packet packet)
    // Create a new Deflater
    Deflater def = new Deflater();
    byte[] buf = new byte[packet.getData().length];
    int offset = 0;
    def.setInput(packet.getData(), offset, packet.getData().length);
    def.finish();
    offset = def.deflate(buf);
    byte[] real_buf = new byte[offset];
    System.arraycopy(buf, 0, real_buf, 0, offset);
    packet.setData(real_buf);
    return packet;
    Any help will be greatly appreciated.
    Thanks.

    take a look at this techtip, may be it will be of some help to you
    http://developer.java.sun.com/developer/TechTips/1998/tt0421.html#tip2

  • Deflater and Inflater again!

    Hi All
    This is in continuation of the thread
    http://forum.java.sun.com/thread.jsp?thread=389668&forum=4&message=1683180
    I am working on a similar problem. We need compress and decompress some messages which are Java 'String's. I have explored this and many other solutions to implement this in Java using the zip package but nothing has worked so far.
    My specific question here is related to the code snippet given below. It doesnt work with String str but works fine with String str2. Can someone please explain to me why?
    Here's the exception trace that I get when I use String str
    /*****************************EXCEPTION TRACE BEGINS*****************************************/
    oversubscribed dynamic bit lengths tree
    java.util.zip.DataFormatException: oversubscribed dynamic bit lengths tree
    at java.util.zip.Inflater.inflateBytes(Native Method)
    at java.util.zip.Inflater.inflate(Unknown Source)
    at EncodeDecodeExample.<init>(EncodeDecodeExample.java:52)
    at EncodeDecodeExample.main(EncodeDecodeExample.java:75)
    /*****************************EXCEPTION TRACE ENDS*****************************************/
    Please note that, the exception message changes to "java.util.zip.DataFormatException: incorrect data check" if String str3 is used instead of String str in the line marked "/***********change string here***********/".
    Here's the code snippet
    /*****************************CODE BEGINS*****************************************************/
    import java.util.zip.*;
    import java.io.*;
    import java.lang.reflect.Array;
    public class EncodeDecodeExample
         public EncodeDecodeExample()
              try{
                   // encoding the string into byte
                   String str = "Please encode this string neelesh abcdefghp ijklmnop";
                   String str2 = "Please encode this string neelesh";
    String str3 = "Please encode this string neelesh Please encode this string neelesh Please encode this string neelesh abcdefghp ijklmnop";
                   byte[] bt = str.getBytes(); /***********change string here***********/
                   // compressing the bytes
              int initialSize = 10;
                   byte[] output;
                   Deflater deflater;
                   int datalen;
                   do{
                        output = new byte[initialSize];
                        deflater = new Deflater();
                        deflater.setInput(bt);
                   deflater.finish();
                        datalen = deflater.deflate(output);
                        if(datalen == initialSize){
                             initialSize *= 2;
                        else{
                             break;
                   }while(true);
                   String tempString = new String(output, 0 , datalen);
                   byte[] tempBt = tempString.getBytes();
                   // decompressing the bytes
                   Inflater inflater = new Inflater();
                   inflater.setInput(tempBt, 0, Array.getLength(tempBt));
                   byte[] result = new byte[4000];
                   int reslen = 0;
                   try{
                        reslen = inflater.inflate(result, 0, 80);
                   catch(DataFormatException e)
                        System.out.println(e.getMessage() );
                        e.printStackTrace();
                   inflater.end();
                   // decoding bytes to a String
                   String outputString = new String(result, 0, reslen);
                   System.out.println(outputString);
              }catch(Exception e){
                   System.out.println(e.getMessage() );
                   e.printStackTrace();
         public static void main(String args[])
              new EncodeDecodeExample();
    /*****************************CODE ENDS*****************************************************/

    HI All
    Extremely sorry for the goof up, please refer to this piece of code intead of the one in the earlier posting, thanks
    Neelesh
    /*****************************CODE BEGINS*****************************************************/
    import java.util.zip.*;
    import java.io.*;
    import java.lang.reflect.Array;
    public class EncodeDecodeExample
         public EncodeDecodeExample()
              try{
                   // encoding the string into byte
                   String str = "Please encode this string neelesh abcdefghp ijklmnop";
                   String str2 = "Please encode this string neelesh";
                   String str3 = "Please encode this string neelesh Please encode this string neelesh Please encode this string neelesh abcdefghp ijklmnop";
                   byte[] bt = str.getBytes(); /***********change string here***********/
                   // compressing the bytes
                   int initialSize = 10;
                   byte[] output;
                   Deflater deflater;
                   int datalen;
                   do{
                        output = new byte[initialSize];
                        deflater = new Deflater();
                        deflater.setInput(bt);
                        deflater.finish();
                        datalen = deflater.deflate(output);
                        if(datalen == initialSize){
                             initialSize *= 2;
                        else{
                             break;
                   }while(true);
                   String tempString = new String(output, 0 , datalen);
                   byte[] tempBt = tempString.getBytes();
                   // decompressing the bytes
                   Inflater inflater = new Inflater();
                   inflater.setInput(tempBt, 0, Array.getLength(tempBt));
                   byte[] result = new byte[4000];
                   int reslen = 0;
                   try{
                        reslen = inflater.inflate(result);
                   catch(DataFormatException e)
                        System.out.println(e.getMessage() );
                        e.printStackTrace();
                   inflater.end();
                   // decoding bytes to a String
                   String outputString = new String(result, 0, reslen);
                   System.out.println(outputString);
              }catch(Exception e){
                   System.out.println(e.getMessage() );
                   e.printStackTrace();
         public static void main(String args[])
              new EncodeDecodeExample();
    /*****************************CODE ENDS*****************************************************/

  • Deflator and Inflator problem

    I use Deflator and Inflator to compress and decompress String for minimizing the size of the data to be sent in a network system.
    I wrote a class (DataCompressor.java) to do it.
    When i try to use it to compress and decompress within a single program (TestDataCompressor.java), it's all right.
    But when i try to compress a String in (DCClient.java), and sent the compressed String to (DCServer.java), there are exception in decompressing the compressed String
    My paltform is Windows2000 and JDK1.31
    Anyone know what is the problem i got?
    DataCompressor.java
    import java.util.zip.Deflater;
    import java.util.zip.Inflater;
    import java.util.zip.DataFormatException;
    import java.io.UnsupportedEncodingException;
    public class DataCompressor
    private Deflater DF = new Deflater(Deflater.HUFFMAN_ONLY,true);
    private Inflater IF = new Inflater(true);
    private int BUFFER_SIZE = 2048;
    private byte[] compressbytes;
    private byte[] decompressbytes;
    private String encoding = "UTF-16LE";
    public DataCompressor()
    compressbytes = new byte[BUFFER_SIZE];
    decompressbytes = new byte[BUFFER_SIZE];
    public DataCompressor(int bsize)
    BUFFER_SIZE = bsize;
    compressbytes = new byte[BUFFER_SIZE];
    decompressbytes = new byte[BUFFER_SIZE];
    synchronized public String compress(String inputString)
    int len;
    DF.reset();
    try
    DF.setInput(inputString.getBytes(encoding));
    DF.finish();
    len = DF.deflate(compressbytes);
    return new String(compressbytes,0,len,encoding);
    catch(UnsupportedEncodingException UEE)
    UEE.printStackTrace();
    return "";
    synchronized public String decompress(String inputString)
    int len;
    IF.reset();
    try
    IF.setInput(inputString.getBytes(encoding));
    len = IF.inflate(decompressbytes);
    return new String(decompressbytes,0,len,encoding);
    catch(UnsupportedEncodingException UEE)
    UEE.printStackTrace();
    return "";
    catch(DataFormatException DFE)
    DFE.printStackTrace();
    return "";
    synchronized void clear()
    DF.end();
    IF.end();
    DF = null;
    IF = null;
    compressbytes = null;
    decompressbytes = null;
    TestDataCompressor.java
    import java.io.*;
    public class TestDataCompressor
    public static void main(String[] args)
    String Data1 = "this is a test for data compression";
    String EnData1 = "";
    String DeData1 = "";
    DataCompressor DC = new DataCompressor();
    EnData1 = DC.compress(Data1);
    System.out.println("Out 1 :\n"+EnData1);
    DeData1 = DC.decompress(EnData1);
    System.out.println("Decom 1 :\n"+DeData1);
    DC.clear();
    DC = null;
    DCClient.java
    import java.io.*;
    import java.net.*;
    public class DCClient
    public static void main(String[] args) throws IOException
    Socket S = new Socket("127.0.0.1",9090);
    PrintWriter PW = new PrintWriter(S.getOutputStream(),true);
    String Data = "this is a test for data compression";
    String EnData = "";
    DataCompressor DC = new DataCompressor();
    EnData = DC.compress(Data);
    PW.println(EnData);
    System.out.println(EnData);
    System.out.println(EnData.length()+" chars sent");
    try
    byte[] D = EnData.getBytes("UTF-16LE");
    for(int i=0;i<D.length;i++)
    System.out.println(D+" : "+(char)D);
    catch(UnsupportedEncodingException UEE)
    UEE.printStackTrace();
    S.close();
    S = null;
    System.exit(0);
    DCServer.java
    import java.io.*;
    import java.net.*;
    public class DCServer
    public static void main(String[] args) throws IOException
    ServerSocket SS = new ServerSocket(9090);
    Socket S;
    BufferedReader BR;
    String message = "";
    DataCompressor DC = new DataCompressor();
    while(true)
    S = SS.accept();
    BR = new BufferedReader(new InputStreamReader(S.getInputStream()));
    while((message = BR.readLine())!=null)
    System.out.println(message);
    System.out.println(message.length() +" chars received.");
    try
    byte[] D = message.getBytes("UTF-16LE");
    for(int i=0;i<D.length;i++)
    System.out.println(D+" : "+(char)D);
    catch(UnsupportedEncodingException UEE)
    UEE.printStackTrace();
    System.out.println("Decompressed : "+DC.decompress(message));
    }

    This is a guess from a puurely cursory glance at your code. I thought I saw you using readline() to get compressed data off the wire. That is probably the wrong thing to do, as the data is not character data.

  • Inflater/Deflater Question

    Hi
    I was lookign into the java.util.zip package utilities - Inflator and Deflator apis and I am bit confused abt their usage - Lets take the scenario where I want to compress a byte[] of data - Ideally I would expect an API to like
    public byte[] compress(byte[] dataToCompress)
    Strangely enough the Deflator api does not have this - instead it has - setInput(byte[] dataToCompress) and then int deflate(byte[] buffer) - How on the world I will know the buffer size if I dont know what the compressed data size will be?? .
    If anyone has really used Deflater - pls let me know how they have got around this problem. OR If there is any other better way to do that pls let me know.
    TIA
    Anamitra

    Disclaimer: I have never used the Deflator class and this is meant to be a hand-waving example, it is not meant to be 'good' code. By no means should you use this code for anything important. You need to understand the class before you actually use it. I am just showing you the basic idea. From looking at the API there is clearly much more than this needed to use the class correctly. I haven't even tried to compile this.
    Do something to the effect of:
    ArrayList bufferList = new ArrayList;
    byte[] input = getInput();
    Deflator deflator = new Deflator();
    int bytesReturned;
    deflator.setInput(input);
    do {
       byte[] buffer = new byte[1000]
       bytesReturned = deflator.deflate(buffer);
       bufferList.add(buffer);
    } while (bytesReturned == 1000)

  • Java Inflater problem decompressing Git packfile objects

    I have a Git packfile that I have saved as a file from the git-upload-pack
    command. I want to read through the packfile, decompressing each of the
    objects. Each object is compressed in the packfile using zlib compression.
    My little inflater procedure works fine for a tiny HelloWorld project. So, I
    decided to mix it up a little and use the jEdit source for a larger test. I
    am 99% certain the jEdit.git packfile itself is ok as I have passed it
    through directly to eGit's Import using an SSH proxy and eGit unpacked it
    just fine.
    So, my inflater method decompresses the first 7 objects fine (a commit, a
    couple of trees, and several blobs) and a cursory visual inspection of the
    decompressed data seems fine. The eighth object becomes a problem, though.
    It is a blob with the name build.xml that is 51,060 bytes decompressed
    (looking at the original pre-git-pushed jEdit source). The actual file size
    matches the decompressed data content size in the packfile object header.
    The inflater procedure outputs the decompressed data to System.out for
    visual inspection. Approximately the first 1/3 looks like the original
    build.xml but after that, the output is garbled. The procedure continues
    decompressing objects after the 8th, but garbled, object but it dies on the
    9th object with an "unknown compression method" error.
    So I created a new test inflater to focus only on decompressing the 8th
    object. Simply opening the packfile, copying out the compressed data (7793
    bytes), and inflating it yields the same 2/3 garbled xml.
    As a further test, I then take the original build.xml file, compress it
    using java's Deflater (yielding a 7793 byte array), and then inflate it
    using my same procedure and it decompresses fine. All of the xml is
    readable.
    Now, I have tried several variations of an inflater procedure, including an
    patchwork variation from jGit's WindowCursor.inflate method. But they all
    yield the same garbled result for the compressed build.xml data.
    Ideas?

    oops. sorry!
              try {
                   byte[] packFile = readFile("/Users/marcos/GitProxyCache/jedit.pack");
                   //THE BELOW OBJECT DECOMPRESSES FINE
              //Object starts at index 8616
              //Type = 3, Decompressed size = 2248 (uses 2 extra size bytes)
                   //byte [] packDataWindow = new byte[8000];
                   //System.arraycopy(packFile, 8619, packDataWindow, 0, packDataWindow.length); //works
                   //THE BELOW OBJECT FAILS TO INFLATE
                   //IT CAUSES an "incorrect data check" error
              //Object starts at index 9470
              //Type = 3, Decompressed size = 51060 (uses 2 extra size bytes)
                   byte [] packDataWindow = new byte[8000];
                   System.arraycopy(packFile, 9473, packDataWindow, 0, packDataWindow.length); //does not work
                   Inflater decompresser = new Inflater();
                   decompresser.setInput(packDataWindow, 0, packDataWindow.length);
                   byte[] result = new byte[60000];
                   int resultLength = 0;
                   resultLength = decompresser.inflate(result);
                   String outputString = new String(result, 0, resultLength, "UTF-8");
                   System.out.println(outputString);
                   decompresser.end();
              } catch (Exception e) {
                   e.printStackTrace();
              }

  • Is there a way to compress using Java deflator & uncompress using UTL ?

    hi,
    I was wondering if there is a way to compress using Java deflator on the java side & then uncompress in the stored procedure. UTL_COMPRESS.LZ_UNCOMPRESS(BLOB) ?
    I tried that, but I'm currently getting Invalid data exceptions..
    The Other option is to use Java Inflator in the Java stored procedure. But I want to avoid java stored procedures.
    Thanks in advance.
    /// java side
    String inputString = loadXML (inputfile);
    byte[] input = inputString.getBytes("UTF-8");
    byte[] output = new byte[inputString.length()];
    Deflater compresser = new Deflater(Deflater.BEST_SPEED, true);
    compresser.setInput(input);
    OracleCallableStatement insertALLStatement = (OracleCallableStatement) con.prepareCall(insertALLSQL);
    InputStream stream = new ByteArrayInputStream(data);
    insertALLStatement.setBinaryStream(1, stream, (int)data.length);
    insertALLStatement.execute();
    // pl sql
    create or replace PROCEDURE INSERTBYTES
    ( compressed IN BLOB
    ) AS
    uncompressed:=UTL_COMPRESS.lz_uncompress (compressed);
    ...

    That depends.
    Does Java Deflator use the same compression technique as UTL_COMPRESS.LZ_UNCOMPRESS? i.e. is it using Lempel Ziff compression algorithms. If so, then, yes, there's a possibility it could work, however it also depends if the Java Deflator stores header information about the compressed data differently to how LZ_UNCOMPRESS expects it, or even if header information is included or it's just raw compressed data.
    It sounds a bit like compressing a file with Yoshi compression and then trying to use PKZIP to uncompress it methinks, in which case you're going to be out of luck. You have to ensure the compression algorithms and file formats are compatible.

  • Deflation Animation not Working

    Hi,
    I have got a code from this forum which create a ellipse on the client area. First it creates a rectangle equivalent to the size of client area. I have put the deflation code in the ::OnTimer( ....) but its not working.
    void CTestingCode1View::OnDraw(CDC* pDC)
    CTestingCode1Doc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
    if (!pDoc)
    return;
    GetClientRect(&rect);
    //CRect rc(rect.TopLeft().x+10, rect.TopLeft().y+10,rect.BottomRight().x-10,rect.BottomRight().y-10);
    //CDC *pDC = GetDC();
    CDC WorkDC;
    CBitmap memBmp, * pOldMemBmp;
    WorkDC.CreateCompatibleDC(pDC);
    memBmp.CreateCompatibleBitmap(pDC, rect.Width(), rect.Height());
    pOldMemBmp = (CBitmap *)WorkDC.SelectObject(&memBmp);
    int oldBkMode = WorkDC.SetBkMode(TRANSPARENT);
    WorkDC.Ellipse(rect);
    WorkDC.SetBkMode(oldBkMode);
    // WorkDC.BitBlt(0, 0, rect.Width(), rect.Height(), pDC, 0, 0, SRCCOPY);
    //pDC->BitBlt(0, 0, rect.Width(), rect.Height(), &WorkDC, 0, 0, SRCCOPY);
    pDC->BitBlt(rect.left, rect.top, rect.Width(), rect.Height(), &WorkDC, 0, 0, SRCCOPY);
    WorkDC.SelectObject(pOldMemBmp);
    pOldMemBmp->DeleteObject();
    // TODO: add draw code for native data here
    }void CTestingCode1View::OnTimer(UINT_PTR nIDEvent)
    // TODO: Add your message handler code here and/or call default
    rect.DeflateRect(10,10);
    CView::OnTimer(nIDEvent);
    int CTestingCode1View::OnCreate(LPCREATESTRUCT lpCreateStruct)
    if (CView::OnCreate(lpCreateStruct) == -1)
    return -1;
    SetTimer(1,200,NULL);
    //#3016
    //@R0
    //return -1;
    //#3016
    //@R0
    //#3016
    // @R0
    // TODO:  Add your specialized creation code here
    //@R0
    return 0;
    CReact is declared as a class variable in *view.h. Some body please guide me.
    Zulfi.

    Hi my friend,
    I am able to calculate the values for initial line like ellipse at the start of inflation process by trial and error process. The values are part of OnDraw(...) method. There is a little bit flicker when the inflation ellipse is maximized.
    #include "stdafx.h"
    // SHARED_HANDLERS can be defined in an ATL project implementing preview, thumbnail
    // and search filter handlers and allows sharing of document code with that project.
    #ifndef SHARED_HANDLERS
    #include "DefInflate2.h"
    #endif
    #include "DefInflate2Doc.h"
    #include "DefInflate2View.h"
    #ifdef _DEBUG
    #define new DEBUG_NEW
    #endif
    // CDefInflate2View
    IMPLEMENT_DYNCREATE(CDefInflate2View, CView)
    BEGIN_MESSAGE_MAP(CDefInflate2View, CView)
    // Standard printing commands
    ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint)
    ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint)
    ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CDefInflate2View::OnFilePrintPreview)
    ON_WM_CONTEXTMENU()
    ON_WM_RBUTTONUP()
    ON_WM_CREATE()
    ON_WM_SIZE()
    ON_WM_TIMER()
    ON_WM_ERASEBKGND()
    END_MESSAGE_MAP()
    // CDefInflate2View construction/destruction
    CDefInflate2View::CDefInflate2View()
    // TODO: add construction code here
    CDefInflate2View::~CDefInflate2View()
    BOOL CDefInflate2View::PreCreateWindow(CREATESTRUCT& cs)
    // TODO: Modify the Window class or styles here by modifying
    // the CREATESTRUCT cs
    return CView::PreCreateWindow(cs);
    // CDefInflate2View drawing
    void CDefInflate2View::OnDraw(CDC* pDC)
    CDefInflate2Doc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
    if (!pDoc)
    return;
    CRect cr;
    int cx, cy;
    GetClientRect(&cr);
    CDC WorkDC;
    WorkDC.CreateCompatibleDC(pDC);
    CBitmap memBmp;
    memBmp.CreateCompatibleBitmap(pDC, cr.Width(), cr.Height());
    CBitmap *pOldMemBmp = (CBitmap *)WorkDC.SelectObject(&memBmp);
    CBrush background(0xFF0000);
    WorkDC.FillRect(&cr, &background);
    if (m_running)
    WorkDC.Ellipse(m_rect);
    else {
    m_running = true;
    SetTimer(1234, 50, 0);
    GetClientRect(&cr);
    if(deflation) {
    m_rect.left = 0;
    m_rect.top = 0;
    m_rect.right = cr.right;
    m_rect.bottom = cr.bottom;
    else {
    m_rect.left = (cr.left+cr.right)/2 -150;
    m_rect.top = (cr.top+ cr.bottom)/2;
    m_rect.right = (cr.left+cr.right)/2 +150;
    m_rect.bottom = (cr.top+ cr.bottom)/2;
    //Special Assigned values
    m_rect.left = 200;//cr.left;//+cr.right-20;
    m_rect.top = (cr.top+ cr.bottom)/2;//cr.bottom/2;
    m_rect.right = 100;//0;//cr.left+cr.right+20;
    m_rect.bottom = (cr.top+ cr.bottom)/2;//0;//cr.bottom;*/
    WorkDC.Ellipse(m_rect);
    pDC->BitBlt(0, 0, cr.Width(), cr.Height(), &WorkDC, 0, 0, SRCCOPY);
    WorkDC.SelectObject(pOldMemBmp);
    // TODO: add draw code for native data here
    // CDefInflate2View printing
    void CDefInflate2View::OnFilePrintPreview()
    #ifndef SHARED_HANDLERS
    AFXPrintPreview(this);
    #endif
    BOOL CDefInflate2View::OnPreparePrinting(CPrintInfo* pInfo)
    // default preparation
    return DoPreparePrinting(pInfo);
    void CDefInflate2View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
    // TODO: add extra initialization before printing
    void CDefInflate2View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
    // TODO: add cleanup after printing
    void CDefInflate2View::OnRButtonUp(UINT /* nFlags */, CPoint point)
    ClientToScreen(&point);
    OnContextMenu(this, point);
    void CDefInflate2View::OnContextMenu(CWnd* /* pWnd */, CPoint point)
    #ifndef SHARED_HANDLERS
    theApp.GetContextMenuManager()->ShowPopupMenu(IDR_POPUP_EDIT, point.x, point.y, this, TRUE);
    #endif
    // CDefInflate2View diagnostics
    #ifdef _DEBUG
    void CDefInflate2View::AssertValid() const
    CView::AssertValid();
    void CDefInflate2View::Dump(CDumpContext& dc) const
    CView::Dump(dc);
    CDefInflate2Doc* CDefInflate2View::GetDocument() const // non-debug version is inline
    ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDefInflate2Doc)));
    return (CDefInflate2Doc*)m_pDocument;
    #endif //_DEBUG
    // CDefInflate2View message handlers
    int CDefInflate2View::OnCreate(LPCREATESTRUCT lpCreateStruct)
    if (CView::OnCreate(lpCreateStruct) == -1)
    return -1;
    m_running= false;
    /*#3016
    @R0
    return -1;
    #3016
    @R0
    #3016
    @R0
    // TODO: Add your specialized creation code here
    @R0*/
    return 0;
    void CDefInflate2View::OnSize(UINT nType, int cx, int cy)
    CView::OnSize(nType, cx, cy);
    m_rect.left = 0;
    m_rect.top = 0;
    m_rect.right = cx;
    m_rect.bottom = cy;
    if (!m_running)
    SetTimer(1234, 50, 0);
    m_running = true;
    deflation=1;
    start = true;
    // TODO: Add your message handler code here
    void CDefInflate2View::OnTimer(UINT_PTR nIDEvent)
    // TODO: Add your message handler code here and/or call default
    CRect cr;
    int width= m_rect.Width();
    int ht = m_rect.Height();
    if (deflation) {
    if (width<=0 || ht<=0){
    deflation=0;
    KillTimer(1234 );
    m_running=false;
    Invalidate();
    else {
    m_rect.DeflateRect(1,1,1,1);
    Invalidate();
    else {
    GetClientRect(&cr);
    width= cr.Width();
    ht = cr.Height();
    if (m_rect.Width() >= width || m_rect.Height() >= ht ) {
    deflation=1;
    KillTimer(1234 );
    m_running=false;
    Invalidate();
    else {
    m_rect.InflateRect(1,1,1,1);
    Invalidate();
    CView::OnTimer(nIDEvent);
    BOOL CDefInflate2View::OnEraseBkgnd(CDC* pDC)
    // TODO: Add your message handler code here and/or call default
    return true;
    return CView::OnEraseBkgnd(pDC);
    Many thanks for your guidance.
    Zulfi.

  • [Android]why can't "text reflow" be fixed instead of using the unreliable broken "text inflation"?

    text inflation as a desktop-to-mobile-view conversion doesn't work, and can never be fixed.
    it will always either fail to make important parts of websites readable, or completely unhinge them.
    opera mini, opera mobile(now classic) and opera for android show how well "text reflow" works for desktop pages on mobile devices.
    before the opera mobile EOL I never needed mobile versions of pages.
    firefox for android already has a text reflow feature, but it is hidden away and unusable because of 2 easily fixable problems.
    1. "text inflation", it's completely unnecessary to require enlarged text in order to reflow but with this feature disabled(text to smallest size) there is no reflow.
    2. "fixed zoom", currently double-tap zooming will always put the text fullscreen(which on a desktop layout is still too small)
    a "fixed zoom" option that always zooms a set % regardless of where you double-tap should allow zooming in to a level where text is readable and should force "text reflow" to reflow text to screen-width.
    so why are we still stuck with either messy or unreadable text on desktop pages?

    you seem to have missed the point of my post.
    firefox has 2 methods to make desktop pages readable on phones, "text reflow" and "text inflation".
    text reflow is disabled by default and doesn't work properly because of a few unchangeable settings, even though it is proven to be a very effective and reliable method to make desktop pages work on phones by opera's browsers.
    and text inflation is broken because it can never accurately determine what can and can't be inflated.
    and my question is why we're still stuck with using text inflation when text reflow is a much better option?

Maybe you are looking for