Problem in String Decompressor

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • yaha1982
    New Member
    • Jul 2007
    • 5

    Problem in String Decompressor

    Good Morning Friends
    I have a compress data with type byte[]. Whenever i tried to write compress data into buffer it goes into infinite loop in some case. For example in 1000 file 2 or 3 files goes into infinite loop. That means those byte[] can not reach at its end of file.

    MY Java Code is :


    public static byte[] deCompressDoc(b yte[] compressedData) {
    //Create the decompressor and give it the data to compress
    Inflater decompressor = new Inflater(true);
    decompressor.se tInput(compress edData);

    // Create an expandable byte array to hold the decompressed data
    ByteArrayOutput Stream byteInputStream = new
    ByteArrayOutput Stream(compress edData.length);

    // Decompress the data
    byte[] buf = new byte[1024];
    while (!decompressor. finished()) {
    try {
    int count = decompressor.in flate(buf);
    byteInputStream .write(buf, 0, count);
    } catch (DataFormatExce ption e) {
    throw new SDFException(" Error on Decompression of a
    Document ", e);
    }
    }
    try {
    byteInputStream .close();
    } catch (IOException e) {
    throw new SDFException("E rror on Decompression of a
    Document ", e);
    }

    // Get the decompressed data
    byte[] decompressedDat a = byteInputStream .toByteArray();

    return decompressedDat a;
    }



    The real problem is in some cases my while loop goes into infinite loop eventhoegh my byte[] is perfect.

    Please give me a positive solution as early as possible.

    Thanks
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #2
    Originally posted by yaha1982
    Good Morning Friends
    I have a compress data with type byte[]. Whenever i tried to write compress data into buffer it goes into infinite loop in some case. For example in 1000 file 2 or 3 files goes into infinite loop. That means those byte[] can not reach at its end of file.

    MY Java Code is :

    [CODE=java]
    public static byte[] deCompressDoc(b yte[] compressedData) {
    //Create the decompressor and give it the data to compress
    Inflater decompressor = new Inflater(true);
    decompressor.se tInput(compress edData);

    // Create an expandable byte array to hold the decompressed data
    ByteArrayOutput Stream byteInputStream = new
    ByteArrayOutput Stream(compress edData.length);

    // Decompress the data
    byte[] buf = new byte[1024];
    while (!decompressor. finished()) {
    try {
    int count = decompressor.in flate(buf);
    byteInputStream .write(buf, 0, count);
    } catch (DataFormatExce ption e) {
    throw new SDFException(" Error on Decompression of a
    Document ", e);
    }
    }
    try {
    byteInputStream .close();
    } catch (IOException e) {
    throw new SDFException("E rror on Decompression of a
    Document ", e);
    }

    // Get the decompressed data
    byte[] decompressedDat a = byteInputStream .toByteArray();

    return decompressedDat a;
    }
    [/CODE]


    The real problem is in some cases my while loop goes into infinite loop eventhoegh my byte[] is perfect.

    Please give me a positive solution as early as possible.

    Thanks
    What does your decompressor.fi nished() method look like and where does it get it's information from? That could be, where it's going wrong.

    Greetings,
    Nepomuk

    PS.: Please use CODE tags to post code.

    Comment

    • yaha1982
      New Member
      • Jul 2007
      • 5

      #3
      Originally posted by nepomuk
      What does your decompressor.fi nished() method look like and where does it get it's information from? That could be, where it's going wrong.

      Greetings,
      Nepomuk

      PS.: Please use CODE tags to post code.
      decompressor.fi nished() is mothod of java.util.zip.I nflater class and my while loop continues upto end of character of byte[].

      Comment

      • Nepomuk
        Recognized Expert Specialist
        • Aug 2007
        • 3111

        #4
        I don't know, why it gets stuck, but you can have a control, which ends the decompression after a certain time, if it hasn't finished. Of course, if a file is very big and takes a long time, you might not be able to unpack it.

        If you want such a limitation, you can do it like this:
        [CODE=java]
        long time = System.currentT imeMillis();
        while(!decompre ssor.finished() && System.currentT imeMillis() - time < 600000) // this will give the process 10 Minutes to finish
        {
        // ...
        }
        [/CODE]
        Can't think of anything else now, but maybe someone else can?

        Greetings,
        Nepomuk

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Why don't you simply use an InflaterInputSt ream which reads from a ByteArrayInputS tream?
          Of course you can write it to a ByteArrayOutput Stream again and retrieve the
          byte array when all has been read, inflated and written again.

          kind regards,

          Jos

          Comment

          • yaha1982
            New Member
            • Jul 2007
            • 5

            #6
            Originally posted by nepomuk
            I don't know, why it gets stuck, but you can have a control, which ends the decompression after a certain time, if it hasn't finished. Of course, if a file is very big and takes a long time, you might not be able to unpack it.

            If you want such a limitation, you can do it like this:
            [CODE=java]
            long time = System.currentT imeMillis();
            while(!decompre ssor.finished() && System.currentT imeMillis() - time < 600000) // this will give the process 10 Minutes to finish
            {
            // ...
            }
            [/CODE]
            Can't think of anything else now, but maybe someone else can?

            Greetings,
            Nepomuk

            Thanks for replying me , now it is working properly for small files but not for big one
            Thanks again

            Comment

            • Nepomuk
              Recognized Expert Specialist
              • Aug 2007
              • 3111

              #7
              Originally posted by yaha1982
              Thanks for replying me , now it is working properly for small files but not for big one
              Thanks again
              What goes wrong? Does it give you an error? Does it just not complete the job, because it stops after 10 Minutes? Or is it something totally different?

              Greetings,
              Nepomuk

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                *cough* reply #5.

                kind regards,

                Jos

                Comment

                Working...