SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
BinaryDataReader.java
1/*
2 * Class: BinaryDataReader
3 * Description: Binary data reader
4 * Environment: Java
5 * Software: SSJ
6 * Copyright (C) 2001 Pierre L'Ecuyer and Universite de Montreal
7 * Organization: DIRO, Universite de Montreal
8 * @author David Munger
9 * @since August 2009
10 *
11 *
12 * Licensed under the Apache License, Version 2.0 (the "License");
13 * you may not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
15 *
16 * http://www.apache.org/licenses/LICENSE-2.0
17 *
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
23 *
24 */
25package umontreal.ssj.util.io;
26
27import java.io.*;
28import java.net.URL;
29
37 protected DataInputStream in;
38
39 // only one of these will be used
40 protected String filename = null;
41 protected URL url = null;
42 protected File file = null;
43 protected boolean canReset = false;
44
49 protected String readLabel() throws IOException {
50
51 byte typechar = in.readByte();
52 if (typechar != BinaryDataWriter.TYPECHAR_LABEL)
53 throw new IOException("Expected a label");
54
55 return readStringData();
56 }
57
62 protected String readStringData() throws IOException {
63 int length = in.readInt();
64 if (length == 0)
65 return null;
66 byte[] s = new byte[length];
67 for (int i = 0; i < length; i++)
68 s[i] = in.readByte();
69 return new String(s);
70 }
71
76 protected String[] readStringArrayData(int dim) throws IOException {
77 String[] a = new String[dim];
78 for (int i = 0; i < dim; i++)
79 a[i] = readStringData();
80 return a;
81 }
82
87 protected String[][] readStringArray2DData(int dims[]) throws IOException {
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++)
91 a[i][j] = readStringData();
92 return a;
93 }
94
99 protected int readIntData() throws IOException {
100 return in.readInt();
101 }
102
107 protected int[] readIntArrayData(int dim) throws IOException {
108 int[] a = new int[dim];
109 for (int i = 0; i < dim; i++)
110 a[i] = readIntData();
111 return a;
112 }
113
118 protected int[][] readIntArray2DData(int dims[]) throws IOException {
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++)
122 a[i][j] = readIntData();
123 return a;
124 }
125
130 protected float readFloatData() throws IOException {
131 return in.readFloat();
132 }
133
138 protected float[] readFloatArrayData(int dim) throws IOException {
139 float[] a = new float[dim];
140 for (int i = 0; i < dim; i++)
141 a[i] = readFloatData();
142 return a;
143 }
144
149 protected float[][] readFloatArray2DData(int dims[]) throws IOException {
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++)
153 a[i][j] = readFloatData();
154 return a;
155 }
156
161 protected double readDoubleData() throws IOException {
162 return in.readDouble();
163 }
164
169 protected double[] readDoubleArrayData(int dim) throws IOException {
170 double[] a = new double[dim];
171 for (int i = 0; i < dim; i++)
172 a[i] = readDoubleData();
173 return a;
174 }
175
180 protected double[][] readDoubleArray2DData(int dims[]) throws IOException {
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++)
184 a[i][j] = readDoubleData();
185 return a;
186 }
187
197 protected Object readFieldData(byte typechar, int nDims, int dims[]) throws IOException {
198
199 if (nDims < 0 || nDims > 2)
200 throw new IOException("unsupported number of dimensions: " + nDims);
201
202 Object data = null;
203
204 switch (typechar) {
205
207 if (nDims == 0)
208 data = readStringData();
209 else if (nDims == 1)
210 data = readStringArrayData(dims[0]);
211 else if (nDims == 2)
212 data = readStringArray2DData(dims);
213 break;
214
216 if (nDims == 0)
217 data = readIntData();
218 else if (nDims == 1)
219 data = readIntArrayData(dims[0]);
220 else if (nDims == 2)
221 data = readIntArray2DData(dims);
222 break;
223
225 if (nDims == 0)
226 data = readFloatData();
227 else if (nDims == 1)
228 data = readFloatArrayData(dims[0]);
229 else if (nDims == 2)
230 data = readFloatArray2DData(dims);
231 break;
232
234 if (nDims == 0)
235 data = readDoubleData();
236 else if (nDims == 1)
237 data = readDoubleArrayData(dims[0]);
238 else if (nDims == 2)
239 data = readDoubleArray2DData(dims);
240 break;
241 }
242
243 return data;
244 }
245
251 public BinaryDataReader(String filename) throws IOException {
252 this.filename = filename;
253 canReset = true;
254 reset();
255 }
256
262 public BinaryDataReader(URL url) throws IOException {
263 this.url = url;
264 canReset = true;
265 reset();
266 }
267
273 public BinaryDataReader(File file) throws IOException {
274 this.file = file;
275 canReset = true;
276 reset();
277 }
278
286 public BinaryDataReader(InputStream inputStream) throws IOException {
287 this.in = new DataInputStream(inputStream);
288 }
289
293
299 public DataField readNextField() throws IOException {
300
301 String label = readLabel();
302
303 byte typechar = in.readByte();
304
305 int nDims = in.readByte();
306
307 int[] dims = new int[nDims];
308 for (int i = 0; i < nDims; i++)
309 dims[i] = in.readInt();
310
311 return new DataField(label, readFieldData(typechar, nDims, dims));
312 }
313
319 public DataField readField(String label) throws IOException {
320
321 reset();
322
323 while (in.available() > 0) {
324
325 String fieldLabel = readLabel();
326
327 byte typechar = in.readByte();
328
329 int nDims = in.readByte();
330 int[] dims = new int[nDims];
331 for (int i = 0; i < nDims; i++)
332 dims[i] = in.readInt();
333
334 // if found, return field instance
335 if (fieldLabel.compareTo(label) == 0)
336 return new DataField(fieldLabel, readFieldData(typechar, nDims, dims));
337
338 // otherwise, just skip the current field
339
340 // strings don't have a fixed size, so just read them out
341 if (typechar == BinaryDataWriter.TYPECHAR_STRING) {
342 readFieldData(typechar, nDims, dims);
343 continue;
344 }
345
346 // compute the number of bytes to be skipped
347 int skipSize = 0;
348 switch (typechar) {
349
351 skipSize = Integer.SIZE / 8;
352 break;
353
355 skipSize = Float.SIZE / 8;
356 break;
357
359 skipSize = Double.SIZE / 8;
360 break;
361 }
362
363 if (nDims > 0)
364 skipSize *= dims[0];
365 if (nDims > 1)
366 skipSize *= dims[1];
367
368 in.skipBytes(skipSize);
369 }
370
371 return null;
372 }
373
377
381
386 public void reset() throws IOException {
387 if (!canReset)
388 return;
389
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());
396 }
397
401 public boolean dataPending() throws IOException {
402 return (in.available() > 0);
403 }
404
408 public void close() throws IOException {
409 in.close();
410 }
411
412}
413
This abstract class implements shared functionality for data readers.
int[] readIntArrayData(int dim)
Reads integer-array data.
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).
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.
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...