Path: blob/master/src/java.desktop/share/classes/javax/imageio/ImageWriter.java
41152 views
/*1* Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.imageio;2627import java.awt.Dimension;28import java.awt.Rectangle;29import java.awt.image.BufferedImage;30import java.awt.image.RenderedImage;31import java.awt.image.Raster;32import java.io.IOException;33import java.util.ArrayList;34import java.util.List;35import java.util.Locale;36import java.util.MissingResourceException;37import java.util.ResourceBundle;38import javax.imageio.event.IIOWriteWarningListener;39import javax.imageio.event.IIOWriteProgressListener;40import javax.imageio.metadata.IIOMetadata;41import javax.imageio.stream.ImageOutputStream;42import javax.imageio.spi.ImageWriterSpi;4344/**45* An abstract superclass for encoding and writing images. This class46* must be subclassed by classes that write out images in the context47* of the Java Image I/O framework.48*49* <p> {@code ImageWriter} objects are normally instantiated by50* the service provider class for the specific format. Service51* provider classes are registered with the {@code IIORegistry},52* which uses them for format recognition and presentation of53* available format readers and writers.54*55* @see ImageReader56* @see ImageWriteParam57* @see javax.imageio.spi.IIORegistry58* @see javax.imageio.spi.ImageWriterSpi59*60*/61public abstract class ImageWriter implements ImageTranscoder {6263/**64* The {@code ImageWriterSpi} that instantiated this object,65* or {@code null} if its identity is not known or none66* exists. By default it is initialized to {@code null}.67*/68protected ImageWriterSpi originatingProvider = null;6970/**71* The {@code ImageOutputStream} or other {@code Object}72* set by {@code setOutput} and retrieved by73* {@code getOutput}. By default it is initialized to74* {@code null}.75*/76protected Object output = null;7778/**79* An array of {@code Locale}s that may be used to localize80* warning messages and compression setting values, or81* {@code null} if localization is not supported. By default82* it is initialized to {@code null}.83*/84protected Locale[] availableLocales = null;8586/**87* The current {@code Locale} to be used for localization, or88* {@code null} if none has been set. By default it is89* initialized to {@code null}.90*/91protected Locale locale = null;9293/**94* A {@code List} of currently registered95* {@code IIOWriteWarningListener}s, initialized by default to96* {@code null}, which is synonymous with an empty97* {@code List}.98*/99protected List<IIOWriteWarningListener> warningListeners = null;100101/**102* A {@code List} of {@code Locale}s, one for each103* element of {@code warningListeners}, initialized by default104* {@code null}, which is synonymous with an empty105* {@code List}.106*/107protected List<Locale> warningLocales = null;108109/**110* A {@code List} of currently registered111* {@code IIOWriteProgressListener}s, initialized by default112* {@code null}, which is synonymous with an empty113* {@code List}.114*/115protected List<IIOWriteProgressListener> progressListeners = null;116117/**118* If {@code true}, the current write operation should be119* aborted.120*/121private boolean abortFlag = false;122123/**124* Constructs an {@code ImageWriter} and sets its125* {@code originatingProvider} instance variable to the126* supplied value.127*128* <p> Subclasses that make use of extensions should provide a129* constructor with signature {@code (ImageWriterSpi, Object)}130* in order to retrieve the extension object. If131* the extension object is unsuitable, an132* {@code IllegalArgumentException} should be thrown.133*134* @param originatingProvider the {@code ImageWriterSpi} that135* is constructing this object, or {@code null}.136*/137protected ImageWriter(ImageWriterSpi originatingProvider) {138this.originatingProvider = originatingProvider;139}140141/**142* Returns the {@code ImageWriterSpi} object that created143* this {@code ImageWriter}, or {@code null} if this144* object was not created through the {@code IIORegistry}.145*146* <p> The default implementation returns the value of the147* {@code originatingProvider} instance variable.148*149* @return an {@code ImageWriterSpi}, or {@code null}.150*151* @see ImageWriterSpi152*/153public ImageWriterSpi getOriginatingProvider() {154return originatingProvider;155}156157/**158* Sets the destination to the given159* {@code ImageOutputStream} or other {@code Object}.160* The destination is assumed to be ready to accept data, and will161* not be closed at the end of each write. This allows distributed162* imaging applications to transmit a series of images over a163* single network connection. If {@code output} is164* {@code null}, any currently set output will be removed.165*166* <p> If {@code output} is an167* {@code ImageOutputStream}, calls to the168* {@code write}, {@code writeToSequence}, and169* {@code prepareWriteEmpty}/{@code endWriteEmpty}170* methods will preserve the existing contents of the stream.171* Other write methods, such as {@code writeInsert},172* {@code replaceStreamMetadata},173* {@code replaceImageMetadata}, {@code replacePixels},174* {@code prepareInsertEmpty}/{@code endInsertEmpty},175* and {@code endWriteSequence}, require the full contents176* of the stream to be readable and writable, and may alter any177* portion of the stream.178*179* <p> Use of a general {@code Object} other than an180* {@code ImageOutputStream} is intended for writers that181* interact directly with an output device or imaging protocol.182* The set of legal classes is advertised by the writer's service183* provider's {@code getOutputTypes} method; most writers184* will return a single-element array containing only185* {@code ImageOutputStream.class} to indicate that they186* accept only an {@code ImageOutputStream}.187*188* <p> The default implementation sets the {@code output}189* instance variable to the value of {@code output} after190* checking {@code output} against the set of classes191* advertised by the originating provider, if there is one.192*193* @param output the {@code ImageOutputStream} or other194* {@code Object} to use for future writing.195*196* @exception IllegalArgumentException if {@code output} is197* not an instance of one of the classes returned by the198* originating service provider's {@code getOutputTypes}199* method.200*201* @see #getOutput202*/203public void setOutput(Object output) {204if (output != null) {205ImageWriterSpi provider = getOriginatingProvider();206if (provider != null) {207Class<?>[] classes = provider.getOutputTypes();208boolean found = false;209for (int i = 0; i < classes.length; i++) {210if (classes[i].isInstance(output)) {211found = true;212break;213}214}215if (!found) {216throw new IllegalArgumentException("Illegal output type!");217}218}219}220221this.output = output;222}223224/**225* Returns the {@code ImageOutputStream} or other226* {@code Object} set by the most recent call to the227* {@code setOutput} method. If no destination has been228* set, {@code null} is returned.229*230* <p> The default implementation returns the value of the231* {@code output} instance variable.232*233* @return the {@code Object} that was specified using234* {@code setOutput}, or {@code null}.235*236* @see #setOutput237*/238public Object getOutput() {239return output;240}241242// Localization243244/**245* Returns an array of {@code Locale}s that may be used to246* localize warning listeners and compression settings. A return247* value of {@code null} indicates that localization is not248* supported.249*250* <p> The default implementation returns a clone of the251* {@code availableLocales} instance variable if it is252* non-{@code null}, or else returns {@code null}.253*254* @return an array of {@code Locale}s that may be used as255* arguments to {@code setLocale}, or {@code null}.256*/257public Locale[] getAvailableLocales() {258return (availableLocales == null) ?259null : availableLocales.clone();260}261262/**263* Sets the current {@code Locale} of this264* {@code ImageWriter} to the given value. A value of265* {@code null} removes any previous setting, and indicates266* that the writer should localize as it sees fit.267*268* <p> The default implementation checks {@code locale}269* against the values returned by270* {@code getAvailableLocales}, and sets the271* {@code locale} instance variable if it is found. If272* {@code locale} is {@code null}, the instance variable273* is set to {@code null} without any checking.274*275* @param locale the desired {@code Locale}, or276* {@code null}.277*278* @exception IllegalArgumentException if {@code locale} is279* non-{@code null} but is not one of the values returned by280* {@code getAvailableLocales}.281*282* @see #getLocale283*/284public void setLocale(Locale locale) {285if (locale != null) {286Locale[] locales = getAvailableLocales();287boolean found = false;288if (locales != null) {289for (int i = 0; i < locales.length; i++) {290if (locale.equals(locales[i])) {291found = true;292break;293}294}295}296if (!found) {297throw new IllegalArgumentException("Invalid locale!");298}299}300this.locale = locale;301}302303/**304* Returns the currently set {@code Locale}, or305* {@code null} if none has been set.306*307* <p> The default implementation returns the value of the308* {@code locale} instance variable.309*310* @return the current {@code Locale}, or {@code null}.311*312* @see #setLocale313*/314public Locale getLocale() {315return locale;316}317318// Write params319320/**321* Returns a new {@code ImageWriteParam} object of the322* appropriate type for this file format containing default323* values, that is, those values that would be used324* if no {@code ImageWriteParam} object were specified. This325* is useful as a starting point for tweaking just a few parameters326* and otherwise leaving the default settings alone.327*328* <p> The default implementation constructs and returns a new329* {@code ImageWriteParam} object that does not allow tiling,330* progressive encoding, or compression, and that will be331* localized for the current {@code Locale} (<i>i.e.</i>,332* what you would get by calling333* {@code new ImageWriteParam(getLocale())}.334*335* <p> Individual plug-ins may return an instance of336* {@code ImageWriteParam} with additional optional features337* enabled, or they may return an instance of a plug-in specific338* subclass of {@code ImageWriteParam}.339*340* @return a new {@code ImageWriteParam} object containing341* default values.342*/343public ImageWriteParam getDefaultWriteParam() {344return new ImageWriteParam(getLocale());345}346347// Metadata348349/**350* Returns an {@code IIOMetadata} object containing default351* values for encoding a stream of images. The contents of the352* object may be manipulated using either the XML tree structure353* returned by the {@code IIOMetadata.getAsTree} method, an354* {@code IIOMetadataController} object, or via plug-in355* specific interfaces, and the resulting data supplied to one of356* the {@code write} methods that take a stream metadata357* parameter.358*359* <p> An optional {@code ImageWriteParam} may be supplied360* for cases where it may affect the structure of the stream361* metadata.362*363* <p> If the supplied {@code ImageWriteParam} contains364* optional setting values not supported by this writer (<i>e.g.</i>365* progressive encoding or any format-specific settings), they366* will be ignored.367*368* <p> Writers that do not make use of stream metadata369* (<i>e.g.</i>, writers for single-image formats) should return370* {@code null}.371*372* @param param an {@code ImageWriteParam} that will be used to373* encode the image, or {@code null}.374*375* @return an {@code IIOMetadata} object.376*/377public abstract IIOMetadata378getDefaultStreamMetadata(ImageWriteParam param);379380/**381* Returns an {@code IIOMetadata} object containing default382* values for encoding an image of the given type. The contents383* of the object may be manipulated using either the XML tree384* structure returned by the {@code IIOMetadata.getAsTree}385* method, an {@code IIOMetadataController} object, or via386* plug-in specific interfaces, and the resulting data supplied to387* one of the {@code write} methods that take a stream388* metadata parameter.389*390* <p> An optional {@code ImageWriteParam} may be supplied391* for cases where it may affect the structure of the image392* metadata.393*394* <p> If the supplied {@code ImageWriteParam} contains395* optional setting values not supported by this writer (<i>e.g.</i>396* progressive encoding or any format-specific settings), they397* will be ignored.398*399* @param imageType an {@code ImageTypeSpecifier} indicating the400* format of the image to be written later.401* @param param an {@code ImageWriteParam} that will be used to402* encode the image, or {@code null}.403*404* @return an {@code IIOMetadata} object.405*/406public abstract IIOMetadata407getDefaultImageMetadata(ImageTypeSpecifier imageType,408ImageWriteParam param);409410// comment inherited411public abstract IIOMetadata convertStreamMetadata(IIOMetadata inData,412ImageWriteParam param);413414// comment inherited415public abstract IIOMetadata416convertImageMetadata(IIOMetadata inData,417ImageTypeSpecifier imageType,418ImageWriteParam param);419420// Thumbnails421422/**423* Returns the number of thumbnails supported by the format being424* written, given the image type and any additional write425* parameters and metadata objects that will be used during426* encoding. A return value of {@code -1} indicates that427* insufficient information is available.428*429* <p> An {@code ImageWriteParam} may optionally be supplied430* for cases where it may affect thumbnail handling.431*432* <p> If the supplied {@code ImageWriteParam} contains433* optional setting values not supported by this writer (<i>e.g.</i>434* progressive encoding or any format-specific settings), they435* will be ignored.436*437* <p> The default implementation returns 0.438*439* @param imageType an {@code ImageTypeSpecifier} indicating440* the type of image to be written, or {@code null}.441* @param param the {@code ImageWriteParam} that will be used for442* writing, or {@code null}.443* @param streamMetadata an {@code IIOMetadata} object that will444* be used for writing, or {@code null}.445* @param imageMetadata an {@code IIOMetadata} object that will446* be used for writing, or {@code null}.447*448* @return the number of thumbnails that may be written given the449* supplied parameters, or {@code -1} if insufficient450* information is available.451*/452public int getNumThumbnailsSupported(ImageTypeSpecifier imageType,453ImageWriteParam param,454IIOMetadata streamMetadata,455IIOMetadata imageMetadata) {456return 0;457}458459/**460* Returns an array of {@code Dimension}s indicating the461* legal size ranges for thumbnail images as they will be encoded462* in the output file or stream. This information is merely463* advisory; the writer will resize any supplied thumbnails as464* necessary.465*466* <p> The information is returned as a set of pairs; the first467* element of a pair contains an (inclusive) minimum width and468* height, and the second element contains an (inclusive) maximum469* width and height. Together, each pair defines a valid range of470* sizes. To specify a fixed size, the same width and height will471* appear for both elements. A return value of {@code null}472* indicates that the size is arbitrary or unknown.473*474* <p> An {@code ImageWriteParam} may optionally be supplied475* for cases where it may affect thumbnail handling.476*477* <p> If the supplied {@code ImageWriteParam} contains478* optional setting values not supported by this writer (<i>e.g.</i>479* progressive encoding or any format-specific settings), they480* will be ignored.481*482* <p> The default implementation returns {@code null}.483*484* @param imageType an {@code ImageTypeSpecifier} indicating the485* type of image to be written, or {@code null}.486* @param param the {@code ImageWriteParam} that will be used for487* writing, or {@code null}.488* @param streamMetadata an {@code IIOMetadata} object that will489* be used for writing, or {@code null}.490* @param imageMetadata an {@code IIOMetadata} object that will491* be used for writing, or {@code null}.492*493* @return an array of {@code Dimension}s with an even length494* of at least two, or {@code null}.495*/496public Dimension[] getPreferredThumbnailSizes(ImageTypeSpecifier imageType,497ImageWriteParam param,498IIOMetadata streamMetadata,499IIOMetadata imageMetadata) {500return null;501}502503/**504* Returns {@code true} if the methods that take an505* {@code IIOImage} parameter are capable of dealing with a506* {@code Raster} (as opposed to {@code RenderedImage})507* source image. If this method returns {@code false}, then508* those methods will throw an509* {@code UnsupportedOperationException} if supplied with an510* {@code IIOImage} containing a {@code Raster}.511*512* <p> The default implementation returns {@code false}.513*514* @return {@code true} if {@code Raster} sources are515* supported.516*/517public boolean canWriteRasters() {518return false;519}520521/**522* Appends a complete image stream containing a single image and523* associated stream and image metadata and thumbnails to the524* output. Any necessary header information is included. If the525* output is an {@code ImageOutputStream}, its existing526* contents prior to the current seek position are not affected,527* and need not be readable or writable.528*529* <p> The output must have been set beforehand using the530* {@code setOutput} method.531*532* <p> Stream metadata may optionally be supplied; if it is533* {@code null}, default stream metadata will be used.534*535* <p> If {@code canWriteRasters} returns {@code true},536* the {@code IIOImage} may contain a {@code Raster}537* source. Otherwise, it must contain a538* {@code RenderedImage} source.539*540* <p> The supplied thumbnails will be resized if needed, and any541* thumbnails in excess of the supported number will be ignored.542* If the format requires additional thumbnails that are not543* provided, the writer should generate them internally.544*545* <p> An {@code ImageWriteParam} may546* optionally be supplied to control the writing process. If547* {@code param} is {@code null}, a default write param548* will be used.549*550* <p> If the supplied {@code ImageWriteParam} contains551* optional setting values not supported by this writer (<i>e.g.</i>552* progressive encoding or any format-specific settings), they553* will be ignored.554*555* @param streamMetadata an {@code IIOMetadata} object representing556* stream metadata, or {@code null} to use default values.557* @param image an {@code IIOImage} object containing an558* image, thumbnails, and metadata to be written.559* @param param an {@code ImageWriteParam}, or560* {@code null} to use a default561* {@code ImageWriteParam}.562*563* @exception IllegalStateException if the output has not564* been set.565* @exception UnsupportedOperationException if {@code image}566* contains a {@code Raster} and {@code canWriteRasters}567* returns {@code false}.568* @exception IllegalArgumentException if {@code image} is569* {@code null}.570* @exception IOException if an error occurs during writing.571*/572public abstract void write(IIOMetadata streamMetadata,573IIOImage image,574ImageWriteParam param) throws IOException;575576/**577* Appends a complete image stream containing a single image with578* default metadata and thumbnails to the output. This method is579* a shorthand for {@code write(null, image, null)}.580*581* @param image an {@code IIOImage} object containing an582* image, thumbnails, and metadata to be written.583*584* @exception IllegalStateException if the output has not585* been set.586* @exception IllegalArgumentException if {@code image} is587* {@code null}.588* @exception UnsupportedOperationException if {@code image}589* contains a {@code Raster} and {@code canWriteRasters}590* returns {@code false}.591* @exception IOException if an error occurs during writing.592*/593public void write(IIOImage image) throws IOException {594write(null, image, null);595}596597/**598* Appends a complete image stream consisting of a single image599* with default metadata and thumbnails to the output. This600* method is a shorthand for601* {@code write(null, new IIOImage(image, null, null), null)}.602*603* @param image a {@code RenderedImage} to be written.604*605* @exception IllegalStateException if the output has not606* been set.607* @exception IllegalArgumentException if {@code image} is608* {@code null}.609* @exception IOException if an error occurs during writing.610*/611public void write(RenderedImage image) throws IOException {612write(null, new IIOImage(image, null, null), null);613}614615// Check that the output has been set, then throw an616// UnsupportedOperationException.617private void unsupported() {618if (getOutput() == null) {619throw new IllegalStateException("getOutput() == null!");620}621throw new UnsupportedOperationException("Unsupported write variant!");622}623624// Sequence writes625626/**627* Returns {@code true} if the writer is able to append an628* image to an image stream that already contains header629* information and possibly prior images.630*631* <p> If {@code canWriteSequence} returns {@code false},632* {@code writeToSequence} and {@code endWriteSequence}633* will throw an {@code UnsupportedOperationException}.634*635* <p> The default implementation returns {@code false}.636*637* @return {@code true} if images may be appended sequentially.638*/639public boolean canWriteSequence() {640return false;641}642643/**644* Prepares a stream to accept a series of subsequent645* {@code writeToSequence} calls, using the provided stream646* metadata object. The metadata will be written to the stream if647* it should precede the image data. If the argument is {@code null},648* default stream metadata is used.649*650* <p> If the output is an {@code ImageOutputStream}, the existing651* contents of the output prior to the current seek position are652* flushed, and need not be readable or writable. If the format653* requires that {@code endWriteSequence} be able to rewind to654* patch up the header information, such as for a sequence of images655* in a single TIFF file, then the metadata written by this method656* must remain in a writable portion of the stream. Other formats657* may flush the stream after this method and after each image.658*659* <p> If {@code canWriteSequence} returns {@code false},660* this method will throw an661* {@code UnsupportedOperationException}.662*663* <p> The output must have been set beforehand using either664* the {@code setOutput} method.665*666* <p> The default implementation throws an667* {@code IllegalStateException} if the output is668* {@code null}, and otherwise throws an669* {@code UnsupportedOperationException}.670*671* @param streamMetadata A stream metadata object, or {@code null}.672*673* @exception IllegalStateException if the output has not674* been set.675* @exception UnsupportedOperationException if676* {@code canWriteSequence} returns {@code false}.677* @exception IOException if an error occurs writing the stream678* metadata.679*/680public void prepareWriteSequence(IIOMetadata streamMetadata)681throws IOException {682unsupported();683}684685/**686* Appends a single image and possibly associated metadata and687* thumbnails, to the output. If the output is an688* {@code ImageOutputStream}, the existing contents of the689* output prior to the current seek position may be flushed, and690* need not be readable or writable, unless the plug-in needs to691* be able to patch up the header information when692* {@code endWriteSequence} is called (<i>e.g.</i> TIFF).693*694* <p> If {@code canWriteSequence} returns {@code false},695* this method will throw an696* {@code UnsupportedOperationException}.697*698* <p> The output must have been set beforehand using699* the {@code setOutput} method.700*701* <p> {@code prepareWriteSequence} must have been called702* beforehand, or an {@code IllegalStateException} is thrown.703*704* <p> If {@code canWriteRasters} returns {@code true},705* the {@code IIOImage} may contain a {@code Raster}706* source. Otherwise, it must contain a707* {@code RenderedImage} source.708*709* <p> The supplied thumbnails will be resized if needed, and any710* thumbnails in excess of the supported number will be ignored.711* If the format requires additional thumbnails that are not712* provided, the writer will generate them internally.713*714* <p> An {@code ImageWriteParam} may optionally be supplied715* to control the writing process. If {@code param} is716* {@code null}, a default write param will be used.717*718* <p> If the supplied {@code ImageWriteParam} contains719* optional setting values not supported by this writer (<i>e.g.</i>720* progressive encoding or any format-specific settings), they721* will be ignored.722*723* <p> The default implementation throws an724* {@code IllegalStateException} if the output is725* {@code null}, and otherwise throws an726* {@code UnsupportedOperationException}.727*728* @param image an {@code IIOImage} object containing an729* image, thumbnails, and metadata to be written.730* @param param an {@code ImageWriteParam}, or731* {@code null} to use a default732* {@code ImageWriteParam}.733*734* @exception IllegalStateException if the output has not735* been set, or {@code prepareWriteSequence} has not been called.736* @exception UnsupportedOperationException if737* {@code canWriteSequence} returns {@code false}.738* @exception IllegalArgumentException if {@code image} is739* {@code null}.740* @exception UnsupportedOperationException if {@code image}741* contains a {@code Raster} and {@code canWriteRasters}742* returns {@code false}.743* @exception IOException if an error occurs during writing.744*/745public void writeToSequence(IIOImage image, ImageWriteParam param)746throws IOException {747unsupported();748}749750/**751* Completes the writing of a sequence of images begun with752* {@code prepareWriteSequence}. Any stream metadata that753* should come at the end of the sequence of images is written out,754* and any header information at the beginning of the sequence is755* patched up if necessary. If the output is an756* {@code ImageOutputStream}, data through the stream metadata757* at the end of the sequence are flushed and need not be readable758* or writable.759*760* <p> If {@code canWriteSequence} returns {@code false},761* this method will throw an762* {@code UnsupportedOperationException}.763*764* <p> The default implementation throws an765* {@code IllegalStateException} if the output is766* {@code null}, and otherwise throws an767* {@code UnsupportedOperationException}.768*769* @exception IllegalStateException if the output has not770* been set, or {@code prepareWriteSequence} has not been called.771* @exception UnsupportedOperationException if772* {@code canWriteSequence} returns {@code false}.773* @exception IOException if an error occurs during writing.774*/775public void endWriteSequence() throws IOException {776unsupported();777}778779// Metadata replacement780781/**782* Returns {@code true} if it is possible to replace the783* stream metadata already present in the output.784*785* <p> The default implementation throws an786* {@code IllegalStateException} if the output is787* {@code null}, and otherwise returns {@code false}.788*789* @return {@code true} if replacement of stream metadata is790* allowed.791*792* @exception IllegalStateException if the output has not793* been set.794* @exception IOException if an I/O error occurs during the query.795*/796public boolean canReplaceStreamMetadata() throws IOException {797if (getOutput() == null) {798throw new IllegalStateException("getOutput() == null!");799}800return false;801}802803/**804* Replaces the stream metadata in the output with new805* information. If the output is an806* {@code ImageOutputStream}, the prior contents of the807* stream are examined and possibly edited to make room for the808* new data. All of the prior contents of the output must be809* available for reading and writing.810*811* <p> If {@code canReplaceStreamMetadata} returns812* {@code false}, an813* {@code UnsupportedOperationException} will be thrown.814*815* <p> The default implementation throws an816* {@code IllegalStateException} if the output is817* {@code null}, and otherwise throws an818* {@code UnsupportedOperationException}.819*820* @param streamMetadata an {@code IIOMetadata} object representing821* stream metadata, or {@code null} to use default values.822*823* @exception IllegalStateException if the output has not824* been set.825* @exception UnsupportedOperationException if the826* {@code canReplaceStreamMetadata} returns827* {@code false}. modes do not include828* @exception IOException if an error occurs during writing.829*/830public void replaceStreamMetadata(IIOMetadata streamMetadata)831throws IOException {832unsupported();833}834835/**836* Returns {@code true} if it is possible to replace the837* image metadata associated with an existing image with index838* {@code imageIndex}. If this method returns839* {@code false}, a call to840* {@code replaceImageMetadata(imageIndex)} will throw an841* {@code UnsupportedOperationException}.842*843* <p> A writer that does not support any image metadata844* replacement may return {@code false} without performing845* bounds checking on the index.846*847* <p> The default implementation throws an848* {@code IllegalStateException} if the output is849* {@code null}, and otherwise returns {@code false}850* without checking the value of {@code imageIndex}.851*852* @param imageIndex the index of the image whose metadata is to853* be replaced.854*855* @return {@code true} if the image metadata of the given856* image can be replaced.857*858* @exception IllegalStateException if the output has not859* been set.860* @exception IndexOutOfBoundsException if the writer supports861* image metadata replacement in general, but862* {@code imageIndex} is less than 0 or greater than the863* largest available index.864* @exception IOException if an I/O error occurs during the query.865*/866public boolean canReplaceImageMetadata(int imageIndex)867throws IOException {868if (getOutput() == null) {869throw new IllegalStateException("getOutput() == null!");870}871return false;872}873874/**875* Replaces the image metadata associated with an existing image.876*877* <p> If {@code canReplaceImageMetadata(imageIndex)} returns878* {@code false}, an879* {@code UnsupportedOperationException} will be thrown.880*881* <p> The default implementation throws an882* {@code IllegalStateException} if the output is883* {@code null}, and otherwise throws an884* {@code UnsupportedOperationException}.885*886* @param imageIndex the index of the image whose metadata is to887* be replaced.888* @param imageMetadata an {@code IIOMetadata} object889* representing image metadata, or {@code null}.890*891* @exception IllegalStateException if the output has not been892* set.893* @exception UnsupportedOperationException if894* {@code canReplaceImageMetadata} returns895* {@code false}.896* @exception IndexOutOfBoundsException if {@code imageIndex}897* is less than 0 or greater than the largest available index.898* @exception IOException if an error occurs during writing.899*/900public void replaceImageMetadata(int imageIndex,901IIOMetadata imageMetadata)902throws IOException {903unsupported();904}905906// Image insertion907908/**909* Returns {@code true} if the writer supports the insertion910* of a new image at the given index. Existing images with911* indices greater than or equal to the insertion index will have912* their indices increased by 1. A value for913* {@code imageIndex} of {@code -1} may be used to914* signify an index one larger than the current largest index.915*916* <p> A writer that does not support any image insertion may917* return {@code false} without performing bounds checking on918* the index.919*920* <p> The default implementation throws an921* {@code IllegalStateException} if the output is922* {@code null}, and otherwise returns {@code false}923* without checking the value of {@code imageIndex}.924*925* @param imageIndex the index at which the image is to be926* inserted.927*928* @return {@code true} if an image may be inserted at the929* given index.930*931* @exception IllegalStateException if the output has not932* been set.933* @exception IndexOutOfBoundsException if the writer supports934* image insertion in general, but {@code imageIndex} is less935* than -1 or greater than the largest available index.936* @exception IOException if an I/O error occurs during the query.937*/938public boolean canInsertImage(int imageIndex) throws IOException {939if (getOutput() == null) {940throw new IllegalStateException("getOutput() == null!");941}942return false;943}944945/**946* Inserts a new image into an existing image stream. Existing947* images with an index greater than {@code imageIndex} are948* preserved, and their indices are each increased by 1. A value949* for {@code imageIndex} of -1 may be used to signify an950* index one larger than the previous largest index; that is, it951* will cause the image to be logically appended to the end of the952* sequence. If the output is an {@code ImageOutputStream},953* the entirety of the stream must be both readable and writeable.954*955* <p> If {@code canInsertImage(imageIndex)} returns956* {@code false}, an957* {@code UnsupportedOperationException} will be thrown.958*959* <p> An {@code ImageWriteParam} may optionally be supplied960* to control the writing process. If {@code param} is961* {@code null}, a default write param will be used.962*963* <p> If the supplied {@code ImageWriteParam} contains964* optional setting values not supported by this writer (<i>e.g.</i>965* progressive encoding or any format-specific settings), they966* will be ignored.967*968* <p> The default implementation throws an969* {@code IllegalStateException} if the output is970* {@code null}, and otherwise throws an971* {@code UnsupportedOperationException}.972*973* @param imageIndex the index at which to write the image.974* @param image an {@code IIOImage} object containing an975* image, thumbnails, and metadata to be written.976* @param param an {@code ImageWriteParam}, or977* {@code null} to use a default978* {@code ImageWriteParam}.979*980* @exception IllegalStateException if the output has not981* been set.982* @exception UnsupportedOperationException if983* {@code canInsertImage(imageIndex)} returns {@code false}.984* @exception IllegalArgumentException if {@code image} is985* {@code null}.986* @exception IndexOutOfBoundsException if {@code imageIndex}987* is less than -1 or greater than the largest available index.988* @exception UnsupportedOperationException if {@code image}989* contains a {@code Raster} and {@code canWriteRasters}990* returns {@code false}.991* @exception IOException if an error occurs during writing.992*/993public void writeInsert(int imageIndex,994IIOImage image,995ImageWriteParam param) throws IOException {996unsupported();997}998999// Image removal10001001/**1002* Returns {@code true} if the writer supports the removal1003* of an existing image at the given index. Existing images with1004* indices greater than the insertion index will have1005* their indices decreased by 1.1006*1007* <p> A writer that does not support any image removal may1008* return {@code false} without performing bounds checking on1009* the index.1010*1011* <p> The default implementation throws an1012* {@code IllegalStateException} if the output is1013* {@code null}, and otherwise returns {@code false}1014* without checking the value of {@code imageIndex}.1015*1016* @param imageIndex the index of the image to be removed.1017*1018* @return {@code true} if it is possible to remove the given1019* image.1020*1021* @exception IllegalStateException if the output has not1022* been set.1023* @exception IndexOutOfBoundsException if the writer supports1024* image removal in general, but {@code imageIndex} is less1025* than 0 or greater than the largest available index.1026* @exception IOException if an I/O error occurs during the1027* query.1028*/1029public boolean canRemoveImage(int imageIndex) throws IOException {1030if (getOutput() == null) {1031throw new IllegalStateException("getOutput() == null!");1032}1033return false;1034}10351036/**1037* Removes an image from the stream.1038*1039* <p> If {@code canRemoveImage(imageIndex)} returns false,1040* an {@code UnsupportedOperationException} will be thrown.1041*1042* <p> The removal may or may not cause a reduction in the actual1043* file size.1044*1045* <p> The default implementation throws an1046* {@code IllegalStateException} if the output is1047* {@code null}, and otherwise throws an1048* {@code UnsupportedOperationException}.1049*1050* @param imageIndex the index of the image to be removed.1051*1052* @exception IllegalStateException if the output has not1053* been set.1054* @exception UnsupportedOperationException if1055* {@code canRemoveImage(imageIndex)} returns {@code false}.1056* @exception IndexOutOfBoundsException if {@code imageIndex}1057* is less than 0 or greater than the largest available index.1058* @exception IOException if an I/O error occurs during the1059* removal.1060*/1061public void removeImage(int imageIndex) throws IOException {1062unsupported();1063}10641065// Empty images10661067/**1068* Returns {@code true} if the writer supports the writing of1069* a complete image stream consisting of a single image with1070* undefined pixel values and associated metadata and thumbnails1071* to the output. The pixel values may be defined by future1072* calls to the {@code replacePixels} methods. If the output1073* is an {@code ImageOutputStream}, its existing contents1074* prior to the current seek position are not affected, and need1075* not be readable or writable.1076*1077* <p> The default implementation throws an1078* {@code IllegalStateException} if the output is1079* {@code null}, and otherwise returns {@code false}.1080*1081* @return {@code true} if the writing of complete image1082* stream with contents to be defined later is supported.1083*1084* @exception IllegalStateException if the output has not been1085* set.1086* @exception IOException if an I/O error occurs during the1087* query.1088*/1089public boolean canWriteEmpty() throws IOException {1090if (getOutput() == null) {1091throw new IllegalStateException("getOutput() == null!");1092}1093return false;1094}10951096/**1097* Begins the writing of a complete image stream, consisting of a1098* single image with undefined pixel values and associated1099* metadata and thumbnails, to the output. The pixel values will1100* be defined by future calls to the {@code replacePixels}1101* methods. If the output is an {@code ImageOutputStream},1102* its existing contents prior to the current seek position are1103* not affected, and need not be readable or writable.1104*1105* <p> The writing is not complete until a call to1106* {@code endWriteEmpty} occurs. Calls to1107* {@code prepareReplacePixels}, {@code replacePixels},1108* and {@code endReplacePixels} may occur between calls to1109* {@code prepareWriteEmpty} and {@code endWriteEmpty}.1110* However, calls to {@code prepareWriteEmpty} cannot be1111* nested, and calls to {@code prepareWriteEmpty} and1112* {@code prepareInsertEmpty} may not be interspersed.1113*1114* <p> If {@code canWriteEmpty} returns {@code false},1115* an {@code UnsupportedOperationException} will be thrown.1116*1117* <p> An {@code ImageWriteParam} may optionally be supplied1118* to control the writing process. If {@code param} is1119* {@code null}, a default write param will be used.1120*1121* <p> If the supplied {@code ImageWriteParam} contains1122* optional setting values not supported by this writer (<i>e.g.</i>1123* progressive encoding or any format-specific settings), they1124* will be ignored.1125*1126* <p> The default implementation throws an1127* {@code IllegalStateException} if the output is1128* {@code null}, and otherwise throws an1129* {@code UnsupportedOperationException}.1130*1131* @param streamMetadata an {@code IIOMetadata} object representing1132* stream metadata, or {@code null} to use default values.1133* @param imageType an {@code ImageTypeSpecifier} describing1134* the layout of the image.1135* @param width the width of the image.1136* @param height the height of the image.1137* @param imageMetadata an {@code IIOMetadata} object1138* representing image metadata, or {@code null}.1139* @param thumbnails a {@code List} of1140* {@code BufferedImage} thumbnails for this image, or1141* {@code null}.1142* @param param an {@code ImageWriteParam}, or1143* {@code null} to use a default1144* {@code ImageWriteParam}.1145*1146* @exception IllegalStateException if the output has not1147* been set.1148* @exception UnsupportedOperationException if1149* {@code canWriteEmpty} returns {@code false}.1150* @exception IllegalStateException if a previous call to1151* {@code prepareWriteEmpty} has been made without a1152* corresponding call to {@code endWriteEmpty}.1153* @exception IllegalStateException if a previous call to1154* {@code prepareInsertEmpty} has been made without a1155* corresponding call to {@code endInsertEmpty}.1156* @exception IllegalArgumentException if {@code imageType}1157* is {@code null} or {@code thumbnails} contains1158* {@code null} references or objects other than1159* {@code BufferedImage}s.1160* @exception IllegalArgumentException if width or height are less1161* than 1.1162* @exception IOException if an I/O error occurs during writing.1163*/1164public void prepareWriteEmpty(IIOMetadata streamMetadata,1165ImageTypeSpecifier imageType,1166int width, int height,1167IIOMetadata imageMetadata,1168List<? extends BufferedImage> thumbnails,1169ImageWriteParam param) throws IOException {1170unsupported();1171}11721173/**1174* Completes the writing of a new image that was begun with a1175* prior call to {@code prepareWriteEmpty}.1176*1177* <p> If {@code canWriteEmpty()} returns {@code false},1178* an {@code UnsupportedOperationException} will be thrown.1179*1180* <p> The default implementation throws an1181* {@code IllegalStateException} if the output is1182* {@code null}, and otherwise throws an1183* {@code UnsupportedOperationException}.1184*1185* @exception IllegalStateException if the output has not1186* been set.1187* @exception UnsupportedOperationException if1188* {@code canWriteEmpty(imageIndex)} returns1189* {@code false}.1190* @exception IllegalStateException if a previous call to1191* {@code prepareWriteEmpty} without a corresponding call to1192* {@code endWriteEmpty} has not been made.1193* @exception IllegalStateException if a previous call to1194* {@code prepareInsertEmpty} without a corresponding call to1195* {@code endInsertEmpty} has been made.1196* @exception IllegalStateException if a call to1197* {@code prepareReiplacePixels} has been made without a1198* matching call to {@code endReplacePixels}.1199* @exception IOException if an I/O error occurs during writing.1200*/1201public void endWriteEmpty() throws IOException {1202if (getOutput() == null) {1203throw new IllegalStateException("getOutput() == null!");1204}1205throw new IllegalStateException("No call to prepareWriteEmpty!");1206}12071208/**1209* Returns {@code true} if the writer supports the insertion1210* of a new, empty image at the given index. The pixel values of1211* the image are undefined, and may be specified in pieces using1212* the {@code replacePixels} methods. Existing images with1213* indices greater than or equal to the insertion index will have1214* their indices increased by 1. A value for1215* {@code imageIndex} of {@code -1} may be used to1216* signify an index one larger than the current largest index.1217*1218* <p> A writer that does not support insertion of empty images1219* may return {@code false} without performing bounds1220* checking on the index.1221*1222* <p> The default implementation throws an1223* {@code IllegalStateException} if the output is1224* {@code null}, and otherwise returns {@code false}1225* without checking the value of {@code imageIndex}.1226*1227* @param imageIndex the index at which the image is to be1228* inserted.1229*1230* @return {@code true} if an empty image may be inserted at1231* the given index.1232*1233* @exception IllegalStateException if the output has not been1234* set.1235* @exception IndexOutOfBoundsException if the writer supports1236* empty image insertion in general, but {@code imageIndex}1237* is less than -1 or greater than the largest available index.1238* @exception IOException if an I/O error occurs during the1239* query.1240*/1241public boolean canInsertEmpty(int imageIndex) throws IOException {1242if (getOutput() == null) {1243throw new IllegalStateException("getOutput() == null!");1244}1245return false;1246}12471248/**1249* Begins the insertion of a new image with undefined pixel values1250* into an existing image stream. Existing images with an index1251* greater than {@code imageIndex} are preserved, and their1252* indices are each increased by 1. A value for1253* {@code imageIndex} of -1 may be used to signify an index1254* one larger than the previous largest index; that is, it will1255* cause the image to be logically appended to the end of the1256* sequence. If the output is an {@code ImageOutputStream},1257* the entirety of the stream must be both readable and writeable.1258*1259* <p> The image contents may be1260* supplied later using the {@code replacePixels} method.1261* The insertion is not complete until a call to1262* {@code endInsertEmpty} occurs. Calls to1263* {@code prepareReplacePixels}, {@code replacePixels},1264* and {@code endReplacePixels} may occur between calls to1265* {@code prepareInsertEmpty} and1266* {@code endInsertEmpty}. However, calls to1267* {@code prepareInsertEmpty} cannot be nested, and calls to1268* {@code prepareWriteEmpty} and1269* {@code prepareInsertEmpty} may not be interspersed.1270*1271* <p> If {@code canInsertEmpty(imageIndex)} returns1272* {@code false}, an1273* {@code UnsupportedOperationException} will be thrown.1274*1275* <p> An {@code ImageWriteParam} may optionally be supplied1276* to control the writing process. If {@code param} is1277* {@code null}, a default write param will be used.1278*1279* <p> If the supplied {@code ImageWriteParam} contains1280* optional setting values not supported by this writer (<i>e.g.</i>1281* progressive encoding or any format-specific settings), they1282* will be ignored.1283*1284* <p> The default implementation throws an1285* {@code IllegalStateException} if the output is1286* {@code null}, and otherwise throws an1287* {@code UnsupportedOperationException}.1288*1289* @param imageIndex the index at which to write the image.1290* @param imageType an {@code ImageTypeSpecifier} describing1291* the layout of the image.1292* @param width the width of the image.1293* @param height the height of the image.1294* @param imageMetadata an {@code IIOMetadata} object1295* representing image metadata, or {@code null}.1296* @param thumbnails a {@code List} of1297* {@code BufferedImage} thumbnails for this image, or1298* {@code null}.1299* @param param an {@code ImageWriteParam}, or1300* {@code null} to use a default1301* {@code ImageWriteParam}.1302*1303* @exception IllegalStateException if the output has not1304* been set.1305* @exception UnsupportedOperationException if1306* {@code canInsertEmpty(imageIndex)} returns1307* {@code false}.1308* @exception IndexOutOfBoundsException if {@code imageIndex}1309* is less than -1 or greater than the largest available index.1310* @exception IllegalStateException if a previous call to1311* {@code prepareInsertEmpty} has been made without a1312* corresponding call to {@code endInsertEmpty}.1313* @exception IllegalStateException if a previous call to1314* {@code prepareWriteEmpty} has been made without a1315* corresponding call to {@code endWriteEmpty}.1316* @exception IllegalArgumentException if {@code imageType}1317* is {@code null} or {@code thumbnails} contains1318* {@code null} references or objects other than1319* {@code BufferedImage}s.1320* @exception IllegalArgumentException if width or height are less1321* than 1.1322* @exception IOException if an I/O error occurs during writing.1323*/1324public void prepareInsertEmpty(int imageIndex,1325ImageTypeSpecifier imageType,1326int width, int height,1327IIOMetadata imageMetadata,1328List<? extends BufferedImage> thumbnails,1329ImageWriteParam param) throws IOException {1330unsupported();1331}13321333/**1334* Completes the insertion of a new image that was begun with a1335* prior call to {@code prepareInsertEmpty}.1336*1337* <p> The default implementation throws an1338* {@code IllegalStateException} if the output is1339* {@code null}, and otherwise throws an1340* {@code UnsupportedOperationException}.1341*1342* @exception IllegalStateException if the output has not1343* been set.1344* @exception UnsupportedOperationException if1345* {@code canInsertEmpty(imageIndex)} returns1346* {@code false}.1347* @exception IllegalStateException if a previous call to1348* {@code prepareInsertEmpty} without a corresponding call to1349* {@code endInsertEmpty} has not been made.1350* @exception IllegalStateException if a previous call to1351* {@code prepareWriteEmpty} without a corresponding call to1352* {@code endWriteEmpty} has been made.1353* @exception IllegalStateException if a call to1354* {@code prepareReplacePixels} has been made without a1355* matching call to {@code endReplacePixels}.1356* @exception IOException if an I/O error occurs during writing.1357*/1358public void endInsertEmpty() throws IOException {1359unsupported();1360}13611362// Pixel replacement13631364/**1365* Returns {@code true} if the writer allows pixels of the1366* given image to be replaced using the {@code replacePixels}1367* methods.1368*1369* <p> A writer that does not support any pixel replacement may1370* return {@code false} without performing bounds checking on1371* the index.1372*1373* <p> The default implementation throws an1374* {@code IllegalStateException} if the output is1375* {@code null}, and otherwise returns {@code false}1376* without checking the value of {@code imageIndex}.1377*1378* @param imageIndex the index of the image whose pixels are to be1379* replaced.1380*1381* @return {@code true} if the pixels of the given1382* image can be replaced.1383*1384* @exception IllegalStateException if the output has not been1385* set.1386* @exception IndexOutOfBoundsException if the writer supports1387* pixel replacement in general, but {@code imageIndex} is1388* less than 0 or greater than the largest available index.1389* @exception IOException if an I/O error occurs during the query.1390*/1391public boolean canReplacePixels(int imageIndex) throws IOException {1392if (getOutput() == null) {1393throw new IllegalStateException("getOutput() == null!");1394}1395return false;1396}13971398/**1399* Prepares the writer to handle a series of calls to the1400* {@code replacePixels} methods. The affected pixel area1401* will be clipped against the supplied1402*1403* <p> If {@code canReplacePixels} returns1404* {@code false}, and1405* {@code UnsupportedOperationException} will be thrown.1406*1407* <p> The default implementation throws an1408* {@code IllegalStateException} if the output is1409* {@code null}, and otherwise throws an1410* {@code UnsupportedOperationException}.1411*1412* @param imageIndex the index of the image whose pixels are to be1413* replaced.1414* @param region a {@code Rectangle} that will be used to clip1415* future pixel regions.1416*1417* @exception IllegalStateException if the output has not1418* been set.1419* @exception UnsupportedOperationException if1420* {@code canReplacePixels(imageIndex)} returns1421* {@code false}.1422* @exception IndexOutOfBoundsException if {@code imageIndex}1423* is less than 0 or greater than the largest available index.1424* @exception IllegalStateException if there is a previous call to1425* {@code prepareReplacePixels} without a matching call to1426* {@code endReplacePixels} (<i>i.e.</i>, nesting is not1427* allowed).1428* @exception IllegalArgumentException if {@code region} is1429* {@code null} or has a width or height less than 1.1430* @exception IOException if an I/O error occurs during the1431* preparation.1432*/1433public void prepareReplacePixels(int imageIndex,1434Rectangle region) throws IOException {1435unsupported();1436}14371438/**1439* Replaces a portion of an image already present in the output1440* with a portion of the given image. The image data must match,1441* or be convertible to, the image layout of the existing image.1442*1443* <p> The destination region is specified in the1444* {@code param} argument, and will be clipped to the image1445* boundaries and the region supplied to1446* {@code prepareReplacePixels}. At least one pixel of the1447* source must not be clipped, or an exception is thrown.1448*1449* <p> An {@code ImageWriteParam} may optionally be supplied1450* to control the writing process. If {@code param} is1451* {@code null}, a default write param will be used.1452*1453* <p> If the supplied {@code ImageWriteParam} contains1454* optional setting values not supported by this writer (<i>e.g.</i>1455* progressive encoding or any format-specific settings), they1456* will be ignored.1457*1458* <p> This method may only be called after a call to1459* {@code prepareReplacePixels}, or else an1460* {@code IllegalStateException} will be thrown.1461*1462* <p> The default implementation throws an1463* {@code IllegalStateException} if the output is1464* {@code null}, and otherwise throws an1465* {@code UnsupportedOperationException}.1466*1467* @param image a {@code RenderedImage} containing source1468* pixels.1469* @param param an {@code ImageWriteParam}, or1470* {@code null} to use a default1471* {@code ImageWriteParam}.1472*1473* @exception IllegalStateException if the output has not1474* been set.1475* @exception UnsupportedOperationException if1476* {@code canReplacePixels(imageIndex)} returns1477* {@code false}.1478* @exception IllegalStateException if there is no previous call to1479* {@code prepareReplacePixels} without a matching call to1480* {@code endReplacePixels}.1481* @exception IllegalArgumentException if any of the following are true:1482* <ul>1483* <li> {@code image} is {@code null}.1484* <li> the intersected region does not contain at least one pixel.1485* <li> the layout of {@code image} does not match, or this1486* writer cannot convert it to, the existing image layout.1487* </ul>1488* @exception IOException if an I/O error occurs during writing.1489*/1490public void replacePixels(RenderedImage image, ImageWriteParam param)1491throws IOException {1492unsupported();1493}14941495/**1496* Replaces a portion of an image already present in the output1497* with a portion of the given {@code Raster}. The image1498* data must match, or be convertible to, the image layout of the1499* existing image.1500*1501* <p> An {@code ImageWriteParam} may optionally be supplied1502* to control the writing process. If {@code param} is1503* {@code null}, a default write param will be used.1504*1505* <p> The destination region is specified in the1506* {@code param} argument, and will be clipped to the image1507* boundaries and the region supplied to1508* {@code prepareReplacePixels}. At least one pixel of the1509* source must not be clipped, or an exception is thrown.1510*1511* <p> If the supplied {@code ImageWriteParam} contains1512* optional setting values not supported by this writer (<i>e.g.</i>1513* progressive encoding or any format-specific settings), they1514* will be ignored.1515*1516* <p> This method may only be called after a call to1517* {@code prepareReplacePixels}, or else an1518* {@code IllegalStateException} will be thrown.1519*1520* <p> The default implementation throws an1521* {@code IllegalStateException} if the output is1522* {@code null}, and otherwise throws an1523* {@code UnsupportedOperationException}.1524*1525* @param raster a {@code Raster} containing source1526* pixels.1527* @param param an {@code ImageWriteParam}, or1528* {@code null} to use a default1529* {@code ImageWriteParam}.1530*1531* @exception IllegalStateException if the output has not1532* been set.1533* @exception UnsupportedOperationException if1534* {@code canReplacePixels(imageIndex)} returns1535* {@code false}.1536* @exception IllegalStateException if there is no previous call to1537* {@code prepareReplacePixels} without a matching call to1538* {@code endReplacePixels}.1539* @exception UnsupportedOperationException if1540* {@code canWriteRasters} returns {@code false}.1541* @exception IllegalArgumentException if any of the following are true:1542* <ul>1543* <li> {@code raster} is {@code null}.1544* <li> the intersected region does not contain at least one pixel.1545* <li> the layout of {@code raster} does not match, or this1546* writer cannot convert it to, the existing image layout.1547* </ul>1548* @exception IOException if an I/O error occurs during writing.1549*/1550public void replacePixels(Raster raster, ImageWriteParam param)1551throws IOException {1552unsupported();1553}15541555/**1556* Terminates a sequence of calls to {@code replacePixels}.1557*1558* <p> If {@code canReplacePixels} returns1559* {@code false}, and1560* {@code UnsupportedOperationException} will be thrown.1561*1562* <p> The default implementation throws an1563* {@code IllegalStateException} if the output is1564* {@code null}, and otherwise throws an1565* {@code UnsupportedOperationException}.1566*1567* @exception IllegalStateException if the output has not1568* been set.1569* @exception UnsupportedOperationException if1570* {@code canReplacePixels(imageIndex)} returns1571* {@code false}.1572* @exception IllegalStateException if there is no previous call1573* to {@code prepareReplacePixels} without a matching call to1574* {@code endReplacePixels}.1575* @exception IOException if an I/O error occurs during writing.1576*/1577public void endReplacePixels() throws IOException {1578unsupported();1579}15801581// Abort15821583/**1584* Requests that any current write operation be aborted. The1585* contents of the output following the abort will be undefined.1586*1587* <p> Writers should call {@code clearAbortRequest} at the1588* beginning of each write operation, and poll the value of1589* {@code abortRequested} regularly during the write.1590*/1591public synchronized void abort() {1592this.abortFlag = true;1593}15941595/**1596* Returns {@code true} if a request to abort the current1597* write operation has been made since the writer was instantiated or1598* {@code clearAbortRequest} was called.1599*1600* @return {@code true} if the current write operation should1601* be aborted.1602*1603* @see #abort1604* @see #clearAbortRequest1605*/1606protected synchronized boolean abortRequested() {1607return this.abortFlag;1608}16091610/**1611* Clears any previous abort request. After this method has been1612* called, {@code abortRequested} will return1613* {@code false}.1614*1615* @see #abort1616* @see #abortRequested1617*/1618protected synchronized void clearAbortRequest() {1619this.abortFlag = false;1620}16211622// Listeners16231624/**1625* Adds an {@code IIOWriteWarningListener} to the list of1626* registered warning listeners. If {@code listener} is1627* {@code null}, no exception will be thrown and no action1628* will be taken. Messages sent to the given listener will be1629* localized, if possible, to match the current1630* {@code Locale}. If no {@code Locale} has been set,1631* warning messages may be localized as the writer sees fit.1632*1633* @param listener an {@code IIOWriteWarningListener} to be1634* registered.1635*1636* @see #removeIIOWriteWarningListener1637*/1638public void addIIOWriteWarningListener(IIOWriteWarningListener listener) {1639if (listener == null) {1640return;1641}1642warningListeners = ImageReader.addToList(warningListeners, listener);1643warningLocales = ImageReader.addToList(warningLocales, getLocale());1644}16451646/**1647* Removes an {@code IIOWriteWarningListener} from the list1648* of registered warning listeners. If the listener was not1649* previously registered, or if {@code listener} is1650* {@code null}, no exception will be thrown and no action1651* will be taken.1652*1653* @param listener an {@code IIOWriteWarningListener} to be1654* deregistered.1655*1656* @see #addIIOWriteWarningListener1657*/1658public1659void removeIIOWriteWarningListener(IIOWriteWarningListener listener) {1660if (listener == null || warningListeners == null) {1661return;1662}1663int index = warningListeners.indexOf(listener);1664if (index != -1) {1665warningListeners.remove(index);1666warningLocales.remove(index);1667if (warningListeners.size() == 0) {1668warningListeners = null;1669warningLocales = null;1670}1671}1672}16731674/**1675* Removes all currently registered1676* {@code IIOWriteWarningListener} objects.1677*1678* <p> The default implementation sets the1679* {@code warningListeners} and {@code warningLocales}1680* instance variables to {@code null}.1681*/1682public void removeAllIIOWriteWarningListeners() {1683this.warningListeners = null;1684this.warningLocales = null;1685}16861687/**1688* Adds an {@code IIOWriteProgressListener} to the list of1689* registered progress listeners. If {@code listener} is1690* {@code null}, no exception will be thrown and no action1691* will be taken.1692*1693* @param listener an {@code IIOWriteProgressListener} to be1694* registered.1695*1696* @see #removeIIOWriteProgressListener1697*/1698public void1699addIIOWriteProgressListener(IIOWriteProgressListener listener) {1700if (listener == null) {1701return;1702}1703progressListeners = ImageReader.addToList(progressListeners, listener);1704}17051706/**1707* Removes an {@code IIOWriteProgressListener} from the list1708* of registered progress listeners. If the listener was not1709* previously registered, or if {@code listener} is1710* {@code null}, no exception will be thrown and no action1711* will be taken.1712*1713* @param listener an {@code IIOWriteProgressListener} to be1714* deregistered.1715*1716* @see #addIIOWriteProgressListener1717*/1718public void1719removeIIOWriteProgressListener(IIOWriteProgressListener listener) {1720if (listener == null || progressListeners == null) {1721return;1722}1723progressListeners =1724ImageReader.removeFromList(progressListeners, listener);1725}17261727/**1728* Removes all currently registered1729* {@code IIOWriteProgressListener} objects.1730*1731* <p> The default implementation sets the1732* {@code progressListeners} instance variable to1733* {@code null}.1734*/1735public void removeAllIIOWriteProgressListeners() {1736this.progressListeners = null;1737}17381739/**1740* Broadcasts the start of an image write to all registered1741* {@code IIOWriteProgressListener}s by calling their1742* {@code imageStarted} method. Subclasses may use this1743* method as a convenience.1744*1745* @param imageIndex the index of the image about to be written.1746*/1747protected void processImageStarted(int imageIndex) {1748if (progressListeners == null) {1749return;1750}1751int numListeners = progressListeners.size();1752for (int i = 0; i < numListeners; i++) {1753IIOWriteProgressListener listener =1754progressListeners.get(i);1755listener.imageStarted(this, imageIndex);1756}1757}17581759/**1760* Broadcasts the current percentage of image completion to all1761* registered {@code IIOWriteProgressListener}s by calling1762* their {@code imageProgress} method. Subclasses may use1763* this method as a convenience.1764*1765* @param percentageDone the current percentage of completion,1766* as a {@code float}.1767*/1768protected void processImageProgress(float percentageDone) {1769if (progressListeners == null) {1770return;1771}1772int numListeners = progressListeners.size();1773for (int i = 0; i < numListeners; i++) {1774IIOWriteProgressListener listener =1775progressListeners.get(i);1776listener.imageProgress(this, percentageDone);1777}1778}17791780/**1781* Broadcasts the completion of an image write to all registered1782* {@code IIOWriteProgressListener}s by calling their1783* {@code imageComplete} method. Subclasses may use this1784* method as a convenience.1785*/1786protected void processImageComplete() {1787if (progressListeners == null) {1788return;1789}1790int numListeners = progressListeners.size();1791for (int i = 0; i < numListeners; i++) {1792IIOWriteProgressListener listener =1793progressListeners.get(i);1794listener.imageComplete(this);1795}1796}17971798/**1799* Broadcasts the start of a thumbnail write to all registered1800* {@code IIOWriteProgressListener}s by calling their1801* {@code thumbnailStarted} method. Subclasses may use this1802* method as a convenience.1803*1804* @param imageIndex the index of the image associated with the1805* thumbnail.1806* @param thumbnailIndex the index of the thumbnail.1807*/1808protected void processThumbnailStarted(int imageIndex,1809int thumbnailIndex) {1810if (progressListeners == null) {1811return;1812}1813int numListeners = progressListeners.size();1814for (int i = 0; i < numListeners; i++) {1815IIOWriteProgressListener listener =1816progressListeners.get(i);1817listener.thumbnailStarted(this, imageIndex, thumbnailIndex);1818}1819}18201821/**1822* Broadcasts the current percentage of thumbnail completion to1823* all registered {@code IIOWriteProgressListener}s by calling1824* their {@code thumbnailProgress} method. Subclasses may1825* use this method as a convenience.1826*1827* @param percentageDone the current percentage of completion,1828* as a {@code float}.1829*/1830protected void processThumbnailProgress(float percentageDone) {1831if (progressListeners == null) {1832return;1833}1834int numListeners = progressListeners.size();1835for (int i = 0; i < numListeners; i++) {1836IIOWriteProgressListener listener =1837progressListeners.get(i);1838listener.thumbnailProgress(this, percentageDone);1839}1840}18411842/**1843* Broadcasts the completion of a thumbnail write to all registered1844* {@code IIOWriteProgressListener}s by calling their1845* {@code thumbnailComplete} method. Subclasses may use this1846* method as a convenience.1847*/1848protected void processThumbnailComplete() {1849if (progressListeners == null) {1850return;1851}1852int numListeners = progressListeners.size();1853for (int i = 0; i < numListeners; i++) {1854IIOWriteProgressListener listener =1855progressListeners.get(i);1856listener.thumbnailComplete(this);1857}1858}18591860/**1861* Broadcasts that the write has been aborted to all registered1862* {@code IIOWriteProgressListener}s by calling their1863* {@code writeAborted} method. Subclasses may use this1864* method as a convenience.1865*/1866protected void processWriteAborted() {1867if (progressListeners == null) {1868return;1869}1870int numListeners = progressListeners.size();1871for (int i = 0; i < numListeners; i++) {1872IIOWriteProgressListener listener =1873progressListeners.get(i);1874listener.writeAborted(this);1875}1876}18771878/**1879* Broadcasts a warning message to all registered1880* {@code IIOWriteWarningListener}s by calling their1881* {@code warningOccurred} method. Subclasses may use this1882* method as a convenience.1883*1884* @param imageIndex the index of the image on which the warning1885* occurred.1886* @param warning the warning message.1887*1888* @exception IllegalArgumentException if {@code warning}1889* is {@code null}.1890*/1891protected void processWarningOccurred(int imageIndex,1892String warning) {1893if (warningListeners == null) {1894return;1895}1896if (warning == null) {1897throw new IllegalArgumentException("warning == null!");1898}1899int numListeners = warningListeners.size();1900for (int i = 0; i < numListeners; i++) {1901IIOWriteWarningListener listener =1902warningListeners.get(i);19031904listener.warningOccurred(this, imageIndex, warning);1905}1906}19071908/**1909* Broadcasts a localized warning message to all registered1910* {@code IIOWriteWarningListener}s by calling their1911* {@code warningOccurred} method with a string taken1912* from a {@code ResourceBundle}. Subclasses may use this1913* method as a convenience.1914*1915* @param imageIndex the index of the image on which the warning1916* occurred.1917* @param baseName the base name of a set of1918* {@code ResourceBundle}s containing localized warning1919* messages.1920* @param keyword the keyword used to index the warning message1921* within the set of {@code ResourceBundle}s.1922*1923* @exception IllegalArgumentException if {@code baseName}1924* is {@code null}.1925* @exception IllegalArgumentException if {@code keyword}1926* is {@code null}.1927* @exception IllegalArgumentException if no appropriate1928* {@code ResourceBundle} may be located.1929* @exception IllegalArgumentException if the named resource is1930* not found in the located {@code ResourceBundle}.1931* @exception IllegalArgumentException if the object retrieved1932* from the {@code ResourceBundle} is not a1933* {@code String}.1934*/1935protected void processWarningOccurred(int imageIndex,1936String baseName,1937String keyword) {1938if (warningListeners == null) {1939return;1940}1941if (baseName == null) {1942throw new IllegalArgumentException("baseName == null!");1943}1944if (keyword == null) {1945throw new IllegalArgumentException("keyword == null!");1946}1947int numListeners = warningListeners.size();1948for (int i = 0; i < numListeners; i++) {1949IIOWriteWarningListener listener =1950warningListeners.get(i);1951Locale locale = warningLocales.get(i);1952if (locale == null) {1953locale = Locale.getDefault();1954}19551956/*1957* Only the plugin knows the messages that are provided, so we1958* can always locate the resource bundles from the same loader1959* as that for the plugin code itself.1960*/1961ResourceBundle bundle = null;1962try {1963bundle = ResourceBundle.getBundle(baseName, locale, this.getClass().getModule());1964} catch (MissingResourceException mre) {1965throw new IllegalArgumentException("Bundle not found!", mre);1966}19671968String warning = null;1969try {1970warning = bundle.getString(keyword);1971} catch (ClassCastException cce) {1972throw new IllegalArgumentException("Resource is not a String!", cce);1973} catch (MissingResourceException mre) {1974throw new IllegalArgumentException("Resource is missing!", mre);1975}19761977listener.warningOccurred(this, imageIndex, warning);1978}1979}19801981// State management19821983/**1984* Restores the {@code ImageWriter} to its initial state.1985*1986* <p> The default implementation calls1987* {@code setOutput(null)}, {@code setLocale(null)},1988* {@code removeAllIIOWriteWarningListeners()},1989* {@code removeAllIIOWriteProgressListeners()}, and1990* {@code clearAbortRequest}.1991*/1992public void reset() {1993setOutput(null);1994setLocale(null);1995removeAllIIOWriteWarningListeners();1996removeAllIIOWriteProgressListeners();1997clearAbortRequest();1998}19992000/**2001* Allows any resources held by this object to be released. The2002* result of calling any other method (other than2003* {@code finalize}) subsequent to a call to this method2004* is undefined.2005*2006* <p>It is important for applications to call this method when they2007* know they will no longer be using this {@code ImageWriter}.2008* Otherwise, the writer may continue to hold on to resources2009* indefinitely.2010*2011* <p>The default implementation of this method in the superclass does2012* nothing. Subclass implementations should ensure that all resources,2013* especially native resources, are released.2014*/2015public void dispose() {2016}2017}201820192020