APIdia Launch Partner
Top Description Fields Constructors Methods
javax.imageio.stream

public Class FileCacheImageOutputStream

extends ImageOutputStreamImpl
Class Inheritance
Imports
java.io.File, .IOException, .OutputStream, .RandomAccessFile, java.nio.file.Files, javax.imageio.stream.FileCacheImageInputStream.StreamDisposerRecord, com.sun.imageio.stream.StreamCloser, sun.java2d.Disposer, .DisposerRecord

An implementation of ImageOutputStream that writes its output to a regular OutputStream. A file is used to cache data until it is flushed to the output stream.

Field Summary

Modifier and TypeField and Description
private RandomAccessFile
private File
private final StreamCloser.CloseAction
closeAction

The CloseAction that closes the stream in the StreamCloser's shutdown hook

private final FileCacheImageInputStream.StreamDisposerRecord
private final Object
private long
private OutputStream

Constructor Summary

AccessConstructor and Description
public
FileCacheImageOutputStream(OutputStream
an OutputStream to write to.
stream
,
File
a File indicating where the cache file should be created, or null to use the system directory.
cacheDir
)

Constructs a FileCacheImageOutputStream that will write to a given outputStream.

Method Summary

Modifier and TypeMethod and Description
public void
close()

Overrides javax.imageio.stream.ImageInputStreamImpl.close.

Implements javax.imageio.stream.ImageInputStream.close.

Closes this FileCacheImageOutputStream.

public void
flushBefore(long pos)

Overrides javax.imageio.stream.ImageInputStreamImpl.flushBefore.

Implements javax.imageio.stream.ImageOutputStream.flushBefore, javax.imageio.stream.ImageInputStream.flushBefore.

Flushes all data prior to the given position to the underlying destination, such as an OutputStream or File.

public boolean

Returns:

true.
isCached
()

Overrides javax.imageio.stream.ImageInputStreamImpl.isCached.

Implements javax.imageio.stream.ImageInputStream.isCached.

Returns true since this ImageOutputStream caches data in order to allow seeking backwards.

public boolean

Returns:

true.
isCachedFile
()

Overrides javax.imageio.stream.ImageInputStreamImpl.isCachedFile.

Implements javax.imageio.stream.ImageInputStream.isCachedFile.

Returns true since this ImageOutputStream maintains a file cache.

public boolean

Returns:

false.
isCachedMemory
()

Overrides javax.imageio.stream.ImageInputStreamImpl.isCachedMemory.

Implements javax.imageio.stream.ImageInputStream.isCachedMemory.

Returns false since this ImageOutputStream does not maintain a main memory cache.

public long
length()

Overrides javax.imageio.stream.ImageInputStreamImpl.length.

Implements javax.imageio.stream.ImageInputStream.length.

Returns the total length of the stream, if known.

public int
read()

Implements abstract javax.imageio.stream.ImageInputStreamImpl.read.

Implements javax.imageio.stream.ImageInputStream.read.

Reads a single byte from the stream and returns it as an integer between 0 and 255.

public int
read(byte[] b, int off, int len)

Implements abstract javax.imageio.stream.ImageInputStreamImpl.read.

Implements javax.imageio.stream.ImageInputStream.read.

Reads up to len bytes from the stream, and stores them into b starting at index off.

public void
seek(long pos)

Overrides javax.imageio.stream.ImageInputStreamImpl.seek.

Implements javax.imageio.stream.ImageInputStream.seek.

Sets the current stream position and resets the bit offset to 0.

public void
write(int b)

Implements abstract javax.imageio.stream.ImageOutputStreamImpl.write.

Implements javax.imageio.stream.ImageOutputStream.write.

Writes a single byte to the stream at the current position.

public void
write(byte[] b, int off, int len)

Implements abstract javax.imageio.stream.ImageOutputStreamImpl.write.

Implements javax.imageio.stream.ImageOutputStream.write.

Writes a sequence of bytes to the stream at the current position.

Inherited from javax.imageio.stream.ImageOutputStreamImpl:
flushBitswritewriteBitwriteBitswriteBooleanwriteBytewriteByteswriteCharwriteCharswriteCharswriteDoublewriteDoubleswriteFloatwriteFloatswriteIntwriteIntswriteLongwriteLongswriteShortwriteShortswriteUTF

Field Detail

cacheback to summary
private RandomAccessFile cache
cacheFileback to summary
private File cacheFile
closeActionback to summary
private final StreamCloser.CloseAction closeAction

The CloseAction that closes the stream in the StreamCloser's shutdown hook

disposerRecordback to summary
private final FileCacheImageInputStream.StreamDisposerRecord disposerRecord
disposerReferentback to summary
private final Object disposerReferent
maxStreamPosback to summary
private long maxStreamPos
streamback to summary
private OutputStream stream

Constructor Detail

FileCacheImageOutputStreamback to summary
public FileCacheImageOutputStream(OutputStream stream, File cacheDir) throws IOException

Constructs a FileCacheImageOutputStream that will write to a given outputStream.

A temporary file is used as a cache. If cacheDir is non-null and is a directory, the file will be created there. If it is null, the system-dependent default temporary-file directory will be used (see the documentation for File.createTempFile for details).

Parameters
stream:OutputStream

