wood burning stoves 2.0*
The moose likes Java in General and the fly likes Order of close() statements GZIP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Order of close() statements GZIP" Watch "Order of close() statements GZIP" New topic
Author

Order of close() statements GZIP

bob connolly
Ranch Hand

Joined: Mar 10, 2004
Posts: 204
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();
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

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.


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Order of close() statements GZIP
 
Similar Threads
Corrupt GZIP trailer
datastream with additional parameter
Problems with Compression Servlet Filter
JSP in websphere throwing illegal state exception
JSP with large data loads slow in Solaris10 OS , we are using Jrun4 and nested logic:iterate