. A file is used to
cache data until it is flushed to the output stream.
| Method from javax.imageio.stream.FileCacheImageOutputStream Detail: |
public void close() throws IOException {
maxStreamPos = cache.length();
seek(maxStreamPos);
flushBefore(maxStreamPos);
super.close();
cache.close();
cache = null;
cacheFile.delete();
cacheFile = null;
stream.flush();
stream = null;
StreamCloser.removeFromQueue(this);
}
Closes this FileCacheImageOututStream. All
pending data is flushed to the output, and the cache file
is closed and removed. The destination OutputStream
is not closed. |
public void flushBefore(long pos) throws IOException {
long oFlushedPos = flushedPos;
super.flushBefore(pos); // this will call checkClosed() for us
long flushBytes = flushedPos - oFlushedPos;
if (flushBytes > 0) {
int bufLen = 512;
byte[] buf = new byte[bufLen];
cache.seek(oFlushedPos);
while (flushBytes > 0) {
int len = (int)Math.min(flushBytes, bufLen);
cache.readFully(buf, 0, len);
stream.write(buf, 0, len);
flushBytes -= len;
}
stream.flush();
}
}
|
public boolean isCached() {
return true;
}
Returns true since this
ImageOutputStream caches data in order to allow
seeking backwards. |
public boolean isCachedFile() {
return true;
}
Returns true since this
ImageOutputStream maintains a file cache. |
public boolean isCachedMemory() {
return false;
}
Returns false since this
ImageOutputStream does not maintain a main memory
cache. |
public long length() {
try {
checkClosed();
return cache.length();
} catch (IOException e) {
return -1L;
}
}
|
public int read() throws IOException {
checkClosed();
bitOffset = 0;
int val = cache.read();
if (val != -1) {
++streamPos;
}
return val;
}
|
public int read(byte[] b,
int off,
int len) throws IOException {
checkClosed();
if (b == null) {
throw new NullPointerException("b == null!");
}
if (off < 0 || len < 0 || off + len > b.length || off + len < 0) {
throw new IndexOutOfBoundsException
("off < 0 || len < 0 || off+len > b.length || off+len < 0!");
}
bitOffset = 0;
if (len == 0) {
return 0;
}
int nbytes = cache.read(b, off, len);
if (nbytes != -1) {
streamPos += nbytes;
}
return nbytes;
}
|
public void seek(long pos) throws IOException {
checkClosed();
if (pos < flushedPos) {
throw new IndexOutOfBoundsException();
}
cache.seek(pos);
this.streamPos = cache.getFilePointer();
maxStreamPos = Math.max(maxStreamPos, streamPos);
this.bitOffset = 0;
}
Sets the current stream position and resets the bit offset to
0. It is legal to seek past the end of the file; an
EOFException will be thrown only if a read is
performed. The file length will not be increased until a write
is performed. |
public void write(int b) throws IOException {
flushBits(); // this will call checkClosed() for us
cache.write(b);
++streamPos;
maxStreamPos = Math.max(maxStreamPos, streamPos);
}
|
public void write(byte[] b,
int off,
int len) throws IOException {
flushBits(); // this will call checkClosed() for us
cache.write(b, off, len);
streamPos += len;
maxStreamPos = Math.max(maxStreamPos, streamPos);
}
|