Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6368025

GZIPOutputStream.finish() does not work

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • 5.0
    • core-libs

      FULL PRODUCT VERSION :
      java version "1.5.0_05"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_05-b05)
      Java HotSpot(TM) Client VM (build 1.5.0_05-b05, mixed mode)


      ADDITIONAL OS VERSION INFORMATION :
      Win XP Pro w/SP2

      A DESCRIPTION OF THE PROBLEM :
      GZIPOutputStream.finish() apparently closes the file; documentation specifically says it does not close the file. File cannot be written to after execution of finish().

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      access a large file, more than one megabyte in length. http://setiathome.berkeley.edu/stats/user.gz (uncompressed) will do.
      execute Split x:/dir/fileName.xml x:/dir
        Program will crash saying Error writing X:/dir/fileName1.gz
      Reason: write beyond end of stream


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
        Program Split should run to end.
      ACTUAL -
        Program crashed, saying Error writing O:/Temp/user1.gz
      Reason: write beyond end of stream

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      Error writing O:/Temp/user1.gz
      Reason: write beyond end of stream

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.io.*;
      import java.util.zip.GZIPOutputStream;

      /**
       *
       */

      /**
       * @author Administrator
       *
       */
      public class Split {
      static final int bufSize = (int)Math.pow(2, 20);
      static byte[] buffer = new byte[bufSize];
      /**
      * @param args
      */
      public static void main(String[] args) {
      int suffix = 1, bytesRead = 0;
      long totalBytesRead = 0, totalLen = 0;
      String baseInputFileName, inputFileNameExtension, inputFileName, inputFileNamePath;
      File inFile;
      FileInputStream fileIn;
      BufferedInputStream bufIn = null;
      FileOutputStream fileOut;
      GZIPOutputStream gzipOut = null;


      // TODO Auto-generated method stub
      if( args.length < 2 ){
      System.out.print("Usage: fileName OutputFilePath");
      System.exit(4);
      }
      inFile = new File(args[0]);
      if( !inFile.exists() ){
      System.out.println("Cannot find " + args[0]);
      System.exit(4);
      }
      inputFileName = inFile.getName();
      int point = inputFileName.lastIndexOf(".");
      if(point == -1){
      inputFileNameExtension = "";
      baseInputFileName = inputFileName;
      }
      else{
      inputFileNameExtension = inputFileName.substring(point);
      baseInputFileName = inputFileName.substring(0, point);
      }
      try{
      inputFileNamePath = inFile.getCanonicalPath();
      }catch(IOException ioe){
      System.out.println("Error: Failed to get path of " + args[0]);
      System.exit(4);
      }
      try{
      fileIn = new FileInputStream(args[0]);
      bufIn = new BufferedInputStream(fileIn, bufSize);
      }catch(IOException ioe){
      System.out.println("Error: Failed to open input file" + args[0]);
      System.exit(4);
      }
      while(true){
      String outFileName = args[1] + "/" + baseInputFileName + suffix + ".gz";
      try{
      fileOut = new FileOutputStream( outFileName );
      gzipOut = new GZIPOutputStream( fileOut );
      }catch(IOException ioe){
      System.out.println("Error: Failed to open output file" + outFileName);
      System.exit(4);
      }
      while( bytesRead != -1 ){
      try{
      bytesRead = bufIn.read( buffer, 0, bufSize - 2);
      }catch(IOException ioe){
      System.out.println("Error reading " + args[0]);
      System.exit(4);
      }
      if( bytesRead == -1 ){
      try{
      bufIn.close();
      gzipOut.close();
      System.out.println("Total Bytes read: " + totalBytesRead);
      System.out.format("Compression ratio: %5.2f\n", ((double)totalBytesRead) / totalLen);
      }catch(IOException ioe){
      System.out.println("Error closing files");
      System.exit(4);
      }
      break;
      }
      totalBytesRead += bytesRead;
      try{
      gzipOut.write(buffer, 0, bytesRead);
      gzipOut.finish();
      gzipOut.flush();
      }catch(IOException ioe){
      System.out.println("Error writing " + outFileName + "\nReason: " + ioe.getMessage());
      System.exit(4);
      }
      File f = new File(outFileName);
      long len = 0;
      if( (len = f.length()) > 690000000 ){
      try{
      gzipOut.close();
      }catch(IOException ioe){
      System.out.println("Error closing " + outFileName + "\nReason: " + ioe.getMessage());
      System.exit(4);
      }
      f = new File(outFileName);
      len = f.length();
      totalLen += len;
      len = 0;
      break;
      }
      System.out.format("Compression ratio: %5.2f\n", ((double)totalBytesRead) / (len + totalLen));
      }
      if( bytesRead == -1 ) break;
      ++suffix;
      }
      }

      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Comment out line 93 GZIPOutputStream.finish();

            sherman Xueming Shen
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: