25package umontreal.ssj.util.io;
37 protected DataInputStream in;
40 protected String filename =
null;
41 protected URL url =
null;
42 protected File file =
null;
43 protected boolean canReset =
false;
51 byte typechar = in.readByte();
53 throw new IOException(
"Expected a label");
63 int length = in.readInt();
66 byte[] s =
new byte[length];
67 for (
int i = 0; i < length; i++)
77 String[] a =
new String[dim];
78 for (
int i = 0; i < dim; i++)
88 String[][] a =
new String[dims[0]][dims[1]];
89 for (
int i = 0; i < dims[0]; i++)
90 for (
int j = 0; j < dims[1]; j++)
108 int[] a =
new int[dim];
109 for (
int i = 0; i < dim; i++)
119 int[][] a =
new int[dims[0]][dims[1]];
120 for (
int i = 0; i < dims[0]; i++)
121 for (
int j = 0; j < dims[1]; j++)
131 return in.readFloat();
139 float[] a =
new float[dim];
140 for (
int i = 0; i < dim; i++)
150 float[][] a =
new float[dims[0]][dims[1]];
151 for (
int i = 0; i < dims[0]; i++)
152 for (
int j = 0; j < dims[1]; j++)
162 return in.readDouble();
170 double[] a =
new double[dim];
171 for (
int i = 0; i < dim; i++)
181 double[][] a =
new double[dims[0]][dims[1]];
182 for (
int i = 0; i < dims[0]; i++)
183 for (
int j = 0; j < dims[1]; j++)
197 protected Object
readFieldData(
byte typechar,
int nDims,
int dims[])
throws IOException {
199 if (nDims < 0 || nDims > 2)
200 throw new IOException(
"unsupported number of dimensions: " + nDims);
252 this.filename = filename;
287 this.in =
new DataInputStream(inputStream);
303 byte typechar = in.readByte();
305 int nDims = in.readByte();
307 int[] dims =
new int[nDims];
308 for (
int i = 0; i < nDims; i++)
309 dims[i] = in.readInt();
323 while (in.available() > 0) {
327 byte typechar = in.readByte();
329 int nDims = in.readByte();
330 int[] dims =
new int[nDims];
331 for (
int i = 0; i < nDims; i++)
332 dims[i] = in.readInt();
335 if (fieldLabel.compareTo(label) == 0)
351 skipSize = Integer.SIZE / 8;
355 skipSize = Float.SIZE / 8;
359 skipSize = Double.SIZE / 8;
368 in.skipBytes(skipSize);
386 public void reset() throws IOException {
390 if (filename !=
null)
391 this.in =
new DataInputStream(
new FileInputStream(filename));
392 else if (file !=
null)
393 this.in =
new DataInputStream(
new FileInputStream(file));
394 else if (url !=
null)
395 this.in =
new DataInputStream(url.openStream());
402 return (in.available() > 0);
408 public void close() throws IOException {
This abstract class implements shared functionality for data readers.
int[] readIntArrayData(int dim)
Reads integer-array data.
void close()
Closes the file.
float[][] readFloatArray2DData(int dims[])
Reads 2D float-array data.
DataField readNextField()
Reads the next available field.
String[][] readStringArray2DData(int dims[])
Reads 2D string-array data.
boolean dataPending()
Returns true if there remains data to be read.
BinaryDataReader(String filename)
Opens the file with the specified name for reading.
void reset()
Reopens the file (does not work with the constructor that takes an input stream).
int readIntData()
Reads integer data.
String readLabel()
Reads current field label.
BinaryDataReader(InputStream inputStream)
Opens the specified input stream for reading.
float[] readFloatArrayData(int dim)
Reads float-array data.
BinaryDataReader(URL url)
Opens the file at the specified url for reading.
BinaryDataReader(File file)
Opens the specified file for reading.
double[] readDoubleArrayData(int dim)
Reads double-array data.
int[][] readIntArray2DData(int dims[])
Reads 2D integer-array data.
float readFloatData()
Reads float data.
double readDoubleData()
Reads double data.
String readStringData()
Reads string data.
String[] readStringArrayData(int dim)
Reads string-array data.
DataField readField(String label)
Reads the first field labeled as label.
double[][] readDoubleArray2DData(int dims[])
Reads 2D double-array data.
Object readFieldData(byte typechar, int nDims, int dims[])
Reads field data of arbitrary type.
static final byte TYPECHAR_DOUBLE
Field-type symbol indicating double data.
static final byte TYPECHAR_FLOAT
Field-type symbol indicating float data.
static final byte TYPECHAR_LABEL
Field-type symbol indicating a label (it more accurately a field separator symbol).
static final byte TYPECHAR_STRING
Field-type symbol indicating String data.
static final byte TYPECHAR_INTEGER
Field-type symbol indicating int data.
This class represents a data field from a file read by an instance of a class implementing DataReader...