0

I am trying to find the size of uncompressed bz2 files using the following code. However,after running the code, I get the size as 0 bytes. Don't know what is wrong. Could someone please point out.

try{
                FileInputStream fin = new FileInputStream("/users/praveen/data1/00.json.bz2");
                BufferedInputStream in = new BufferedInputStream(fin);


                BZip2CompressorInputStream bzIn = new BZip2CompressorInputStream(in);
                  long size = 0;
                  while (bzIn.available() > 0)
                  {
                    byte[] buf = new byte[1024];
                    int read = bzIn.read(buf);
                    if (read > 0) size += read;
                  }

                  System.out.println("File Size: " + size + "bytes");
                  bzIn.close();
                //bzIn.close();
                }
                catch (Exception e) {
                throw new Error(e.getMessage());
                } 

1 Answers1

2

It is very probable that BZip2CompressorInputStream does not fully implement the available() method. It probably just returns 0. Instead, you should try using InputStream#read(byte[]) and checking for a -1 return.

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152
ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
  • @keerthupraveen In this case, you should consider filing an issue with the maintainers to see whether they could implement `available()`. – chrylis -cautiouslyoptimistic- Jul 17 '15 at 04:30
  • In general available() is not to be trusted. I suspected the answer in this case because I'd seen it with another InputStream (maybe CipherInputStream?) The problem with getting available to work correctly with these streams is that it generally requires reading a chunk of data first into a buffer and then translating/uncompressing it, which adds object/memory allocation that otherwise wouldn't be necessary... The choice is between minimum memory impact and implementation of available, and they chose the former – ControlAltDel Jul 17 '15 at 13:32