1

I wrote a code that writes compressed objects into a file, My questions is: is there a way that I could keep track of the increment of size of my file as the object being wrote in? here is my code:

public static void storeCompressedObjs(File outFile, ArrayList<Object[]> obj) {
    FileOutputStream fos = null;
    GZIPOutputStream gz = null;
    ObjectOutputStream oos = null;
    try {
        fos = new FileOutputStream(outFile);
        gz = new GZIPOutputStream(fos);
        oos = new ObjectOutputStream(gz);
        for (Object str : obj) {
            oos.writeObject(str);
            oos.flush();
            //I was hoping to print outFile.length() here, but it doesn't work
        }

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        oos.close();
        gz.close();
        fos.close();
    }
}

I tried to use flush after every oos.writeObject(str); and then get the file size by using outFile.length(), but no matter how much I flush it, the file size remain unchanged until the last jump to its final size. Anyway that I could fix it? Thanks

user685275
  • 2,097
  • 8
  • 26
  • 32

1 Answers1

1

The Apache Commons project provides a class CountingOutputStream, which you can put into your chain of OutputStreams. You can even have two of them:

package so5997784;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.zip.GZIPOutputStream;

import org.apache.commons.io.output.CountingOutputStream;

public class CountBytes {

  private static void dump(File outFile, Object... objs) throws IOException {
    FileOutputStream fos = new FileOutputStream(outFile);
    try {
      CountingOutputStream compressedCounter = new CountingOutputStream(fos);
      OutputStream gz = new GZIPOutputStream(compressedCounter);
      CountingOutputStream uncompressedCounter = new CountingOutputStream(gz);
      ObjectOutputStream oos = new ObjectOutputStream(uncompressedCounter);

      for (Object obj : objs) {
        oos.writeObject(obj);
        oos.flush();
        System.out.println(uncompressedCounter.getByteCount() + " -> " + compressedCounter.getByteCount());
      }
      oos.close();
      System.out.println(uncompressedCounter.getByteCount() + " -> " + compressedCounter.getByteCount());

    } finally {
      fos.close();
    }
  }

  public static void main(String[] args) throws IOException {
    File outFile = new File("objects.out.gz");
    dump(outFile, "a", "b", "cde", "hello", "world");
  }

}
Roland Illig
  • 40,703
  • 10
  • 88
  • 121
  • When I was calling the method, I got `8 -> 10 12 -> 10 18 -> 10 26 -> 10 34 -> 10 34 -> 51` Why is there 5 10s there, shouldn't it be incrementing? – user685275 May 14 '11 at 05:02
  • Anyways, this achieves what I want, Thanks so much for your help – user685275 May 14 '11 at 05:21
  • This is because `GZIPOutputStream.flush()` *does not* flush its output, maybe to guarantee some compression level or because the file format doesn't allow for it. – Roland Illig May 14 '11 at 05:58