• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Order of close() statements GZIP

 
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

When i have the files nested this way, is there an order that the close statements should be in?

I keep getting a Corrupt GZIP trailer for some reason!

Thanks much!


GZIPInputStream in = null;
GZIPOutputStream gz = null;
PrintWriter outf = null;
byte[] buf=new byte[4096];

try
{
gz = new GZIPOutputStream(new FileOutputStream(xml_file));
outf = new PrintWriter (gz);
......
} catch ......

in.close();
gz.finish();
gz.close();
outf.close();
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The PrintWriter will line-buffer the output; the last line won't necessarily be sent along until you call flush() on the PrintWriter, or until you close it. So by closing the gzip stream first, you're actually closing it before all the data can be sent to it.

You actually only need to call close on the PrintWriter; it will call close on the gzip stream, which will in turn close the file stream, and everything will work fine.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic