Path: blob/master/src/java.desktop/share/classes/javax/imageio/stream/ImageOutputStream.java
41153 views
/*1* Copyright (c) 2000, 2018, 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.stream;2627import java.io.DataOutput;28import java.io.IOException;2930/**31* A seekable output stream interface for use by32* {@code ImageWriter}s. Various output destinations, such as33* {@code OutputStream}s and {@code File}s, as well as34* future fast I/O destinations may be "wrapped" by a suitable35* implementation of this interface for use by the Image I/O API.36*37* <p> Unlike a standard {@code OutputStream}, ImageOutputStream38* extends its counterpart, {@code ImageInputStream}. Thus it is39* possible to read from the stream as it is being written. The same40* seek and flush positions apply to both reading and writing, although41* the semantics for dealing with a non-zero bit offset before a byte-aligned42* write are necessarily different from the semantics for dealing with43* a non-zero bit offset before a byte-aligned read. When reading bytes,44* any bit offset is set to 0 before the read; when writing bytes, a45* non-zero bit offset causes the remaining bits in the byte to be written46* as 0s. The byte-aligned write then starts at the next byte position.47*48* @see ImageInputStream49*50*/51public interface ImageOutputStream extends ImageInputStream, DataOutput {5253/**54* Writes a single byte to the stream at the current position.55* The 24 high-order bits of {@code b} are ignored.56*57* <p> If the bit offset within the stream is non-zero, the58* remainder of the current byte is padded with 0s59* and written out first. The bit offset will be 0 after the60* write. Implementers can use the61* {@link ImageOutputStreamImpl#flushBits flushBits}62* method of {@link ImageOutputStreamImpl ImageOutputStreamImpl}63* to guarantee this.64*65* @param b an {@code int} whose lower 8 bits are to be66* written.67*68* @exception IOException if an I/O error occurs.69*/70void write(int b) throws IOException;7172/**73* Writes a sequence of bytes to the stream at the current74* position. If {@code b.length} is 0, nothing is written.75* The byte {@code b[0]} is written first, then the byte76* {@code b[1]}, and so on.77*78* <p> If the bit offset within the stream is non-zero, the79* remainder of the current byte is padded with 0s80* and written out first. The bit offset will be 0 after the81* write.82*83* @param b an array of {@code byte}s to be written.84*85* @exception NullPointerException if {@code b} is86* {@code null}.87* @exception IOException if an I/O error occurs.88*/89void write(byte[] b) throws IOException;9091/**92* Writes a sequence of bytes to the stream at the current93* position. If {@code len} is 0, nothing is written.94* The byte {@code b[off]} is written first, then the byte95* {@code b[off + 1]}, and so on.96*97* <p> If the bit offset within the stream is non-zero, the98* remainder of the current byte is padded with 0s99* and written out first. The bit offset will be 0 after the100* write. Implementers can use the101* {@link ImageOutputStreamImpl#flushBits flushBits}102* method of {@link ImageOutputStreamImpl ImageOutputStreamImpl}103* to guarantee this.104*105* @param b an array of {@code byte}s to be written.106* @param off the start offset in the data.107* @param len the number of {@code byte}s to write.108*109* @exception IndexOutOfBoundsException if {@code off} is110* negative, {@code len} is negative, or {@code off + len}111* is greater than {@code b.length}.112* @exception NullPointerException if {@code b} is113* {@code null}.114* @exception IOException if an I/O error occurs.115*/116void write(byte[] b, int off, int len) throws IOException;117118/**119* Writes a {@code boolean} value to the stream. If120* {@code v} is true, the value {@code (byte)1} is121* written; if {@code v} is false, the value122* {@code (byte)0} is written.123*124* <p> If the bit offset within the stream is non-zero, the125* remainder of the current byte is padded with 0s126* and written out first. The bit offset will be 0 after the127* write.128*129* @param v the {@code boolean} to be written.130*131* @exception IOException if an I/O error occurs.132*/133void writeBoolean(boolean v) throws IOException;134135/**136* Writes the 8 low-order bits of {@code v} to the137* stream. The 24 high-order bits of {@code v} are ignored.138* (This means that {@code writeByte} does exactly the same139* thing as {@code write} for an integer argument.)140*141* <p> If the bit offset within the stream is non-zero, the142* remainder of the current byte is padded with 0s143* and written out first. The bit offset will be 0 after the144* write.145*146* @param v an {@code int} containing the byte value to be147* written.148*149* @exception IOException if an I/O error occurs.150*/151void writeByte(int v) throws IOException;152153/**154* Writes the 16 low-order bits of {@code v} to the155* stream. The 16 high-order bits of {@code v} are ignored.156* If the stream uses network byte order, the bytes written, in157* order, will be:158*159* <pre>160* (byte)((v >> 8) & 0xff)161* (byte)(v & 0xff)162* </pre>163*164* Otherwise, the bytes written will be:165*166* <pre>167* (byte)(v & 0xff)168* (byte)((v >> 8) & 0xff)169* </pre>170*171* <p> If the bit offset within the stream is non-zero, the172* remainder of the current byte is padded with 0s173* and written out first. The bit offset will be 0 after the174* write.175*176* @param v an {@code int} containing the short value to be177* written.178*179* @exception IOException if an I/O error occurs.180*/181void writeShort(int v) throws IOException;182183/**184* This method is a synonym for {@link #writeShort writeShort}.185*186* @param v an {@code int} containing the char (unsigned187* short) value to be written.188*189* @exception IOException if an I/O error occurs.190*191* @see #writeShort(int)192*/193void writeChar(int v) throws IOException;194195/**196* Writes the 32 bits of {@code v} to the stream. If the197* stream uses network byte order, the bytes written, in order,198* will be:199*200* <pre>201* (byte)((v >> 24) & 0xff)202* (byte)((v >> 16) & 0xff)203* (byte)((v >> 8) & 0xff)204* (byte)(v & 0xff)205* </pre>206*207* Otheriwse, the bytes written will be:208*209* <pre>210* (byte)(v & 0xff)211* (byte)((v >> 8) & 0xff)212* (byte)((v >> 16) & 0xff)213* (byte)((v >> 24) & 0xff)214* </pre>215*216* <p> If the bit offset within the stream is non-zero, the217* remainder of the current byte is padded with 0s218* and written out first. The bit offset will be 0 after the219* write.220*221* @param v an {@code int} containing the value to be222* written.223*224* @exception IOException if an I/O error occurs.225*/226void writeInt(int v) throws IOException;227228/**229* Writes the 64 bits of {@code v} to the stream. If the230* stream uses network byte order, the bytes written, in order,231* will be:232*233* <pre>234* (byte)((v >> 56) & 0xff)235* (byte)((v >> 48) & 0xff)236* (byte)((v >> 40) & 0xff)237* (byte)((v >> 32) & 0xff)238* (byte)((v >> 24) & 0xff)239* (byte)((v >> 16) & 0xff)240* (byte)((v >> 8) & 0xff)241* (byte)(v & 0xff)242* </pre>243*244* Otherwise, the bytes written will be:245*246* <pre>247* (byte)(v & 0xff)248* (byte)((v >> 8) & 0xff)249* (byte)((v >> 16) & 0xff)250* (byte)((v >> 24) & 0xff)251* (byte)((v >> 32) & 0xff)252* (byte)((v >> 40) & 0xff)253* (byte)((v >> 48) & 0xff)254* (byte)((v >> 56) & 0xff)255* </pre>256*257* <p> If the bit offset within the stream is non-zero, the258* remainder of the current byte is padded with 0s259* and written out first. The bit offset will be 0 after the260* write.261*262* @param v a {@code long} containing the value to be263* written.264*265* @exception IOException if an I/O error occurs.266*/267void writeLong(long v) throws IOException;268269/**270* Writes a {@code float} value, which is comprised of four271* bytes, to the output stream. It does this as if it first272* converts this {@code float} value to an {@code int}273* in exactly the manner of the {@code Float.floatToIntBits}274* method and then writes the int value in exactly the manner of275* the {@code writeInt} method.276*277* <p> If the bit offset within the stream is non-zero, the278* remainder of the current byte is padded with 0s279* and written out first. The bit offset will be 0 after the280* write.281*282* @param v a {@code float} containing the value to be283* written.284*285* @exception IOException if an I/O error occurs.286*/287void writeFloat(float v) throws IOException;288289/**290* Writes a {@code double} value, which is comprised of four291* bytes, to the output stream. It does this as if it first292* converts this {@code double} value to a {@code long}293* in exactly the manner of the294* {@code Double.doubleToLongBits} method and then writes the295* long value in exactly the manner of the {@code writeLong}296* method.297*298* <p> If the bit offset within the stream is non-zero, the299* remainder of the current byte is padded with 0s300* and written out first. The bit offset will be 0 after the301* write.302*303* @param v a {@code double} containing the value to be304* written.305*306* @exception IOException if an I/O error occurs.307*/308void writeDouble(double v) throws IOException;309310/**311* Writes a string to the output stream. For every character in312* the string {@code s}, taken in order, one byte is written313* to the output stream. If {@code s} is {@code null}, a314* {@code NullPointerException} is thrown.315*316* <p> If {@code s.length} is zero, then no bytes are317* written. Otherwise, the character {@code s[0]} is written318* first, then {@code s[1]}, and so on; the last character319* written is {@code s[s.length-1]}. For each character, one320* byte is written, the low-order byte, in exactly the manner of321* the {@code writeByte} method. The high-order eight bits of322* each character in the string are ignored.323*324* <p> If the bit offset within the stream is non-zero, the325* remainder of the current byte is padded with 0s326* and written out first. The bit offset will be 0 after the327* write.328*329* @param s a {@code String} containing the value to be330* written.331*332* @exception NullPointerException if {@code s} is333* {@code null}.334* @exception IOException if an I/O error occurs.335*/336void writeBytes(String s) throws IOException;337338/**339* Writes a string to the output stream. For every character in340* the string {@code s}, taken in order, two bytes are341* written to the output stream, ordered according to the current342* byte order setting. If network byte order is being used, the343* high-order byte is written first; the order is reversed344* otherwise. If {@code s} is {@code null}, a345* {@code NullPointerException} is thrown.346*347* <p> If {@code s.length} is zero, then no bytes are348* written. Otherwise, the character {@code s[0]} is written349* first, then {@code s[1]}, and so on; the last character350* written is {@code s[s.length-1]}.351*352* <p> If the bit offset within the stream is non-zero, the353* remainder of the current byte is padded with 0s354* and written out first. The bit offset will be 0 after the355* write.356*357* @param s a {@code String} containing the value to be358* written.359*360* @exception NullPointerException if {@code s} is361* {@code null}.362* @exception IOException if an I/O error occurs.363*/364void writeChars(String s) throws IOException;365366/**367* Writes two bytes of length information to the output stream in368* network byte order, followed by the369* <a href="../../../../java.base/java/io/DataInput.html#modified-utf-8">370* modified UTF-8</a>371* representation of every character in the string {@code s}.372* If {@code s} is {@code null}, a373* {@code NullPointerException} is thrown. Each character in374* the string {@code s} is converted to a group of one, two,375* or three bytes, depending on the value of the character.376*377* <p> If a character {@code c} is in the range378* <code>\u0001</code> through <code>\u007f</code>, it is379* represented by one byte:380*381* <pre>382* (byte)c383* </pre>384*385* <p> If a character {@code c} is <code>\u0000</code> or386* is in the range <code>\u0080</code> through387* <code>\u07ff</code>, then it is represented by two bytes,388* to be written in the order shown:389*390* <pre><code>391* (byte)(0xc0 | (0x1f & (c >> 6)))392* (byte)(0x80 | (0x3f & c))393* </code></pre>394*395* <p> If a character {@code c} is in the range396* <code>\u0800</code> through {@code uffff}, then it is397* represented by three bytes, to be written in the order shown:398*399* <pre><code>400* (byte)(0xe0 | (0x0f & (c >> 12)))401* (byte)(0x80 | (0x3f & (c >> 6)))402* (byte)(0x80 | (0x3f & c))403* </code></pre>404*405* <p> First, the total number of bytes needed to represent all406* the characters of {@code s} is calculated. If this number407* is larger than {@code 65535}, then a408* {@code UTFDataFormatException} is thrown. Otherwise, this409* length is written to the output stream in exactly the manner of410* the {@code writeShort} method; after this, the one-, two-,411* or three-byte representation of each character in the string412* {@code s} is written.413*414* <p> The current byte order setting is ignored.415*416* <p> If the bit offset within the stream is non-zero, the417* remainder of the current byte is padded with 0s418* and written out first. The bit offset will be 0 after the419* write.420*421* <p><strong>Note:</strong> This method should not be used in422* the implementation of image formats that use standard UTF-8,423* because the modified UTF-8 used here is incompatible with424* standard UTF-8.425*426* @param s a {@code String} containing the value to be427* written.428*429* @exception NullPointerException if {@code s} is430* {@code null}.431* @exception java.io.UTFDataFormatException if the modified UTF-8432* representation of {@code s} requires more than 65536 bytes.433* @exception IOException if an I/O error occurs.434*/435void writeUTF(String s) throws IOException;436437/**438* Writes a sequence of shorts to the stream at the current439* position. If {@code len} is 0, nothing is written.440* The short {@code s[off]} is written first, then the short441* {@code s[off + 1]}, and so on. The byte order of the442* stream is used to determine the order in which the individual443* bytes are written.444*445* <p> If the bit offset within the stream is non-zero, the446* remainder of the current byte is padded with 0s447* and written out first. The bit offset will be 0 after the448* write.449*450* @param s an array of {@code short}s to be written.451* @param off the start offset in the data.452* @param len the number of {@code short}s to write.453*454* @exception IndexOutOfBoundsException if {@code off} is455* negative, {@code len} is negative, or {@code off + len}456* is greater than {@code s.length}.457* @exception NullPointerException if {@code s} is458* {@code null}.459* @exception IOException if an I/O error occurs.460*/461void writeShorts(short[] s, int off, int len) throws IOException;462463/**464* Writes a sequence of chars to the stream at the current465* position. If {@code len} is 0, nothing is written.466* The char {@code c[off]} is written first, then the char467* {@code c[off + 1]}, and so on. The byte order of the468* stream is used to determine the order in which the individual469* bytes are written.470*471* <p> If the bit offset within the stream is non-zero, the472* remainder of the current byte is padded with 0s473* and written out first. The bit offset will be 0 after the474* write.475*476* @param c an array of {@code char}s to be written.477* @param off the start offset in the data.478* @param len the number of {@code char}s to write.479*480* @exception IndexOutOfBoundsException if {@code off} is481* negative, {@code len} is negative, or {@code off + len}482* is greater than {@code c.length}.483* @exception NullPointerException if {@code c} is484* {@code null}.485* @exception IOException if an I/O error occurs.486*/487void writeChars(char[] c, int off, int len) throws IOException;488489/**490* Writes a sequence of ints to the stream at the current491* position. If {@code len} is 0, nothing is written.492* The int {@code i[off]} is written first, then the int493* {@code i[off + 1]}, and so on. The byte order of the494* stream is used to determine the order in which the individual495* bytes are written.496*497* <p> If the bit offset within the stream is non-zero, the498* remainder of the current byte is padded with 0s499* and written out first. The bit offset will be 0 after the500* write.501*502* @param i an array of {@code int}s to be written.503* @param off the start offset in the data.504* @param len the number of {@code int}s to write.505*506* @exception IndexOutOfBoundsException if {@code off} is507* negative, {@code len} is negative, or {@code off + len}508* is greater than {@code i.length}.509* @exception NullPointerException if {@code i} is510* {@code null}.511* @exception IOException if an I/O error occurs.512*/513void writeInts(int[] i, int off, int len) throws IOException;514515/**516* Writes a sequence of longs to the stream at the current517* position. If {@code len} is 0, nothing is written.518* The long {@code l[off]} is written first, then the long519* {@code l[off + 1]}, and so on. The byte order of the520* stream is used to determine the order in which the individual521* bytes are written.522*523* <p> If the bit offset within the stream is non-zero, the524* remainder of the current byte is padded with 0s525* and written out first. The bit offset will be 0 after the526* write.527*528* @param l an array of {@code long}s to be written.529* @param off the start offset in the data.530* @param len the number of {@code long}s to write.531*532* @exception IndexOutOfBoundsException if {@code off} is533* negative, {@code len} is negative, or {@code off + len}534* is greater than {@code l.length}.535* @exception NullPointerException if {@code l} is536* {@code null}.537* @exception IOException if an I/O error occurs.538*/539void writeLongs(long[] l, int off, int len) throws IOException;540541/**542* Writes a sequence of floats to the stream at the current543* position. If {@code len} is 0, nothing is written.544* The float {@code f[off]} is written first, then the float545* {@code f[off + 1]}, and so on. The byte order of the546* stream is used to determine the order in which the individual547* bytes are written.548*549* <p> If the bit offset within the stream is non-zero, the550* remainder of the current byte is padded with 0s551* and written out first. The bit offset will be 0 after the552* write.553*554* @param f an array of {@code float}s to be written.555* @param off the start offset in the data.556* @param len the number of {@code float}s to write.557*558* @exception IndexOutOfBoundsException if {@code off} is559* negative, {@code len} is negative, or {@code off + len}560* is greater than {@code f.length}.561* @exception NullPointerException if {@code f} is562* {@code null}.563* @exception IOException if an I/O error occurs.564*/565void writeFloats(float[] f, int off, int len) throws IOException;566567/**568* Writes a sequence of doubles to the stream at the current569* position. If {@code len} is 0, nothing is written.570* The double {@code d[off]} is written first, then the double571* {@code d[off + 1]}, and so on. The byte order of the572* stream is used to determine the order in which the individual573* bytes are written.574*575* <p> If the bit offset within the stream is non-zero, the576* remainder of the current byte is padded with 0s577* and written out first. The bit offset will be 0 after the578* write.579*580* @param d an array of {@code doubles}s to be written.581* @param off the start offset in the data.582* @param len the number of {@code double}s to write.583*584* @exception IndexOutOfBoundsException if {@code off} is585* negative, {@code len} is negative, or {@code off + len}586* is greater than {@code d.length}.587* @exception NullPointerException if {@code d} is588* {@code null}.589* @exception IOException if an I/O error occurs.590*/591void writeDoubles(double[] d, int off, int len) throws IOException;592593/**594* Writes a single bit, given by the least significant bit of the595* argument, to the stream at the current bit offset within the596* current byte position. The upper 31 bits of the argument are597* ignored. The given bit replaces the previous bit at that598* position. The bit offset is advanced by one and reduced modulo599* 8.600*601* <p> If any bits of a particular byte have never been set602* at the time the byte is flushed to the destination, those603* bits will be set to 0 automatically.604*605* @param bit an {@code int} whose least significant bit606* is to be written to the stream.607*608* @exception IOException if an I/O error occurs.609*/610void writeBit(int bit) throws IOException;611612/**613* Writes a sequence of bits, given by the {@code numBits}614* least significant bits of the {@code bits} argument in615* left-to-right order, to the stream at the current bit offset616* within the current byte position. The upper {@code 64 - numBits}617* bits of the argument are ignored. The bit618* offset is advanced by {@code numBits} and reduced modulo619* 8. Note that a bit offset of 0 always indicates the620* most-significant bit of the byte, and bytes of bits are written621* out in sequence as they are encountered. Thus bit writes are622* always effectively in network byte order. The actual stream623* byte order setting is ignored.624*625* <p> Bit data may be accumulated in memory indefinitely, until626* {@code flushBefore} is called. At that time, all bit data627* prior to the flushed position will be written.628*629* <p> If any bits of a particular byte have never been set630* at the time the byte is flushed to the destination, those631* bits will be set to 0 automatically.632*633* @param bits a {@code long} containing the bits to be634* written, starting with the bit in position {@code numBits - 1}635* down to the least significant bit.636*637* @param numBits an {@code int} between 0 and 64, inclusive.638*639* @exception IllegalArgumentException if {@code numBits} is640* not between 0 and 64, inclusive.641* @exception IOException if an I/O error occurs.642*/643void writeBits(long bits, int numBits) throws IOException;644645/**646* Flushes all data prior to the given position to the underlying647* destination, such as an {@code OutputStream} or648* {@code File}. Attempting to seek to the flushed portion649* of the stream will result in an650* {@code IndexOutOfBoundsException}.651*652* @param pos a {@code long} containing the length of the653* stream prefix that may be flushed to the destination.654*655* @exception IndexOutOfBoundsException if {@code pos} lies656* in the flushed portion of the stream or past the current stream657* position.658* @exception IOException if an I/O error occurs.659*/660void flushBefore(long pos) throws IOException;661}662663664