Class BufferedDataInputStream
- All Implemented Interfaces:
Closeable
,DataInput
,AutoCloseable
,ArrayDataInput
,FitsIO
Many new read calls are added to allow efficient reading off array data. The read(Object o) call provides for reading a primitive array of arbitrary type or dimensionality. There are also reads for each type of one dimensional array.
Note that there is substantial duplication of code to minimize method invocations. E.g., the floating point read routines read the data as integer values and then convert to float. However the integer code is duplicated rather than invoked. There has been considerable effort expended to ensure that these routines are efficient, but they could easily be superceded if an efficient underlying I/O package were ever delivered as part of the basic Java libraries. [This has subsequently happened with the NIO package and in an ideal universe these classes would be rewritten to take advantage of NIO.]
Testing and timing routines are provided in the nom.tam.util.test.BufferedFileTester class. Version 1.1: October 12, 2000: Fixed handling of EOF to return partially read arrays when EOF is detected. Version 1.2: July 20, 2009: Added handling of very large Object arrays. Additional work is required to handle very large arrays generally.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final BufferDecoder
private static final Logger
private final BufferPointer
private static final int
size of the skip buffer (if it exists)skipBuf
.private byte[]
Skip the requested number of bytes.Fields inherited from class java.io.FilterInputStream
in
Fields inherited from interface nom.tam.util.FitsIO
BITS_OF_1_BYTE, BITS_OF_2_BYTES, BITS_OF_3_BYTES, BITS_OF_4_BYTES, BITS_OF_5_BYTES, BITS_OF_6_BYTES, BITS_OF_7_BYTES, BYTE_1_OF_LONG_MASK, BYTE_2_OF_LONG_MASK, BYTE_3_OF_LONG_MASK, BYTE_4_OF_LONG_MASK, BYTE_MASK, BYTES_IN_BOOLEAN, BYTES_IN_BYTE, BYTES_IN_CHAR, BYTES_IN_DOUBLE, BYTES_IN_FLOAT, BYTES_IN_INTEGER, BYTES_IN_LONG, BYTES_IN_SHORT, DEFAULT_BUFFER_SIZE, HIGH_INTEGER_MASK, INTEGER_MASK, SHORT_MASK, SHORT_OF_LONG_MASK
-
Constructor Summary
ConstructorsConstructorDescriptionCreate a BufferedInputStream based on an input stream.BufferedDataInputStream
(InputStream o, int bufLength) Create a BufferedInputStream based on a input stream with a specified buffer size. -
Method Summary
Modifier and TypeMethodDescriptionprivate int
eofCheck
(EOFException e, int i, int start, int length) For array reads return an EOF if unable to read any data.private long
handleExceptionInSkip
(long skip, IOException e) int
read
(boolean[] b) Read an array of boolean's.int
read
(boolean[] b, int start, int length) Read a segment of an array of boolean's.int
read
(byte[] obuf, int offset, int length) Read a segment of an array of byte's.int
read
(char[] c) Read an array of char's.int
read
(char[] c, int start, int length) Read a segment of an array of char's.int
read
(double[] d) Read an array of double's.int
read
(double[] d, int start, int length) Read a segment of an array of double's.int
read
(float[] f) Read an array of float's.int
read
(float[] f, int start, int length) Read a segment of an array of float's.int
read
(int[] i) Read an array of int's.int
read
(int[] i, int start, int length) Read a segment of an array of int's.int
read
(long[] l) Read a segment of an array of long's.int
read
(long[] l, int start, int length) Read a segment of an array of long's.int
read
(short[] s) Read an array of short's.int
read
(short[] s, int start, int length) Read a segment of an array of short's.int
Deprecated.boolean
byte
readByte()
private void
readBytesIntoSharedBuffer
(int bytes) char
readChar()
double
float
void
readFully
(byte[] b) void
readFully
(byte[] b, int off, int len) Read a buffer and signal an EOF if the requested elements cannot be read.int
readInt()
long
readLArray
(Object o) Read an object.readLine()
Deprecated.long
readLong()
int
Deprecated.usereadLArray(Object)
insteadshort
int
int
readUTF()
void
skipAllBytes
(int toSkip) Skip the number of bytes.void
skipAllBytes
(long toSkip) Skip the number of bytes.int
skipBytes
(int toSkip) toString()
Methods inherited from class java.io.BufferedInputStream
available, close, mark, markSupported, read, reset, skip
Methods inherited from class java.io.FilterInputStream
read
Methods inherited from class java.io.InputStream
nullInputStream, readAllBytes, readNBytes, readNBytes, skipNBytes, transferTo
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface nom.tam.util.ArrayDataInput
mark, read, reset, skip
-
Field Details
-
LOG
-
SKIP_BUFFER_SIZE
private static final int SKIP_BUFFER_SIZEsize of the skip buffer (if it exists)skipBuf
.- See Also:
-
bufferDecoder
-
skipBuf
private byte[] skipBufSkip the requested number of bytes. This differs from the skip call in that it takes an long argument and will throw an end of file if the full number of bytes cannot be skipped.
-
-
Constructor Details
-
BufferedDataInputStream
Create a BufferedInputStream based on an input stream.- Parameters:
o
- the input stream to use for reading.
-
BufferedDataInputStream
Create a BufferedInputStream based on a input stream with a specified buffer size.- Parameters:
o
- the input stream to use for reading.bufLength
- the buffer length to use.
-
-
Method Details
-
eofCheck
For array reads return an EOF if unable to read any data.- Parameters:
e
- the eof exception that happened.i
- the current indexstart
- the start indexlength
- the element length- Returns:
- the number of bytes read before the end of file exception.
- Throws:
EOFException
- if no extra bytes could be read
-
read
Description copied from interface:ArrayDataInput
Read an array of boolean's.- Specified by:
read
in interfaceArrayDataInput
- Parameters:
b
- array of boolean's.- Returns:
- number of bytes read.
- Throws:
IOException
- if one of the underlying read operations failed
-
read
Description copied from interface:ArrayDataInput
Read a segment of an array of boolean's.- Specified by:
read
in interfaceArrayDataInput
- Parameters:
b
- array of boolean's.start
- start index in the arraylength
- number of array elements to read- Returns:
- number of bytes read.
- Throws:
IOException
- if one of the underlying read operations failed
-
read
Description copied from interface:ArrayDataInput
Read a segment of an array of byte's.- Specified by:
read
in interfaceArrayDataInput
- Overrides:
read
in classBufferedInputStream
- Parameters:
obuf
- array of byte's.offset
- start index in the arraylength
- number of array elements to read- Returns:
- number of bytes read.
- Throws:
IOException
- if one of the underlying read operations failed- See Also:
-
read
Description copied from interface:ArrayDataInput
Read an array of char's.- Specified by:
read
in interfaceArrayDataInput
- Parameters:
c
- array of char's.- Returns:
- number of bytes read.
- Throws:
IOException
- if one of the underlying read operations failed
-
read
Description copied from interface:ArrayDataInput
Read a segment of an array of char's.- Specified by:
read
in interfaceArrayDataInput
- Parameters:
c
- array of char's.start
- start index in the arraylength
- number of array elements to read- Returns:
- number of bytes read.
- Throws:
IOException
- if one of the underlying read operations failed
-
read
Description copied from interface:ArrayDataInput
Read an array of double's.- Specified by:
read
in interfaceArrayDataInput
- Parameters:
d
- array of double's.- Returns:
- number of bytes read.
- Throws:
IOException
- if one of the underlying read operations failed
-
read
Description copied from interface:ArrayDataInput
Read a segment of an array of double's.- Specified by:
read
in interfaceArrayDataInput
- Parameters:
d
- array of double's.start
- start index in the arraylength
- number of array elements to read- Returns:
- number of bytes read.
- Throws:
IOException
- if one of the underlying read operations failed
-
read
Description copied from interface:ArrayDataInput
Read an array of float's.- Specified by:
read
in interfaceArrayDataInput
- Parameters:
f
- array of float's.- Returns:
- number of bytes read.
- Throws:
IOException
- if one of the underlying read operations failed
-
read
Description copied from interface:ArrayDataInput
Read a segment of an array of float's.- Specified by:
read
in interfaceArrayDataInput
- Parameters:
f
- array of float's.start
- start index in the arraylength
- number of array elements to read- Returns:
- number of bytes read.
- Throws:
IOException
- if one of the underlying read operations failed
-
read
Description copied from interface:ArrayDataInput
Read an array of int's.- Specified by:
read
in interfaceArrayDataInput
- Parameters:
i
- array of int's.- Returns:
- number of bytes read.
- Throws:
IOException
- if one of the underlying read operations failed
-
read
Description copied from interface:ArrayDataInput
Read a segment of an array of int's.- Specified by:
read
in interfaceArrayDataInput
- Parameters:
i
- array of int's.start
- start index in the arraylength
- number of array elements to read- Returns:
- number of bytes read.
- Throws:
IOException
- if one of the underlying read operations failed
-
read
Description copied from interface:ArrayDataInput
Read a segment of an array of long's.- Specified by:
read
in interfaceArrayDataInput
- Parameters:
l
- array of long's.- Returns:
- number of bytes read.
- Throws:
IOException
- if one of the underlying read operations failed
-
read
Description copied from interface:ArrayDataInput
Read a segment of an array of long's.- Specified by:
read
in interfaceArrayDataInput
- Parameters:
l
- array of long's.start
- start index in the arraylength
- number of array elements to read- Returns:
- number of bytes read.
- Throws:
IOException
- if one of the underlying read operations failed
-
read
Description copied from interface:ArrayDataInput
Read an array of short's.- Specified by:
read
in interfaceArrayDataInput
- Parameters:
s
- array of short's.- Returns:
- number of bytes read.
- Throws:
IOException
- if one of the underlying read operations failed
-
read
Description copied from interface:ArrayDataInput
Read a segment of an array of short's.- Specified by:
read
in interfaceArrayDataInput
- Parameters:
s
- array of short's.start
- start index in the arraylength
- number of array elements to read- Returns:
- number of bytes read.
- Throws:
IOException
- if one of the underlying read operations failed
-
readArray
Deprecated.Description copied from interface:ArrayDataInput
Read a generic (possibly multidimensional) primitive array. An Object[] array is also a legal argument if each element of the array is a legal.The ArrayDataInput classes do not support String input since it is unclear how one would read in an Array of strings.
- Specified by:
readArray
in interfaceArrayDataInput
- Parameters:
o
- A [multidimensional] primitive (or Object) array.- Returns:
- number of bytes read.
- Throws:
IOException
- if the underlying stream failed
-
readBoolean
- Specified by:
readBoolean
in interfaceDataInput
- Throws:
IOException
-
readByte
- Specified by:
readByte
in interfaceDataInput
- Throws:
IOException
-
readChar
- Specified by:
readChar
in interfaceDataInput
- Throws:
IOException
-
readDouble
- Specified by:
readDouble
in interfaceDataInput
- Throws:
IOException
-
readFloat
- Specified by:
readFloat
in interfaceDataInput
- Throws:
IOException
-
readFully
- Specified by:
readFully
in interfaceDataInput
- Throws:
IOException
-
readFully
Description copied from interface:ArrayDataInput
Read a buffer and signal an EOF if the requested elements cannot be read. This differs from read(b,off,len) since that call will not signal and end of file unless no bytes can be read. However both of these routines will attempt to fill their buffers completely.- Specified by:
readFully
in interfaceArrayDataInput
- Specified by:
readFully
in interfaceDataInput
- Parameters:
b
- The input buffer.off
- The requested offset into the buffer.len
- The number of bytes requested.- Throws:
IOException
-
readInt
- Specified by:
readInt
in interfaceDataInput
- Throws:
IOException
-
readLArray
Description copied from interface:ArrayDataInput
Read an object. An EOF will be signaled if the object cannot be fully read. This version works even if the underlying data is more than 2 Gigabytes.- Specified by:
readLArray
in interfaceArrayDataInput
- Parameters:
o
- The object to be read. This object should be a primitive (possibly multi-dimensional) array.- Returns:
- number of bytes read.
- Throws:
IOException
- if the underlying stream failed
-
readLine
Deprecated.UseBufferedReader
methods.Emulate the deprecated DataInputStream.readLine() method. Originally we used the method itself, but Alan Brighton suggested using a java.io.BufferedReader to eliminate the deprecation warning. This was used for a long time, but more recently we noted that this doesn't work. We now use a simple method that largely ignores character encodings and only uses the "\n" as the line separator. This method is slow regardless. In the current version- Specified by:
readLine
in interfaceDataInput
- Returns:
- The String read.
- Throws:
IOException
-
readLong
- Specified by:
readLong
in interfaceDataInput
- Throws:
IOException
-
readPrimitiveArray
Deprecated.usereadLArray(Object)
insteadThis routine provides efficient reading of arrays of any primitive type. It is an error to invoke this method with an object that is not an array of some primitive type. Note that there is no corresponding capability to writePrimitiveArray in BufferedDataOutputStream to read in an array of Strings.- Parameters:
o
- The object to be read. It must be an array of a primitive type, or an array of Object's.- Returns:
- number of bytes read.
- Throws:
IOException
- if the underlying read operation fails
-
readShort
- Specified by:
readShort
in interfaceDataInput
- Throws:
IOException
-
readUnsignedByte
- Specified by:
readUnsignedByte
in interfaceDataInput
- Throws:
IOException
-
readUnsignedShort
- Specified by:
readUnsignedShort
in interfaceDataInput
- Throws:
IOException
-
readUTF
- Specified by:
readUTF
in interfaceDataInput
- Throws:
IOException
-
skipAllBytes
Description copied from interface:ArrayDataInput
Skip the number of bytes. This differs from the skip method in that it will throw an EOF if a forward skip cannot be fully accomplished... (However that isn't supposed to happen with a random access file, so there is probably no operational difference).- Specified by:
skipAllBytes
in interfaceArrayDataInput
- Parameters:
toSkip
- the number of bytes to skip- Throws:
IOException
- if the underlying stream failed
-
skipAllBytes
Description copied from interface:ArrayDataInput
Skip the number of bytes. This differs from the skip method in that it will throw an EOF if a forward skip cannot be fully accomplished... (However that isn't supposed to happen with a random access file, so there is probably no operational difference).- Specified by:
skipAllBytes
in interfaceArrayDataInput
- Parameters:
toSkip
- the number of bytes to skip- Throws:
IOException
- if the underlying stream failed
-
handleExceptionInSkip
- Throws:
IOException
-
skipBytes
- Specified by:
skipBytes
in interfaceDataInput
- Throws:
IOException
-
toString
-
BufferedReader
methods.