an OutputStream to write to.

cacheDir:File

a File indicating where the cache file should be created, or null to use the system directory.

Exceptions
IOException:
if a cache file cannot be created.
IllegalArgumentException:
  • if stream is null.
  • if cacheDir is non-null but is not a directory.

Method Detail

closeback to summary
public void close() throws IOException

Overrides javax.imageio.stream.ImageInputStreamImpl.close.

Implements javax.imageio.stream.ImageInputStream.close.

Closes this FileCacheImageOutputStream. All pending data is flushed to the output, and the cache file is closed and removed. The destination OutputStream is not closed.

Exceptions
IOException:
if an error occurs.
flushBeforeback to summary
public void flushBefore(long pos) throws IOException

Overrides javax.imageio.stream.ImageInputStreamImpl.flushBefore.

Implements javax.imageio.stream.ImageOutputStream.flushBefore, javax.imageio.stream.ImageInputStream.flushBefore.

Doc from javax.imageio.stream.ImageOutputStream.flushBefore.

Flushes all data prior to the given position to the underlying destination, such as an OutputStream or File. Attempting to seek to the flushed portion of the stream will result in an IndexOutOfBoundsException.

Exceptions
IOException:
if an I/O error occurs.
isCachedback to summary
public boolean isCached()

Overrides javax.imageio.stream.ImageInputStreamImpl.isCached.

Implements javax.imageio.stream.ImageInputStream.isCached.

Returns true since this ImageOutputStream caches data in order to allow seeking backwards.

Returns:boolean

true.

See Also
isCachedMemory, isCachedFile
isCachedFileback to summary
public boolean isCachedFile()

Overrides javax.imageio.stream.ImageInputStreamImpl.isCachedFile.

Implements javax.imageio.stream.ImageInputStream.isCachedFile.

Returns true since this ImageOutputStream maintains a file cache.

Returns:boolean

true.

See Also
isCached, isCachedMemory
isCachedMemoryback to summary
public boolean isCachedMemory()

Overrides javax.imageio.stream.ImageInputStreamImpl.isCachedMemory.

Implements javax.imageio.stream.ImageInputStream.isCachedMemory.

Returns false since this ImageOutputStream does not maintain a main memory cache.

Returns:boolean

false.

See Also
isCached, isCachedFile
lengthback to summary
public long length()

Overrides javax.imageio.stream.ImageInputStreamImpl.length.

Implements javax.imageio.stream.ImageInputStream.length.

Doc from javax.imageio.stream.ImageInputStream.length.

Returns the total length of the stream, if known. Otherwise, -1 is returned.

readback to summary
public int read() throws IOException

Implements abstract javax.imageio.stream.ImageInputStreamImpl.read.

Implements javax.imageio.stream.ImageInputStream.read.

Doc from javax.imageio.stream.ImageInputStream.read.

Reads a single byte from the stream and returns it as an integer between 0 and 255. If the end of the stream is reached, -1 is returned.

The bit offset within the stream is reset to zero before the read occurs.

Exceptions
IOException:
if an I/O error occurs.
readback to summary
public int read(byte[] b, int off, int len) throws IOException

Implements abstract javax.imageio.stream.ImageInputStreamImpl.read.

Implements javax.imageio.stream.ImageInputStream.read.

Doc from javax.imageio.stream.ImageInputStream.read.

Reads up to len bytes from the stream, and stores them into b starting at index off. The number of bytes read is returned. If no bytes can be read because the end of the stream has been reached, -1 is returned.

The bit offset within the stream is reset to zero before the read occurs.

Exceptions
IOException:
if an I/O error occurs.
seekback to summary
public void seek(long pos) throws IOException

Overrides javax.imageio.stream.ImageInputStreamImpl.seek.

Implements javax.imageio.stream.ImageInputStream.seek.

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.

Exceptions
IOException:
if any other I/O error occurs.
IndexOutOfBoundsException:
if pos is smaller than the flushed position.
writeback to summary
public void write(int b) throws IOException

Implements abstract javax.imageio.stream.ImageOutputStreamImpl.write.

Implements javax.imageio.stream.ImageOutputStream.write.

Doc from javax.imageio.stream.ImageOutputStream.write.

Writes a single byte to the stream at the current position. The 24 high-order bits of b are ignored.

If the bit offset within the stream is non-zero, the remainder of the current byte is padded with 0s and written out first. The bit offset will be 0 after the write. Implementers can use the flushBits method of ImageOutputStreamImpl to guarantee this.

Exceptions
IOException:
if an I/O error occurs.
writeback to summary
public void write(byte[] b, int off, int len) throws IOException

Implements abstract javax.imageio.stream.ImageOutputStreamImpl.write.

Implements javax.imageio.stream.ImageOutputStream.write.

Doc from javax.imageio.stream.ImageOutputStream.write.

Writes a sequence of bytes to the stream at the current position. If len is 0, nothing is written. The byte b[off] is written first, then the byte b[off + 1], and so on.

If the bit offset within the stream is non-zero, the remainder of the current byte is padded with 0s and written out first. The bit offset will be 0 after the write. Implementers can use the flushBits method of ImageOutputStreamImpl to guarantee this.

Exceptions
IOException:
if an I/O error occurs.