Class HTTPImageInputStream
- All Implemented Interfaces:
Closeable
,DataInput
,AutoCloseable
,ImageInputStream
Input stream that supports pseudo-seeking over HTTP.
The stream is divided conceptually into fixed-size windows from which chunks of data are fetched as needed using ranged HTTP requests. This technique may improve efficiency when reading small portions of large images that are selectively readable, like JPEG2000 and multiresolution+tiled TIFF, over low-bandwidth connections. Conversely, it may reduce efficiency when reading large portions of images.
Downloaded chunks can be cached in memory. This could help when readers seek around a lot beyond the window size, or request a lot more data than they would theoretically need.
The HTTP client is abstracted into the exceedingly simple HTTPImageInputStreamClient
interface, so probably any existing client
implementation, including many cloud storage clients, can be hooked up and
used easily, without this class needing to know about things like SSL/TLS,
request signing, etc.
This class works only with HTTP servers that support Range
requests, as advertised by the presence of a Accept-Ranges: bytes
header in a HEAD
response.
-
Field Summary
Fields inherited from class javax.imageio.stream.ImageInputStreamImpl
bitOffset, byteOrder, flushedPos, streamPos
-
Constructor Summary
ConstructorsConstructorDescriptionVariant that sends a preliminaryHEAD
request to retrieve some needed information.HTTPImageInputStream
(HTTPImageInputStreamClient client, long resourceLength) -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
void
flushBefore
(long pos) Invalidates any cached data lying entirely before the stream position.static long
int
static boolean
long
length()
int
read()
int
read
(byte[] b, int offset, int requestedLength) void
seek
(long pos) void
setWindowSize
(int windowSize) Sets the window size.Methods inherited from class javax.imageio.stream.ImageInputStreamImpl
checkClosed, finalize, flush, getBitOffset, getByteOrder, getFlushedPosition, getStreamPosition, isCached, isCachedFile, isCachedMemory, 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, setBitOffset, setByteOrder, skipBytes, skipBytes
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface javax.imageio.stream.ImageInputStream
flush, getBitOffset, getByteOrder, getFlushedPosition, getStreamPosition, isCached, isCachedFile, isCachedMemory, 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, setBitOffset, setByteOrder, skipBytes, skipBytes
-
Constructor Details
-
HTTPImageInputStream
Variant that sends a preliminaryHEAD
request to retrieve some needed information. UseHTTPImageInputStream(HTTPImageInputStreamClient, long)
instead if you already know the resource length and that the server supports ranged requests.- Parameters:
client
- Client to use to handle requests.- Throws:
RangesNotSupportedException
- if the server does not advertise support for ranges.IOException
- if the response does not include a validContent-Length
header or some other communication error occurs.
-
HTTPImageInputStream
- Parameters:
client
- Client to use to handle requests.resourceLength
- Resource length/size.
-
-
Method Details
-
getMaxChunkCacheSize
public static long getMaxChunkCacheSize() -
isChunkCacheEnabled
public static boolean isChunkCacheEnabled() -
getWindowSize
public int getWindowSize() -
setWindowSize
public void setWindowSize(int windowSize) Sets the window size. Must be called before any reading or seeking occurs.
In general, a smaller size means more requests may be needed, and a larger size means more irrelevant data will have to be read and discarded. The optimal size will vary depending on the source image, the amount of data needed from it, and network transfer rate vs. latency. The size should probably always be at least a few KB so as to be able to read the image header in one request.
- Parameters:
windowSize
- Window/chunk size.
-
close
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Specified by:
close
in interfaceImageInputStream
- Overrides:
close
in classImageInputStreamImpl
- Throws:
IOException
-
flushBefore
Invalidates any cached data lying entirely before the stream position.- Specified by:
flushBefore
in interfaceImageInputStream
- Overrides:
flushBefore
in classImageInputStreamImpl
- Throws:
IOException
-
length
public long length()- Specified by:
length
in interfaceImageInputStream
- Overrides:
length
in classImageInputStreamImpl
-
read
- Specified by:
read
in interfaceImageInputStream
- Specified by:
read
in classImageInputStreamImpl
- Throws:
IOException
-
read
- Specified by:
read
in interfaceImageInputStream
- Specified by:
read
in classImageInputStreamImpl
- Throws:
IOException
-
seek
- Specified by:
seek
in interfaceImageInputStream
- Overrides:
seek
in classImageInputStreamImpl
- Throws:
IOException
-