This implements a bunch of missing methods in ImageIO's
FileCacheImageOutputStream.
2006-12-12 Roman Kennke <[EMAIL PROTECTED]>
* javax/imageio/FileCacheImageOutputStream.java
(cache): New field. The actual cache as RandomAccessFile.
(cacheDir): Removed. Not needed at all.
(cacheFile): New field. The cache file.
(maxPos): New field. Stores the maximum position.
(FileCacheImageOutputStream): Initialize cache file.
(checkStreamClosed): Removed. This should be done by the super
method checkClosed().
(close): Properly close the stream and delete the cache.
(flushBefore): Implemented to also flush the cache.
(length): Implemented to return the length of the cache.
(read()): Implemented to read from the cache.
(read(byte[],int,int)): Likewise.
(seek): Implemented to seek the cache too.
(write(int)): Write to cache.
(write(byte[],int,int))
/Roman
Index: javax/imageio/stream/FileCacheImageOutputStream.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/imageio/stream/FileCacheImageOutputStream.java,v
retrieving revision 1.5
diff -u -1 -5 -r1.5 FileCacheImageOutputStream.java
--- javax/imageio/stream/FileCacheImageOutputStream.java 22 Mar 2006 19:15:25 -0000 1.5
+++ javax/imageio/stream/FileCacheImageOutputStream.java 12 Dec 2006 21:36:42 -0000
@@ -26,100 +26,153 @@
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version. */
package javax.imageio.stream;
-import gnu.classpath.NotImplementedException;
-
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
+import java.io.RandomAccessFile;
/**
* @author Michael Koch ([EMAIL PROTECTED])
*/
public class FileCacheImageOutputStream extends ImageOutputStreamImpl
{
private OutputStream stream;
- private File cacheDir;
-
- public FileCacheImageOutputStream(OutputStream stream, File cacheDir)
- throws IOException
- {
- super();
- this.stream = stream;
- // FIXME: We do not support caching yet.
- this.cacheDir = cacheDir;
- }
+ private File cacheFile;
+ private RandomAccessFile cache;
+ private long maxPos;
- public void close()
+ public FileCacheImageOutputStream(OutputStream s, File cacheDir)
throws IOException
{
- if (stream != null)
- {
- stream.close();
- stream = null;
- }
+ stream = s;
+ cacheFile = File.createTempFile("imageio", ".tmp", cacheDir);
+ cache = new RandomAccessFile(cacheFile, "rw");
+ maxPos = 0;
}
- private void checkStreamClosed()
+ public void close()
throws IOException
{
- if (stream == null)
- throw new IOException("stream closed");
+ maxPos = cache.length();
+ seek(maxPos);
+ flushBefore(maxPos);
+ super.close();
+ cache.close();
+ cacheFile.delete();
+ stream.flush();
+ stream = null;
}
public boolean isCached()
{
return true;
}
public boolean isCachedFile()
{
return true;
}
public boolean isCachedMemory()
{
return false;
}
public int read()
- throws IOException, NotImplementedException
+ throws IOException
{
- // FIXME: Implement me.
- throw new Error("not implemented");
+ bitOffset = 0;
+ int val = cache.read();
+ if (val != -1)
+ streamPos++;
+ return val;
}
public int read(byte[] data, int offset, int len)
- throws IOException, NotImplementedException
+ throws IOException
{
- // FIXME: Implement me.
- throw new Error("not implemented");
+ bitOffset = 0;
+ int num = cache.read(data, offset, len);
+ if (num != -1)
+ streamPos += num;
+ return num;
}
public void write(byte[] data, int offset, int len)
throws IOException
{
- checkStreamClosed();
- // FIXME: Flush pending bits.
- stream.write(data, offset, len);
+ flushBits();
+ cache.write(data, offset, len);
+ streamPos += len;
+ maxPos = Math.max(streamPos, maxPos);
}
public void write(int value)
throws IOException
{
- checkStreamClosed();
- // FIXME: Flush pending bits.
- stream.write(value);
+ flushBits();
+ cache.write(value);
+ streamPos++;
+ maxPos = Math.max(streamPos, maxPos);
+ }
+
+ public long length()
+ {
+ long l;
+ try
+ {
+ l = cache.length();
+ }
+ catch (IOException ex)
+ {
+ l = -1;
+ }
+ return l;
+ }
+
+ public void seek(long pos)
+ throws IOException
+ {
+ checkClosed();
+ if (pos < flushedPos)
+ throw new IndexOutOfBoundsException();
+ cache.seek(pos);
+ streamPos = cache.getFilePointer();
+ maxPos = Math.max(streamPos, maxPos);
+ bitOffset = 0;
+ }
+
+ public void flushBefore(long pos)
+ throws IOException
+ {
+ long oldPos = flushedPos;
+ super.flushBefore(pos);
+ long flushed = flushedPos - oldPos;
+ if (flushed > 0)
+ {
+ int len = 512;
+ byte[] buf = new byte[len];
+ cache.seek(oldPos);
+ while (flushed > 0)
+ {
+ int l = (int) Math.min(flushed, len);
+ cache.readFully(buf, 0, l);
+ stream.write(buf, 0, l);
+ flushed -= l;
+ }
+ stream.flush();
+ }
}
}