Class MemoryCacheImageOutputStream
- All Implemented Interfaces:
Closeable, DataInput, DataOutput, AutoCloseable, ImageInputStream, ImageOutputStream
ImageOutputStream that writes its
output to a regular OutputStream. A memory buffer is
used to cache at least the data between the discard position and
the current write position. The only constructor takes an
OutputStream, so this class may not be used for
read/modify/write operations. Reading can occur only on parts of
the stream that have already been written to the cache and not
yet flushed.-
Field Summary
Fields declared in class ImageInputStreamImpl
bitOffset, byteOrder, flushedPos, streamPosModifier and TypeFieldDescriptionprotected intThe current bit offset within the stream.protected ByteOrderThe byte order of the stream as an instance of the enumeration classjava.nio.ByteOrder, whereByteOrder.BIG_ENDIANindicates network byte order andByteOrder.LITTLE_ENDIANindicates the reverse order.protected longThe position prior to which data may be discarded.protected longThe current read position within the stream. -
Constructor Summary
ConstructorsConstructorDescriptionConstructs aMemoryCacheImageOutputStreamthat will write to a givenOutputStream. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes thisMemoryCacheImageOutputStream.voidflushBefore(long pos) Flushes all data prior to the given position to the underlying destination, such as anOutputStreamorFile.booleanisCached()Returnstruesince thisImageOutputStreamcaches data in order to allow seeking backwards.booleanReturnsfalsesince thisImageOutputStreamdoes not maintain a file cache.booleanReturnstruesince thisImageOutputStreammaintains a main memory cache.intread()Reads a single byte from the stream and returns it as anintbetween 0 and 255.intread(byte[] b, int off, int len) Reads up tolenbytes from the stream, and stores them intobstarting at indexoff.voidwrite(byte[] b, int off, int len) Writes a sequence of bytes to the stream at the current position.voidwrite(int b) Writes a single byte to the stream at the current position.Methods declared in class ImageOutputStreamImpl
flushBits, write, writeBit, writeBits, writeBoolean, writeByte, writeBytes, writeChar, writeChars, writeChars, writeDouble, writeDoubles, writeFloat, writeFloats, writeInt, writeInts, writeLong, writeLongs, writeShort, writeShorts, writeUTFModifier and TypeMethodDescriptionprotected final voidIf the bit offset is non-zero, forces the remaining bits in the current byte to 0 and advances the stream position by one.voidwrite(byte[] b) Writes a sequence of bytes to the stream at the current position.voidwriteBit(int bit) Writes a single bit, given by the least significant bit of the argument, to the stream at the current bit offset within the current byte position.voidwriteBits(long bits, int numBits) Writes a sequence of bits, given by thenumBitsleast significant bits of thebitsargument in left-to-right order, to the stream at the current bit offset within the current byte position.voidwriteBoolean(boolean v) Writes abooleanvalue to the stream.voidwriteByte(int v) Writes the 8 low-order bits ofvto the stream.voidwriteBytes(String s) Writes a string to the output stream.voidwriteChar(int v) This method is a synonym forwriteShort.voidwriteChars(char[] c, int off, int len) Writes a sequence of chars to the stream at the current position.voidwriteChars(String s) Writes a string to the output stream.voidwriteDouble(double v) Writes adoublevalue, which is comprised of four bytes, to the output stream.voidwriteDoubles(double[] d, int off, int len) Writes a sequence of doubles to the stream at the current position.voidwriteFloat(float v) Writes afloatvalue, which is comprised of four bytes, to the output stream.voidwriteFloats(float[] f, int off, int len) Writes a sequence of floats to the stream at the current position.voidwriteInt(int v) Writes the 32 bits ofvto the stream.voidwriteInts(int[] i, int off, int len) Writes a sequence of ints to the stream at the current position.voidwriteLong(long v) Writes the 64 bits ofvto the stream.voidwriteLongs(long[] l, int off, int len) Writes a sequence of longs to the stream at the current position.voidwriteShort(int v) Writes the 16 low-order bits ofvto the stream.voidwriteShorts(short[] s, int off, int len) Writes a sequence of shorts to the stream at the current position.voidWrites two bytes of length information to the output stream in network byte order, followed by the modified UTF-8 representation of every character in the strings.Methods declared in class ImageInputStreamImpl
checkClosed, flush, getBitOffset, getByteOrder, getFlushedPosition, getStreamPosition, length, mark, read, readBit, readBits, readBoolean, readByte, readBytes, readChar, readDouble, readFloat, readFully, readFully, readFully, readFully, readFully, readFully, readFully, readFully, readInt, readLine, readLong, readShort, readUnsignedByte, readUnsignedInt, readUnsignedShort, readUTF, reset, seek, setBitOffset, setByteOrder, skipBytes, skipBytesModifier and TypeMethodDescriptionprotected final voidThrows anIOExceptionif the stream has been closed.voidflush()Discards the initial position of the stream prior to the current stream position.intReturns the current bit offset, as an integer between 0 and 7, inclusive.Returns the byte order with which data values will be read from this stream as an instance of thejava.nio.ByteOrderenumeration.longReturns the earliest position in the stream to which seeking may be performed.longReturns the current byte position of the stream.longlength()Returns-1Lto indicate that the stream has unknown length.voidmark()Pushes the current stream position onto a stack of marked positions.intread(byte[] b) A convenience method that callsread(b, 0, b.length).intreadBit()Reads a single bit from the stream and returns it as anintwith the value0or1.longreadBits(int numBits) Reads a bitstring from the stream and returns it as along, with the first bit read becoming the most significant bit of the output.booleanReads a byte from the stream and returns abooleanvalue oftrueif it is nonzero,falseif it is zero.bytereadByte()Reads a byte from the stream and returns it as abytevalue.voidreadBytes(IIOByteBuffer buf, int len) Reads up tolenbytes from the stream, and modifies the suppliedIIOByteBufferto indicate the byte array, offset, and length where the data may be found.charreadChar()Equivalent toreadUnsignedShort, except that the result is returned using thechardatatype.doubleReads 8 bytes from the stream, and (conceptually) concatenates them according to the current byte order and returns the result as adouble.floatReads 4 bytes from the stream, and (conceptually) concatenates them according to the current byte order and returns the result as afloat.voidreadFully(byte[] b) Readsb.lengthbytes from the stream, and stores them intobstarting at index0.voidreadFully(byte[] b, int off, int len) Readslenbytes from the stream, and stores them intobstarting at indexoff.voidreadFully(char[] c, int off, int len) Readslenchars (unsigned 16-bit integers) from the stream according to the current byte order, and stores them intocstarting at indexoff.voidreadFully(double[] d, int off, int len) Readslendoubles (64-bit IEEE double-precision floats) from the stream according to the current byte order, and stores them intodstarting at indexoff.voidreadFully(float[] f, int off, int len) Readslenfloats (32-bit IEEE single-precision floats) from the stream according to the current byte order, and stores them intofstarting at indexoff.voidreadFully(int[] i, int off, int len) Readslenints (signed 32-bit integers) from the stream according to the current byte order, and stores them intoistarting at indexoff.voidreadFully(long[] l, int off, int len) Readslenlongs (signed 64-bit integers) from the stream according to the current byte order, and stores them intolstarting at indexoff.voidreadFully(short[] s, int off, int len) Readslenshorts (signed 16-bit integers) from the stream according to the current byte order, and stores them intosstarting at indexoff.intreadInt()Reads 4 bytes from the stream, and (conceptually) concatenates them according to the current byte order and returns the result as anint.readLine()Reads the next line of text from the input stream.longreadLong()Reads 8 bytes from the stream, and (conceptually) concatenates them according to the current byte order and returns the result as along.shortReads two bytes from the stream, and (conceptually) concatenates them according to the current byte order, and returns the result as ashortvalue.intReads a byte from the stream, and (conceptually) converts it to an int, masks it with0xffin order to strip off any sign-extension bits, and returns it as abytevalue.longReads 4 bytes from the stream, and (conceptually) concatenates them according to the current byte order, converts the result to a long, masks it with0xffffffffLin order to strip off any sign-extension bits, and returns the result as an unsignedlongvalue.intReads two bytes from the stream, and (conceptually) concatenates them according to the current byte order, converts the resulting value to anint, masks it with0xffffin order to strip off any sign-extension buts, and returns the result as an unsignedintvalue.readUTF()Reads in a string that has been encoded using a modified UTF-8 format.voidreset()Resets the current stream byte and bit positions from the stack of marked positions.voidseek(long pos) Sets the current stream position to the desired location.voidsetBitOffset(int bitOffset) Sets the bit offset to an integer between 0 and 7, inclusive.voidsetByteOrder(ByteOrder byteOrder) Sets the desired byte order for future reads of data values from this stream.intskipBytes(int n) Advances the current stream position by callingseek(getStreamPosition() + n).longskipBytes(long n) Advances the current stream position by callingseek(getStreamPosition() + n).Methods declared in class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitModifier and TypeMethodDescriptionprotected Objectclone()Creates and returns a copy of this object.booleanIndicates whether some other object is "equal to" this one.protected voidfinalize()Deprecated, for removal: This API element is subject to removal in a future version.Finalization is deprecated and subject to removal in a future release.final Class<?> getClass()Returns the runtime class of thisObject.inthashCode()Returns a hash code value for this object.final voidnotify()Wakes up a single thread that is waiting on this object's monitor.final voidWakes up all threads that are waiting on this object's monitor.toString()Returns a string representation of the object.final voidwait()Causes the current thread to wait until it is awakened, typically by being notified or interrupted.final voidwait(long timeoutMillis) Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.final voidwait(long timeoutMillis, int nanos) Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.
-
Constructor Details
-
MemoryCacheImageOutputStream
Constructs aMemoryCacheImageOutputStreamthat will write to a givenOutputStream.- Parameters:
stream- anOutputStreamto write to.- Throws:
IllegalArgumentException- ifstreamisnull.
-
-
Method Details
-
read
Description copied from class:ImageInputStreamImplReads a single byte from the stream and returns it as anintbetween 0 and 255. If EOF is reached,-1is returned.Subclasses must provide an implementation for this method. The subclass implementation should update the stream position before exiting.
The bit offset within the stream must be reset to zero before the read occurs.
- Specified by:
readin interfaceImageInputStream- Specified by:
readin classImageInputStreamImpl- Returns:
- the value of the next byte in the stream, or
-1if EOF is reached. - Throws:
IOException- if the stream has been closed.
-
read
Description copied from class:ImageInputStreamImplReads up tolenbytes from the stream, and stores them intobstarting at indexoff. If no bytes can be read because the end of the stream has been reached,-1is returned.The bit offset within the stream must be reset to zero before the read occurs.
Subclasses must provide an implementation for this method. The subclass implementation should update the stream position before exiting.
- Specified by:
readin interfaceImageInputStream- Specified by:
readin classImageInputStreamImpl- Parameters:
b- an array of bytes to be written to.off- the starting position withinbto write to.len- the maximum number of bytes to read.- Returns:
- the number of bytes actually read, or
-1to indicate EOF. - Throws:
IOException- if an I/O error occurs.
-
write
Description copied from interface:ImageOutputStreamWrites a single byte to the stream at the current position. The 24 high-order bits ofbare ignored.If the bit offset within the stream is non-zero, the remainder of the current byte is padded with 0s and written out first. The bit offset will be 0 after the write. Implementers can use the
flushBitsmethod ofImageOutputStreamImplto guarantee this.- Parameters:
b- anintwhose lower 8 bits are to be written.- Throws:
IOException- if an I/O error occurs.
-
write
Description copied from interface:ImageOutputStreamWrites a sequence of bytes to the stream at the current position. Iflenis 0, nothing is written. The byteb[off]is written first, then the byteb[off + 1], and so on.If the bit offset within the stream is non-zero, the remainder of the current byte is padded with 0s and written out first. The bit offset will be 0 after the write. Implementers can use the
flushBitsmethod ofImageOutputStreamImplto guarantee this.- Parameters:
b- an array ofbytes to be written.off- the start offset in the data.len- the number ofbytes to write.- Throws:
IOException- if an I/O error occurs.
-
isCached
public boolean isCached()Returnstruesince thisImageOutputStreamcaches data in order to allow seeking backwards.- Specified by:
isCachedin interfaceImageInputStream- Overrides:
isCachedin classImageInputStreamImpl- Returns:
true.- See Also:
-
isCachedFile
public boolean isCachedFile()Returnsfalsesince thisImageOutputStreamdoes not maintain a file cache.- Specified by:
isCachedFilein interfaceImageInputStream- Overrides:
isCachedFilein classImageInputStreamImpl- Returns:
false.- See Also:
-
isCachedMemory
public boolean isCachedMemory()Returnstruesince thisImageOutputStreammaintains a main memory cache.- Specified by:
isCachedMemoryin interfaceImageInputStream- Overrides:
isCachedMemoryin classImageInputStreamImpl- Returns:
true.- See Also:
-
close
Closes thisMemoryCacheImageOutputStream. All pending data is flushed to the output, and the cache is released. The destinationOutputStreamis not closed.- Throws:
IOException- if an I/O error occurs.
-
flushBefore
Flushes all data prior to the given position to the underlying destination, such as anOutputStreamorFile. Attempting to seek to the flushed portion of the stream will result in anIndexOutOfBoundsException.- Parameters:
pos- alongcontaining the length of the stream prefix that may be flushed to the destination.- Throws:
IOException- if an I/O error occurs.
-