GZIPOutputStream and MemoryCacheImageOutputStream

B

brechmos

I am working through some code I had written several years ago and am
sure that it worked then... I figured out my problem comes down to some
interaction with GZIPOutputStream and MemoryCacheImageOutputStream. A
sample of code to show the problem is below...

If I comment out the MemoryCacheImageOutputStream and put in the
DataOutputStream it will work.

If I leave the MemoryCacheImageOutputStream and comment out the
GZIPOutputStream wrapper it will work.

Why does it not work with the MemoryCacheImageOutputStream and
GZIPOutputStream??

If I write out more data I watch a file in /tmp grow but when the
program finishes the file frank.txt.gz is still only 10 bytes (an empty
gzip?).

Any help would be appreciated!

import java.util.*;
import java.io.*;
import java.nio.*;
import java.util.zip.*;
import javax.imageio.stream.*;

public class Bob
{
public static void main(String[] args)
{
OutputStream fos = null;
String filename = "frank.txt.gz";

try {
fos = new FileOutputStream(new File(filename));
if( filename.endsWith(".gz") )
{ fos = new GZIPOutputStream( fos ); }
fos = new BufferedOutputStream( fos );
} catch (IOException e) { System.out.println(e);
System.exit(0); }

ImageOutputStream fcos = new MemoryCacheImageOutputStream(fos);
// ImageOutputStream fcos = null; try { fcos = new
FileCacheImageOutputStream(fos, null);
} catch (Exception e) {}
// DataOutputStream fcos = new DataOutputStream(fos);

try {
fcos.writeBytes("Here I am\n");
fcos.close();
}
catch( Exception e)
{
System.out.println("could not write " + e);
System.exit(-1);
}

}

public Bob()
{
}
}
 
R

Roedy Green

If I write out more data I watch a file in /tmp grow but when the
program finishes the file frank.txt.gz is still only 10 bytes (an empty
gzip?).

I think you need more proof that than it did not work. Try reading the
file with the reverse program and see if you the back where you
started.

I am surprised DataOutputStream.writeBytes has not been deprecated.
For that sort of thing you really want a PrintWriter.
 
R

Roedy Green

fos = new FileOutputStream(new File(filename));

this is the naming convention used in the File IO Amanuensis. I have
wonder when I see people using it how that came to be:

1. convergent evolution to an obvious convention.

2. lots of people cut their teeth using
http://mindprod.com/applets/fileio.html

3. people copy others in a chain back to a file i/o amanuensis user.
 
C

Chris Uppal

try {
fcos.writeBytes("Here I am\n");
fcos.close();
}

The JavaDoc for MemoryCacheImageOutputStream.close() states that it does not
close() the underlying OutputStream (which is unexpected). So you have to
close() that yourself before leaving the program.

-- chris
 
R

Roedy Green

The JavaDoc for MemoryCacheImageOutputStream.close() states that it does not
close() the underlying OutputStream (which is unexpected). So you have to
close() that yourself before leaving the program.

Why would they do that?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top