Path: blob/master/src/java.desktop/share/classes/javax/imageio/stream/ImageInputStream.java
41153 views
/*1* Copyright (c) 1999, 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.Closeable;28import java.io.DataInput;29import java.io.IOException;30import java.nio.ByteOrder;3132/**33* A seekable input stream interface for use by34* {@code ImageReader}s. Various input sources, such as35* {@code InputStream}s and {@code File}s,36* as well as future fast I/O sources may be "wrapped" by a suitable37* implementation of this interface for use by the Image I/O API.38*39* @see ImageInputStreamImpl40* @see FileImageInputStream41* @see FileCacheImageInputStream42* @see MemoryCacheImageInputStream43*44*/45public interface ImageInputStream extends DataInput, Closeable {4647/**48* Sets the desired byte order for future reads of data values49* from this stream. For example, the sequence of bytes '0x0150* 0x02 0x03 0x04' if read as a 4-byte integer would have the51* value '0x01020304' using network byte order and the value52* '0x04030201' under the reverse byte order.53*54* <p> The enumeration class {@code java.nio.ByteOrder} is55* used to specify the byte order. A value of56* {@code ByteOrder.BIG_ENDIAN} specifies so-called57* big-endian or network byte order, in which the high-order byte58* comes first. Motorola and Sparc processors store data in this59* format, while Intel processors store data in the reverse60* {@code ByteOrder.LITTLE_ENDIAN} order.61*62* <p> The byte order has no effect on the results returned from63* the {@code readBits} method (or the value written by64* {@code ImageOutputStream.writeBits}).65*66* @param byteOrder one of {@code ByteOrder.BIG_ENDIAN} or67* {@code java.nio.ByteOrder.LITTLE_ENDIAN}, indicating whether68* network byte order or its reverse will be used for future69* reads.70*71* @see java.nio.ByteOrder72* @see #getByteOrder73* @see #readBits(int)74*/75void setByteOrder(ByteOrder byteOrder);7677/**78* Returns the byte order with which data values will be read from79* this stream as an instance of the80* {@code java.nio.ByteOrder} enumeration.81*82* @return one of {@code ByteOrder.BIG_ENDIAN} or83* {@code ByteOrder.LITTLE_ENDIAN}, indicating which byte84* order is being used.85*86* @see java.nio.ByteOrder87* @see #setByteOrder88*/89ByteOrder getByteOrder();9091/**92* Reads a single byte from the stream and returns it as an93* integer between 0 and 255. If the end of the stream is94* reached, -1 is returned.95*96* <p> The bit offset within the stream is reset to zero before97* the read occurs.98*99* @return a byte value from the stream, as an int, or -1 to100* indicate EOF.101*102* @exception IOException if an I/O error occurs.103*/104int read() throws IOException;105106/**107* Reads up to {@code b.length} bytes from the stream, and108* stores them into {@code b} starting at index 0. The109* number of bytes read is returned. If no bytes can be read110* because the end of the stream has been reached, -1 is returned.111*112* <p> The bit offset within the stream is reset to zero before113* the read occurs.114*115* @param b an array of bytes to be written to.116*117* @return the number of bytes actually read, or {@code -1}118* to indicate EOF.119*120* @exception NullPointerException if {@code b} is121* {@code null}.122*123* @exception IOException if an I/O error occurs.124*/125int read(byte[] b) throws IOException;126127/**128* Reads up to {@code len} bytes from the stream, and stores129* them into {@code b} starting at index {@code off}.130* The number of bytes read is returned. If no bytes can be read131* because the end of the stream has been reached, {@code -1}132* is returned.133*134* <p> The bit offset within the stream is reset to zero before135* the read occurs.136*137* @param b an array of bytes to be written to.138* @param off the starting position within {@code b} to write to.139* @param len the maximum number of {@code byte}s to read.140*141* @return the number of bytes actually read, or {@code -1}142* to indicate EOF.143*144* @exception NullPointerException if {@code b} is145* {@code null}.146* @exception IndexOutOfBoundsException if {@code off} is147* negative, {@code len} is negative, or {@code off + len}148* is greater than {@code b.length}.149* @exception IOException if an I/O error occurs.150*/151int read(byte[] b, int off, int len) throws IOException;152153/**154* Reads up to {@code len} bytes from the stream, and155* modifies the supplied {@code IIOByteBuffer} to indicate156* the byte array, offset, and length where the data may be found.157* The caller should not attempt to modify the data found in the158* {@code IIOByteBuffer}.159*160* <p> The bit offset within the stream is reset to zero before161* the read occurs.162*163* @param buf an IIOByteBuffer object to be modified.164* @param len the maximum number of {@code byte}s to read.165*166* @exception IndexOutOfBoundsException if {@code len} is167* negative.168* @exception NullPointerException if {@code buf} is169* {@code null}.170*171* @exception IOException if an I/O error occurs.172*/173void readBytes(IIOByteBuffer buf, int len) throws IOException;174175/**176* Reads a byte from the stream and returns a {@code boolean}177* value of {@code true} if it is nonzero, {@code false}178* if it is zero.179*180* <p> The bit offset within the stream is reset to zero before181* the read occurs.182*183* @return a boolean value from the stream.184*185* @exception java.io.EOFException if the end of the stream is reached.186* @exception IOException if an I/O error occurs.187*/188boolean readBoolean() throws IOException;189190/**191* Reads a byte from the stream and returns it as a192* {@code byte} value. Byte values between {@code 0x00}193* and {@code 0x7f} represent integer values between194* {@code 0} and {@code 127}. Values between195* {@code 0x80} and {@code 0xff} represent negative196* values from {@code -128} to {@code /1}.197*198* <p> The bit offset within the stream is reset to zero before199* the read occurs.200*201* @return a signed byte value from the stream.202*203* @exception java.io.EOFException if the end of the stream is reached.204* @exception IOException if an I/O error occurs.205*/206byte readByte() throws IOException;207208/**209* Reads a byte from the stream, and (conceptually) converts it to210* an int, masks it with {@code 0xff} in order to strip off211* any sign-extension bits, and returns it as a {@code byte}212* value.213*214* <p> Thus, byte values between {@code 0x00} and215* {@code 0x7f} are simply returned as integer values between216* {@code 0} and {@code 127}. Values between217* {@code 0x80} and {@code 0xff}, which normally218* represent negative {@code byte} values, will be mapped into219* positive integers between {@code 128} and220* {@code 255}.221*222* <p> The bit offset within the stream is reset to zero before223* the read occurs.224*225* @return an unsigned byte value from the stream.226*227* @exception java.io.EOFException if the end of the stream is reached.228* @exception IOException if an I/O error occurs.229*/230int readUnsignedByte() throws IOException;231232/**233* Reads two bytes from the stream, and (conceptually)234* concatenates them according to the current byte order, and235* returns the result as a {@code short} value.236*237* <p> The bit offset within the stream is reset to zero before238* the read occurs.239*240* @return a signed short value from the stream.241*242* @exception java.io.EOFException if the stream reaches the end before243* reading all the bytes.244* @exception IOException if an I/O error occurs.245*246* @see #getByteOrder247*/248short readShort() throws IOException;249250/**251* Reads two bytes from the stream, and (conceptually)252* concatenates them according to the current byte order, converts253* the resulting value to an {@code int}, masks it with254* {@code 0xffff} in order to strip off any sign-extension255* buts, and returns the result as an unsigned {@code int}256* value.257*258* <p> The bit offset within the stream is reset to zero before259* the read occurs.260*261* @return an unsigned short value from the stream, as an int.262*263* @exception java.io.EOFException if the stream reaches the end before264* reading all the bytes.265* @exception IOException if an I/O error occurs.266*267* @see #getByteOrder268*/269int readUnsignedShort() throws IOException;270271/**272* Equivalent to {@code readUnsignedShort}, except that the273* result is returned using the {@code char} datatype.274*275* <p> The bit offset within the stream is reset to zero before276* the read occurs.277*278* @return an unsigned char value from the stream.279*280* @exception java.io.EOFException if the stream reaches the end before281* reading all the bytes.282* @exception IOException if an I/O error occurs.283*284* @see #readUnsignedShort285*/286char readChar() throws IOException;287288/**289* Reads 4 bytes from the stream, and (conceptually) concatenates290* them according to the current byte order and returns the result291* as an {@code int}.292*293* <p> The bit offset within the stream is ignored and treated as294* though it were zero.295*296* @return a signed int value from the stream.297*298* @exception java.io.EOFException if the stream reaches the end before299* reading all the bytes.300* @exception IOException if an I/O error occurs.301*302* @see #getByteOrder303*/304int readInt() throws IOException;305306/**307* Reads 4 bytes from the stream, and (conceptually) concatenates308* them according to the current byte order, converts the result309* to a long, masks it with {@code 0xffffffffL} in order to310* strip off any sign-extension bits, and returns the result as an311* unsigned {@code long} value.312*313* <p> The bit offset within the stream is reset to zero before314* the read occurs.315*316* @return an unsigned int value from the stream, as a long.317*318* @exception java.io.EOFException if the stream reaches the end before319* reading all the bytes.320* @exception IOException if an I/O error occurs.321*322* @see #getByteOrder323*/324long readUnsignedInt() throws IOException;325326/**327* Reads 8 bytes from the stream, and (conceptually) concatenates328* them according to the current byte order and returns the result329* as a {@code long}.330*331* <p> The bit offset within the stream is reset to zero before332* the read occurs.333*334* @return a signed long value from the stream.335*336* @exception java.io.EOFException if the stream reaches the end before337* reading all the bytes.338* @exception IOException if an I/O error occurs.339*340* @see #getByteOrder341*/342long readLong() throws IOException;343344/**345* Reads 4 bytes from the stream, and (conceptually) concatenates346* them according to the current byte order and returns the result347* as a {@code float}.348*349* <p> The bit offset within the stream is reset to zero before350* the read occurs.351*352* @return a float value from the stream.353*354* @exception java.io.EOFException if the stream reaches the end before355* reading all the bytes.356* @exception IOException if an I/O error occurs.357*358* @see #getByteOrder359*/360float readFloat() throws IOException;361362/**363* Reads 8 bytes from the stream, and (conceptually) concatenates364* them according to the current byte order and returns the result365* as a {@code double}.366*367* <p> The bit offset within the stream is reset to zero before368* the read occurs.369*370* @return a double value from the stream.371*372* @exception java.io.EOFException if the stream reaches the end before373* reading all the bytes.374* @exception IOException if an I/O error occurs.375*376* @see #getByteOrder377*/378double readDouble() throws IOException;379380/**381* Reads the next line of text from the input stream. It reads382* successive bytes, converting each byte separately into a383* character, until it encounters a line terminator or end of384* file; the characters read are then returned as a385* {@code String}. Note that because this method processes386* bytes, it does not support input of the full Unicode character387* set.388*389* <p> If end of file is encountered before even one byte can be390* read, then {@code null} is returned. Otherwise, each byte391* that is read is converted to type {@code char} by392* zero-extension. If the character {@code '\n'} is393* encountered, it is discarded and reading ceases. If the394* character {@code '\r'} is encountered, it is discarded395* and, if the following byte converts  to the character396* {@code '\n'}, then that is discarded also; reading then397* ceases. If end of file is encountered before either of the398* characters {@code '\n'} and {@code '\r'} is399* encountered, reading ceases. Once reading has ceased, a400* {@code String} is returned that contains all the401* characters read and not discarded, taken in order. Note that402* every character in this string will have a value less than403* <code>\u0100</code>, that is, {@code (char)256}.404*405* <p> The bit offset within the stream is reset to zero before406* the read occurs.407*408* @return a String containing a line of text from the stream.409*410* @exception IOException if an I/O error occurs.411*/412String readLine() throws IOException;413414/**415* Reads in a string that has been encoded using a416* <a href="../../../../java.base/java/io/DataInput.html#modified-utf-8">417* modified UTF-8</a>418* format. The general contract of {@code readUTF} is that419* it reads a representation of a Unicode character string encoded420* in modified UTF-8 format; this string of characters is421* then returned as a {@code String}.422*423* <p> First, two bytes are read and used to construct an unsigned424* 16-bit integer in the manner of the425* {@code readUnsignedShort} method, using network byte order426* (regardless of the current byte order setting). This integer427* value is called the <i>UTF length</i> and specifies the number428* of additional bytes to be read. These bytes are then converted429* to characters by considering them in groups. The length of each430* group is computed from the value of the first byte of the431* group. The byte following a group, if any, is the first byte of432* the next group.433*434* <p> If the first byte of a group matches the bit pattern435* {@code 0xxxxxxx} (where {@code x} means "may be436* {@code 0} or {@code 1}"), then the group consists of437* just that byte. The byte is zero-extended to form a character.438*439* <p> If the first byte of a group matches the bit pattern440* {@code 110xxxxx}, then the group consists of that byte441* {@code a} and a second byte {@code b}. If there is no442* byte {@code b} (because byte {@code a} was the last443* of the bytes to be read), or if byte {@code b} does not444* match the bit pattern {@code 10xxxxxx}, then a445* {@code UTFDataFormatException} is thrown. Otherwise, the446* group is converted to the character:447*448* <pre><code>449* (char)(((a& 0x1F) << 6) | (b & 0x3F))450* </code></pre>451*452* If the first byte of a group matches the bit pattern453* {@code 1110xxxx}, then the group consists of that byte454* {@code a} and two more bytes {@code b} and455* {@code c}. If there is no byte {@code c} (because456* byte {@code a} was one of the last two of the bytes to be457* read), or either byte {@code b} or byte {@code c}458* does not match the bit pattern {@code 10xxxxxx}, then a459* {@code UTFDataFormatException} is thrown. Otherwise, the460* group is converted to the character:461*462* <pre><code>463* (char)(((a & 0x0F) << 12) | ((b & 0x3F) << 6) | (c & 0x3F))464* </code></pre>465*466* If the first byte of a group matches the pattern467* {@code 1111xxxx} or the pattern {@code 10xxxxxx},468* then a {@code UTFDataFormatException} is thrown.469*470* <p> If end of file is encountered at any time during this471* entire process, then a {@code java.io.EOFException} is thrown.472*473* <p> After every group has been converted to a character by this474* process, the characters are gathered, in the same order in475* which their corresponding groups were read from the input476* stream, to form a {@code String}, which is returned.477*478* <p> The current byte order setting is ignored.479*480* <p> The bit offset within the stream is reset to zero before481* the read occurs.482*483* <p><strong>Note:</strong> This method should not be used in484* the implementation of image formats that use standard UTF-8,485* because the modified UTF-8 used here is incompatible with486* standard UTF-8.487*488* @return a String read from the stream.489*490* @exception java.io.EOFException if this stream reaches the end491* before reading all the bytes.492* @exception java.io.UTFDataFormatException if the bytes do not represent493* a valid modified UTF-8 encoding of a string.494* @exception IOException if an I/O error occurs.495*/496String readUTF() throws IOException;497498/**499* Reads {@code len} bytes from the stream, and stores them500* into {@code b} starting at index {@code off}.501* If the end of the stream is reached, a {@code java.io.EOFException}502* will be thrown.503*504* <p> The bit offset within the stream is reset to zero before505* the read occurs.506*507* @param b an array of bytes to be written to.508* @param off the starting position within {@code b} to write to.509* @param len the maximum number of {@code byte}s to read.510*511* @exception IndexOutOfBoundsException if {@code off} is512* negative, {@code len} is negative, or {@code off + len}513* is greater than {@code b.length}.514* @exception NullPointerException if {@code b} is515* {@code null}.516* @exception java.io.EOFException if the stream reaches the end before517* reading all the bytes.518* @exception IOException if an I/O error occurs.519*/520void readFully(byte[] b, int off, int len) throws IOException;521522/**523* Reads {@code b.length} bytes from the stream, and stores them524* into {@code b} starting at index {@code 0}.525* If the end of the stream is reached, a {@code java.io.EOFException}526* will be thrown.527*528* <p> The bit offset within the stream is reset to zero before529* the read occurs.530*531* @param b an array of {@code byte}s.532*533* @exception NullPointerException if {@code b} is534* {@code null}.535* @exception java.io.EOFException if the stream reaches the end before536* reading all the bytes.537* @exception IOException if an I/O error occurs.538*/539void readFully(byte[] b) throws IOException;540541/**542* Reads {@code len} shorts (signed 16-bit integers) from the543* stream according to the current byte order, and544* stores them into {@code s} starting at index545* {@code off}. If the end of the stream is reached,546* a {@code java.io.EOFException} will be thrown.547*548* <p> The bit offset within the stream is reset to zero before549* the read occurs.550*551* @param s an array of shorts to be written to.552* @param off the starting position within {@code s} to write to.553* @param len the maximum number of {@code short}s to read.554*555* @exception IndexOutOfBoundsException if {@code off} is556* negative, {@code len} is negative, or {@code off + len}557* is greater than {@code s.length}.558* @exception NullPointerException if {@code s} is559* {@code null}.560* @exception java.io.EOFException if the stream reaches the end before561* reading all the bytes.562* @exception IOException if an I/O error occurs.563*/564void readFully(short[] s, int off, int len) throws IOException;565566/**567* Reads {@code len} chars (unsigned 16-bit integers) from the568* stream according to the current byte order, and569* stores them into {@code c} starting at index570* {@code off}. If the end of the stream is reached,571* a {@code java.io.EOFException} will be thrown.572*573* <p> The bit offset within the stream is reset to zero before574* the read occurs.575*576* @param c an array of chars to be written to.577* @param off the starting position within {@code c} to write to.578* @param len the maximum number of {@code char}s to read.579*580* @exception IndexOutOfBoundsException if {@code off} is581* negative, {@code len} is negative, or {@code off + len}582* is greater than {@code c.length}.583* @exception NullPointerException if {@code c} is584* {@code null}.585* @exception java.io.EOFException if the stream reaches the end before586* reading all the bytes.587* @exception IOException if an I/O error occurs.588*/589void readFully(char[] c, int off, int len) throws IOException;590591/**592* Reads {@code len} ints (signed 32-bit integers) from the593* stream according to the current byte order, and594* stores them into {@code i} starting at index595* {@code off}. If the end of the stream is reached,596* a {@code java.io.EOFException} will be thrown.597*598* <p> The bit offset within the stream is reset to zero before599* the read occurs.600*601* @param i an array of ints to be written to.602* @param off the starting position within {@code i} to write to.603* @param len the maximum number of {@code int}s to read.604*605* @exception IndexOutOfBoundsException if {@code off} is606* negative, {@code len} is negative, or {@code off + len}607* is greater than {@code i.length}.608* @exception NullPointerException if {@code i} is609* {@code null}.610* @exception java.io.EOFException if the stream reaches the end before611* reading all the bytes.612* @exception IOException if an I/O error occurs.613*/614void readFully(int[] i, int off, int len) throws IOException;615616/**617* Reads {@code len} longs (signed 64-bit integers) from the618* stream according to the current byte order, and619* stores them into {@code l} starting at index620* {@code off}. If the end of the stream is reached,621* a {@code java.io.EOFException} will be thrown.622*623* <p> The bit offset within the stream is reset to zero before624* the read occurs.625*626* @param l an array of longs to be written to.627* @param off the starting position within {@code l} to write to.628* @param len the maximum number of {@code long}s to read.629*630* @exception IndexOutOfBoundsException if {@code off} is631* negative, {@code len} is negative, or {@code off + len}632* is greater than {@code l.length}.633* @exception NullPointerException if {@code l} is634* {@code null}.635* @exception java.io.EOFException if the stream reaches the end before636* reading all the bytes.637* @exception IOException if an I/O error occurs.638*/639void readFully(long[] l, int off, int len) throws IOException;640641/**642* Reads {@code len} floats (32-bit IEEE single-precision643* floats) from the stream according to the current byte order,644* and stores them into {@code f} starting at645* index {@code off}. If the end of the stream is reached,646* a {@code java.io.EOFException} will be thrown.647*648* <p> The bit offset within the stream is reset to zero before649* the read occurs.650*651* @param f an array of floats to be written to.652* @param off the starting position within {@code f} to write to.653* @param len the maximum number of {@code float}s to read.654*655* @exception IndexOutOfBoundsException if {@code off} is656* negative, {@code len} is negative, or {@code off + len}657* is greater than {@code f.length}.658* @exception NullPointerException if {@code f} is659* {@code null}.660* @exception java.io.EOFException if the stream reaches the end before661* reading all the bytes.662* @exception IOException if an I/O error occurs.663*/664void readFully(float[] f, int off, int len) throws IOException;665666/**667* Reads {@code len} doubles (64-bit IEEE double-precision668* floats) from the stream according to the current byte order,669* and stores them into {@code d} starting at670* index {@code off}. If the end of the stream is reached,671* a {@code java.io.EOFException} will be thrown.672*673* <p> The bit offset within the stream is reset to zero before674* the read occurs.675*676* @param d an array of doubles to be written to.677* @param off the starting position within {@code d} to write to.678* @param len the maximum number of {@code double}s to read.679*680* @exception IndexOutOfBoundsException if {@code off} is681* negative, {@code len} is negative, or {@code off + len}682* is greater than {@code d.length}.683* @exception NullPointerException if {@code d} is684* {@code null}.685* @exception java.io.EOFException if the stream reaches the end before686* reading all the bytes.687* @exception IOException if an I/O error occurs.688*/689void readFully(double[] d, int off, int len) throws IOException;690691/**692* Returns the current byte position of the stream. The next read693* will take place starting at this offset.694*695* @return a long containing the position of the stream.696*697* @exception IOException if an I/O error occurs.698*/699long getStreamPosition() throws IOException;700701/**702* Returns the current bit offset, as an integer between 0 and 7,703* inclusive. The bit offset is updated implicitly by calls to704* the {@code readBits} method. A value of 0 indicates the705* most-significant bit, and a value of 7 indicates the least706* significant bit, of the byte being read.707*708* <p> The bit offset is set to 0 when a stream is first709* opened, and is reset to 0 by calls to {@code seek},710* {@code skipBytes}, or any {@code read} or711* {@code readFully} method.712*713* @return an {@code int} containing the bit offset between714* 0 and 7, inclusive.715*716* @exception IOException if an I/O error occurs.717*718* @see #setBitOffset719*/720int getBitOffset() throws IOException;721722/**723* Sets the bit offset to an integer between 0 and 7, inclusive.724* The byte offset within the stream, as returned by725* {@code getStreamPosition}, is left unchanged.726* A value of 0 indicates the727* most-significant bit, and a value of 7 indicates the least728* significant bit, of the byte being read.729*730* @param bitOffset the desired offset, as an {@code int}731* between 0 and 7, inclusive.732*733* @exception IllegalArgumentException if {@code bitOffset}734* is not between 0 and 7, inclusive.735* @exception IOException if an I/O error occurs.736*737* @see #getBitOffset738*/739void setBitOffset(int bitOffset) throws IOException;740741/**742* Reads a single bit from the stream and returns it as an743* {@code int} with the value {@code 0} or744* {@code 1}. The bit offset is advanced by one and reduced745* modulo 8.746*747* @return an {@code int} containing the value {@code 0}748* or {@code 1}.749*750* @exception java.io.EOFException if the stream reaches the end before751* reading all the bits.752* @exception IOException if an I/O error occurs.753*/754int readBit() throws IOException;755756/**757* Reads a bitstring from the stream and returns it as a758* {@code long}, with the first bit read becoming the most759* significant bit of the output. The read starts within the byte760* indicated by {@code getStreamPosition}, at the bit given761* by {@code getBitOffset}. The bit offset is advanced by762* {@code numBits} and reduced modulo 8.763*764* <p> The byte order of the stream has no effect on this765* method. The return value of this method is constructed as766* though the bits were read one at a time, and shifted into767* the right side of the return value, as shown by the following768* pseudo-code:769*770* <pre>{@code771* long accum = 0L;772* for (int i = 0; i < numBits; i++) {773* accum <<= 1; // Shift left one bit to make room774* accum |= readBit();775* }776* }</pre>777*778* Note that the result of {@code readBits(32)} may thus not779* be equal to that of {@code readInt()} if a reverse network780* byte order is being used (i.e., {@code getByteOrder() == false}).781*782* <p> If the end of the stream is encountered before all the bits783* have been read, a {@code java.io.EOFException} is thrown.784*785* @param numBits the number of bits to read, as an {@code int}786* between 0 and 64, inclusive.787* @return the bitstring, as a {@code long} with the last bit788* read stored in the least significant bit.789*790* @exception IllegalArgumentException if {@code numBits}791* is not between 0 and 64, inclusive.792* @exception java.io.EOFException if the stream reaches the end before793* reading all the bits.794* @exception IOException if an I/O error occurs.795*/796long readBits(int numBits) throws IOException;797798/**799* Returns the total length of the stream, if known. Otherwise,800* {@code -1} is returned.801*802* @return a {@code long} containing the length of the803* stream, if known, or else {@code -1}.804*805* @exception IOException if an I/O error occurs.806*/807long length() throws IOException;808809/**810* Moves the stream position forward by a given number of bytes. It811* is possible that this method will only be able to skip forward812* by a smaller number of bytes than requested, for example if the813* end of the stream is reached. In all cases, the actual number814* of bytes skipped is returned. The bit offset is set to zero815* prior to advancing the position.816*817* @param n an {@code int} containing the number of bytes to818* be skipped.819*820* @return an {@code int} representing the number of bytes skipped.821*822* @exception IOException if an I/O error occurs.823*/824int skipBytes(int n) throws IOException;825826/**827* Moves the stream position forward by a given number of bytes.828* This method is identical to {@code skipBytes(int)} except829* that it allows for a larger skip distance.830*831* @param n a {@code long} containing the number of bytes to832* be skipped.833*834* @return a {@code long} representing the number of bytes835* skipped.836*837* @exception IOException if an I/O error occurs.838*/839long skipBytes(long n) throws IOException;840841/**842* Sets the current stream position to the desired location. The843* next read will occur at this location. The bit offset is set844* to 0.845*846* <p> An {@code IndexOutOfBoundsException} will be thrown if847* {@code pos} is smaller than the flushed position (as848* returned by {@code getflushedPosition}).849*850* <p> It is legal to seek past the end of the file;851* a {@code java.io.EOFException} will be thrown only if a read is852* performed.853*854* @param pos a {@code long} containing the desired file855* pointer position.856*857* @exception IndexOutOfBoundsException if {@code pos} is smaller858* than the flushed position.859* @exception IOException if any other I/O error occurs.860*/861void seek(long pos) throws IOException;862863/**864* Marks a position in the stream to be returned to by a865* subsequent call to {@code reset}. Unlike a standard866* {@code InputStream}, all {@code ImageInputStream}s867* support marking. Additionally, calls to {@code mark} and868* {@code reset} may be nested arbitrarily.869*870* <p> Unlike the {@code mark} methods declared by the871* {@code Reader} and {@code InputStream} interfaces, no872* {@code readLimit} parameter is used. An arbitrary amount873* of data may be read following the call to {@code mark}.874*875* <p> The bit position used by the {@code readBits} method876* is saved and restored by each pair of calls to877* {@code mark} and {@code reset}.878*879* <p> Note that it is valid for an {@code ImageReader} to call880* {@code flushBefore} as part of a read operation.881* Therefore, if an application calls {@code mark} prior to882* passing that stream to an {@code ImageReader}, the application883* should not assume that the marked position will remain valid after884* the read operation has completed.885*/886void mark();887888/**889* Returns the stream pointer to its previous position, including890* the bit offset, at the time of the most recent unmatched call891* to {@code mark}.892*893* <p> Calls to {@code reset} without a corresponding call894* to {@code mark} have no effect.895*896* <p> An {@code IOException} will be thrown if the previous897* marked position lies in the discarded portion of the stream.898*899* @exception IOException if an I/O error occurs.900*/901void reset() throws IOException;902903/**904* Discards the initial portion of the stream prior to the905* indicated position. Attempting to seek to an offset within the906* flushed portion of the stream will result in an907* {@code IndexOutOfBoundsException}.908*909* <p> Calling {@code flushBefore} may allow classes910* implementing this interface to free up resources such as memory911* or disk space that are being used to store data from the912* stream.913*914* @param pos a {@code long} containing the length of the915* stream prefix that may be flushed.916*917* @exception IndexOutOfBoundsException if {@code pos} lies918* in the flushed portion of the stream or past the current stream919* position.920* @exception IOException if an I/O error occurs.921*/922void flushBefore(long pos) throws IOException;923924/**925* Discards the initial position of the stream prior to the current926* stream position. Equivalent to927* {@code flushBefore(getStreamPosition())}.928*929* @exception IOException if an I/O error occurs.930*/931void flush() throws IOException;932933/**934* Returns the earliest position in the stream to which seeking935* may be performed. The returned value will be the maximum of936* all values passed into previous calls to937* {@code flushBefore}.938*939* @return the earliest legal position for seeking, as a940* {@code long}.941*/942long getFlushedPosition();943944/**945* Returns {@code true} if this {@code ImageInputStream}946* caches data itself in order to allow seeking backwards.947* Applications may consult this in order to decide how frequently,948* or whether, to flush in order to conserve cache resources.949*950* @return {@code true} if this {@code ImageInputStream}951* caches data.952*953* @see #isCachedMemory954* @see #isCachedFile955*/956boolean isCached();957958/**959* Returns {@code true} if this {@code ImageInputStream}960* caches data itself in order to allow seeking backwards, and961* the cache is kept in main memory. Applications may consult962* this in order to decide how frequently, or whether, to flush963* in order to conserve cache resources.964*965* @return {@code true} if this {@code ImageInputStream}966* caches data in main memory.967*968* @see #isCached969* @see #isCachedFile970*/971boolean isCachedMemory();972973/**974* Returns {@code true} if this {@code ImageInputStream}975* caches data itself in order to allow seeking backwards, and976* the cache is kept in a temporary file. Applications may consult977* this in order to decide how frequently, or whether, to flush978* in order to conserve cache resources.979*980* @return {@code true} if this {@code ImageInputStream}981* caches data in a temporary file.982*983* @see #isCached984* @see #isCachedMemory985*/986boolean isCachedFile();987988/**989* Closes the stream. Attempts to access a stream that has been990* closed may result in {@code IOException}s or incorrect991* behavior. Calling this method may allow classes implementing992* this interface to release resources associated with the stream993* such as memory, disk space, or file descriptors.994*995* @exception IOException if an I/O error occurs.996*/997void close() throws IOException;998}99910001001