Class EncapsulatedInputStream
- All Implemented Interfaces:
Closeable
,AutoCloseable
A class that extends InputStream
by adding
a mechanism for unencapsulating an undefined length DICOM attribute, such as is used for
compressed Pixel Data.
The read methods hide the fact that the data is encapsulated by removing the Items and Item and Sequence delimiter tags, as well as skipping any Basic Offset Table that may be present in the first Item.
Since an individual frame may be fragmented and padded beyond the JPEG EOI marker (0xffd9), and since the codec used for decoding may be "reading ahead" this class also removes any padding bytes at the end of any fragment, back as far as the EOI marker. Note that this means that theoretically frames could span fragments as long as there was no padding between them.
-
Constructor Summary
ConstructorsConstructorDescriptionConstruct a byte ordered stream from the supplied stream.EncapsulatedInputStream
(BinaryInputStream i, boolean jpegEOIDetection, boolean oneFragmentPerFrame) Construct a byte ordered stream from the supplied stream. -
Method Summary
Modifier and TypeMethodDescriptionboolean
Are frames separated (seperable)?long[]
getByteOffsetsOfItemsForCurrentFrame
(long startOfPixelData) protected long
long[]
int
void
Skip to the start of the first fragment for a frame, if not already there.final int
read()
Extracts the next byte of data from the current or subsequent fragments.final int
read
(byte[] b) Extractsbyte.length
bytes of data from the current or subsequent fragments.final int
read
(byte[] b, int off, int len) Extractslen
bytes of data from the current or subsequent fragments.void
final void
readUnsigned16
(short[] w, int offset, int len) Read an array of unsigned integer 16 bit values.final long
Read an unsigned integer 32 bit values little endian regardless of endianness of stream.final void
readUnsigned32LittleEndian
(long[] w, int offset, int len) Read an array of unsigned integer 32 bit values little endian regardless of endianness of stream.void
skipInsistently
(long length) Skip as many bytes as requested, unless an exception occurs.Methods inherited from class java.io.InputStream
available, close, mark, markSupported, nullInputStream, readAllBytes, readNBytes, readNBytes, reset, skip, skipNBytes, transferTo
-
Constructor Details
-
EncapsulatedInputStream
public EncapsulatedInputStream(BinaryInputStream i, boolean jpegEOIDetection, boolean oneFragmentPerFrame) Construct a byte ordered stream from the supplied stream.
The byte order may be changed later.
- Parameters:
i
- the input stream to read fromjpegEOIDetection
- whether or not to detect JPEG EOI, to allow frames to span fragmentsoneFragmentPerFrame
- if not using jpegEOIDetection, whether or not to assume one fragment per frame (e.g., for RLE), rather than all fragments in one frame (e.g., for MPEG)
-
EncapsulatedInputStream
Construct a byte ordered stream from the supplied stream.
The byte order may be changed later.
JPEG EOI detection is enabled, to allow frames to span fragments.
Do not use for RLE encapsulated data, since it will assume all fragments are in one frame, rather than one fragment per frame.
- Parameters:
i
- the input stream to read from
-
-
Method Details
-
getBytesRead
protected long getBytesRead() -
getByteOffsetsOfItemsForCurrentFrame
public long[] getByteOffsetsOfItemsForCurrentFrame(long startOfPixelData) -
getItemLengthsForCurrentFrame
public long[] getItemLengthsForCurrentFrame() -
readSequenceDelimiter
- Throws:
IOException
- if an I/O error occurs
-
nextFrame
public void nextFrame()Skip to the start of the first fragment for a frame, if not already there.
-
readUnsigned16
Read an array of unsigned integer 16 bit values.
- Parameters:
w
- an array of sufficient size in which to return the values readoffset
- the offset in the array at which to begin storing valueslen
- the number of 16 bit values to read- Throws:
IOException
- if an I/O error occurs
-
readUnsigned32LittleEndian
Read an unsigned integer 32 bit values little endian regardless of endianness of stream.
- Returns:
- the value read
- Throws:
IOException
- if an I/O error occurs
-
readUnsigned32LittleEndian
Read an array of unsigned integer 32 bit values little endian regardless of endianness of stream.
- Parameters:
w
- an array of sufficient size in which to return the values readoffset
- the offset in the array at which to begin storing valueslen
- the number of 32 bit values to read- Throws:
IOException
- if an I/O error occurs
-
skipInsistently
Skip as many bytes as requested, unless an exception occurs.
- Parameters:
length
- number of bytes to read (no more and no less)- Throws:
IOException
- if an I/O error occurs
-
read
Extracts the next byte of data from the current or subsequent fragments.
- Specified by:
read
in classInputStream
- Returns:
- the next byte of data, or -1 if there is no more data because the end of the stream has been reached.
- Throws:
IOException
- if an I/O error occurs.
-
read
Extracts
byte.length
bytes of data from the current or subsequent fragments.This method simply performs the call
read(b, 0, b.length)
and returns the result.- Overrides:
read
in classInputStream
- Parameters:
b
- the buffer into which the data is read.- Returns:
- the total number of bytes read into the buffer (always whatever was asked for), or -1 if there is no more data because the end of the stream has been reached.
- Throws:
IOException
- if an I/O error occurs.- See Also:
-
read
Extracts
len
bytes of data from the current or subsequent fragments.- Overrides:
read
in classInputStream
- Parameters:
b
- the buffer into which the data is read.off
- the start offset of the data.len
- the number of bytes read.- Returns:
- the total number of bytes read into the buffer (always whatever was asked for), or -1 if there is no more data because the end of a frame has been reached.
- Throws:
IOException
- if an I/O error occurs.
-
getOffsetOfNextByteToReadFromStartOfFragment
public int getOffsetOfNextByteToReadFromStartOfFragment() -
framesAreSeparated
public boolean framesAreSeparated()Are frames separated (seperable)?
This will be either because the stream contains recognizable end of frame markers (e.g., JPEG) or the Transfer Syntax specifies one fragment per frame (e.g., RLE).
- Returns:
- true if frames are separated rather than being lumped together in one byte stream/array
-