org.geotoolkit.image.io
Class StreamImageWriter

Object
  extended by ImageWriter
      extended by SpatialImageWriter
          extended by StreamImageWriter
All Implemented Interfaces:
ImageTranscoder, WarningProducer, Disposable, Localized
Direct Known Subclasses:
FileImageWriter, TextImageWriter

public abstract class StreamImageWriter
extends SpatialImageWriter

Base class for image writers that expect an OutputStream or channel output. This class provides a getOutputStream() method, which return the output as an OutputStream for convenience.

Different kinds of outputs are automatically handled. The default implementation handles all the following types:

String, File, URI, URL, URLConnection, OutputStream, ImageOutputStream, WritableByteChannel.
Note the TextImageWriter subclass can go one step further by wrapping the InputStream into a BufferedWriter using some character encoding.

Since:
2.4
Version:
3.20
Author:
Martin Desruisseaux (IRD, Geomatys)
See Also:
StreamImageReader
Module:
coverage/geotk-coverageio (download)    View source code for this class

Nested Class Summary
protected static class StreamImageWriter.Spi
          Service provider interface (SPI) for StreamImageWriters.
 
Field Summary
protected  Closeable closeOnReset
          The stream to close on setOutput(.Object), ImageWriter.reset() or SpatialImageWriter.dispose() method invocation.
 
Fields inherited from class ImageWriter
availableLocales, locale, originatingProvider, output, progressListeners, warningListeners, warningLocales
 
Fields inherited from interface WarningProducer
LOGGER
 
Constructor Summary
protected StreamImageWriter(StreamImageWriter.Spi provider)
          Constructs a new image writer.
 
Method Summary
protected  void close()
          Closes the output stream created by getOutputStream().
protected  void finalize()
          Invokes the close() method when this writer is garbage-collected.
protected  OutputStream getOutputStream()
          Returns the output as an output stream object.
 void setOutput(Object output)
          Sets the output source to use.
 
Methods inherited from class SpatialImageWriter
canWriteRasters, computeSize, convertImageMetadata, convertStreamMetadata, createRectIter, dispose, getDefaultImageMetadata, getDefaultStreamMetadata, getDefaultWriteParam, getSampleModel, processImageComplete, processImageStarted, processThumbnailComplete, processThumbnailStarted, processWarningOccurred, processWarningOccurred, warningOccurred
 
Methods inherited from class ImageWriter
abort, abortRequested, addIIOWriteProgressListener, addIIOWriteWarningListener, canInsertEmpty, canInsertImage, canRemoveImage, canReplaceImageMetadata, canReplacePixels, canReplaceStreamMetadata, canWriteEmpty, canWriteSequence, clearAbortRequest, endInsertEmpty, endReplacePixels, endWriteEmpty, endWriteSequence, getAvailableLocales, getLocale, getNumThumbnailsSupported, getOriginatingProvider, getOutput, getPreferredThumbnailSizes, prepareInsertEmpty, prepareReplacePixels, prepareWriteEmpty, prepareWriteSequence, processImageProgress, processImageStarted, processThumbnailProgress, processThumbnailStarted, processWarningOccurred, processWarningOccurred, processWriteAborted, removeAllIIOWriteProgressListeners, removeAllIIOWriteWarningListeners, removeIIOWriteProgressListener, removeIIOWriteWarningListener, removeImage, replaceImageMetadata, replacePixels, replacePixels, replaceStreamMetadata, reset, setLocale, write, write, write, writeInsert, writeToSequence
 
Methods inherited from class Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface Localized
getLocale
 

Field Detail

closeOnReset

protected Closeable closeOnReset
The stream to close on setOutput(.Object), ImageWriter.reset() or SpatialImageWriter.dispose() method invocation. This stream is typically an output stream or a writer created by getOutputStream() or similar methods in subclasses.

This field is never equal to the user-specified output, since the usual ImageWriter contract is to not close the user-provided stream. It is set to a non-null value only if a stream has been created from an other user object like File or URL.

See Also:
getOutputStream(), TextImageWriter.getWriter(ImageWriteParam), close()
Constructor Detail

StreamImageWriter

protected StreamImageWriter(StreamImageWriter.Spi provider)
Constructs a new image writer.

Parameters:
provider - The ImageWriterSpi that is constructing this object, or null.
Method Detail

setOutput

public void setOutput(Object output)
Sets the output source to use. Output can be any of the object documented in the class javadoc, namely String, File, URI, URL, URLConnection, OutputStream, ImageOutputStream or WritableByteChannel.

If the given output is null, then any currently set output source will be removed.

Overrides:
setOutput in class SpatialImageWriter
Parameters:
output - The output object to use for future writing.
See Also:
ImageWriter.getOutput(), getOutputStream()

getOutputStream

protected OutputStream getOutputStream()
                                throws IllegalStateException,
                                       IOException
Returns the output as an output stream object. If the output is already an output stream, it is returned unchanged. Otherwise this method creates a new output stream (usually not buffered) from File, URI, URL, URLConnection, ImageOutputStream or WritableByteChannel outputs.

This method creates a new output stream only when first invoked. All subsequent calls will returns the same instance. Consequently, the returned stream should never be closed by the caller. It may be closed automatically when setOutput(java.lang.Object), ImageWriter.reset() or SpatialImageWriter.dispose() methods are invoked.

Returns:
ImageWriter.getOutput() as an OutputStream. This output stream is usually not buffered.
Throws:
IllegalStateException - if the output is not set.
IOException - If the output stream can't be created for an other reason.
See Also:
ImageWriter.getOutput(), TextImageWriter.getWriter(ImageWriteParam)

close

protected void close()
              throws IOException
Closes the output stream created by getOutputStream(). This method does nothing if the output stream is the output instance given by the user rather than a stream created by this class from a File or URL output.

This method is invoked automatically by setOutput(Object), ImageWriter.reset(), SpatialImageWriter.dispose() or finalize() methods and doesn't need to be invoked explicitly. It has protected access only in order to allow overriding by subclasses.

Throws:
IOException - if an error occurred while closing the stream.
See Also:
closeOnReset

finalize

protected void finalize()
                 throws Throwable
Invokes the close() method when this writer is garbage-collected. Note that this will actually close the stream only if it has been created by this writer, rather than supplied by the user.

Overrides:
finalize in class Object
Throws:
Throwable


Copyright © 2009-2012 Geotoolkit.org. All Rights Reserved.