SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
BinaryDataWriter.java
1/*
2 * Class: BinaryDataWriter
3 * Description: Binary data writer
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.*;
28
80 protected DataOutputStream out;
81
86 protected void writeStringData(String s) throws IOException {
87 if (s != null) {
88 out.writeInt(s.length());
89 out.writeBytes(s);
90 } else {
91 out.writeInt(0);
92 }
93 }
94
101 protected void writeLabel(String label) throws IOException {
102 out.writeByte(TYPECHAR_LABEL);
103 writeStringData(label);
104 }
105
109
114 public final static byte TYPECHAR_LABEL = '|';
115
119 public final static byte TYPECHAR_STRING = 'S';
120
124 public final static byte TYPECHAR_INTEGER = 'i';
125
129 public final static byte TYPECHAR_FLOAT = 'f';
130
134 public final static byte TYPECHAR_DOUBLE = 'd';
135
139
147 public BinaryDataWriter(String filename, boolean append) throws IOException {
148 this.out = new DataOutputStream(new FileOutputStream(filename, append));
149 }
150
158 public BinaryDataWriter(File file, boolean append) throws IOException {
159 this.out = new DataOutputStream(new FileOutputStream(file, append));
160 }
161
167 public BinaryDataWriter(String filename) throws IOException {
168 this.out = new DataOutputStream(new FileOutputStream(filename));
169 }
170
176 public BinaryDataWriter(File file) throws IOException {
177 this.out = new DataOutputStream(new FileOutputStream(file));
178 }
179
185 public BinaryDataWriter(OutputStream outputStream) throws IOException {
186 this.out = new DataOutputStream(outputStream);
187 }
188
192
197 public void write(String label, String s) throws IOException {
198 writeLabel(label);
199 out.writeByte(TYPECHAR_STRING);
200 out.writeByte(0);
202 }
203
208 public void write(String label, int a) throws IOException {
209 writeLabel(label);
210 out.writeByte(TYPECHAR_INTEGER);
211 out.writeByte(0);
212 out.writeInt(a);
213 }
214
219 public void write(String label, float a) throws IOException {
220 writeLabel(label);
221 out.writeByte(TYPECHAR_FLOAT);
222 out.writeByte(0);
223 out.writeFloat(a);
224 }
225
230 public void write(String label, double a) throws IOException {
231 writeLabel(label);
232 out.writeByte(TYPECHAR_DOUBLE);
233 out.writeByte(0);
234 out.writeDouble(a);
235 }
236
240
244
249 public void write(String label, String[] a, int n) throws IOException {
250 writeLabel(label);
251 out.writeByte(TYPECHAR_STRING);
252 out.writeByte(1);
253 out.writeInt(n);
254 for (int i = 0; i < n; i++)
255 writeStringData(a[i]);
256 }
257
262 public void write(String label, int[] a, int n) throws IOException {
263 writeLabel(label);
264 out.writeByte(TYPECHAR_INTEGER);
265 out.writeByte(1);
266 out.writeInt(n);
267 for (int i = 0; i < n; i++)
268 out.writeInt(a[i]);
269 }
270
275 public void write(String label, float[] a, int n) throws IOException {
276 writeLabel(label);
277 out.writeByte(TYPECHAR_FLOAT);
278 out.writeByte(1);
279 out.writeInt(n);
280 for (int i = 0; i < n; i++)
281 out.writeFloat(a[i]);
282 }
283
288 public void write(String label, double[] a, int n) throws IOException {
289 writeLabel(label);
290 out.writeByte(TYPECHAR_DOUBLE);
291 out.writeByte(1);
292 out.writeInt(n);
293 for (int i = 0; i < n; i++)
294 out.writeDouble(a[i]);
295 }
296
300
304
309 public void write(String label, String[][] a) throws IOException {
310 writeLabel(label);
311 out.writeByte(TYPECHAR_STRING);
312 out.writeByte(2);
313 out.writeInt(a.length);
314 out.writeInt(a[0].length);
315 for (int i = 0; i < a.length; i++)
316 for (int j = 0; j < a[i].length; j++)
317 writeStringData(a[i][j]);
318 }
319
324 public void write(String label, int[][] a) throws IOException {
325 writeLabel(label);
326 out.writeByte(TYPECHAR_INTEGER);
327 out.writeByte(2);
328 out.writeInt(a.length);
329 out.writeInt(a[0].length);
330 for (int i = 0; i < a.length; i++)
331 for (int j = 0; j < a[i].length; j++)
332 out.writeInt(a[i][j]);
333 }
334
339 public void write(String label, float[][] a) throws IOException {
340 writeLabel(label);
341 out.writeByte(TYPECHAR_FLOAT);
342 out.writeByte(2);
343 out.writeInt(a.length);
344 out.writeInt(a[0].length);
345 for (int i = 0; i < a.length; i++)
346 for (int j = 0; j < a[i].length; j++)
347 out.writeFloat(a[i][j]);
348 }
349
354 public void write(String label, double[][] a) throws IOException {
355 writeLabel(label);
356 out.writeByte(TYPECHAR_DOUBLE);
357 out.writeByte(2);
358 out.writeInt(a.length);
359 out.writeInt(a[0].length);
360 for (int i = 0; i < a.length; i++)
361 for (int j = 0; j < a[i].length; j++)
362 out.writeDouble(a[i][j]);
363 }
364
368
372
376 public void close() throws IOException {
377 out.close();
378 }
379
380}
381
This abstract class implements shared functionality for data writers.
static final byte TYPECHAR_DOUBLE
Field-type symbol indicating double data.
static final byte TYPECHAR_FLOAT
Field-type symbol indicating float data.
void write(String label, int a)
Writes an atomic 32-bit integer (big endian).
void write(String label, int[][] a)
Writes a two-dimensional array of 32-bit integers (big endian).
void write(String label, float[] a, int n)
Writes the first n elements of a one-dimensional array of 32-bit floats (big endian).
BinaryDataWriter(File file)
Truncates any existing file with the specified name.
void write(String label, String[] a, int n)
Writes the first n elements of a one-dimensional array of strings.
BinaryDataWriter(OutputStream outputStream)
Constructor.
BinaryDataWriter(String filename)
Truncates any existing file with the specified name.
void write(String label, String s)
Writes an atomic string field.
static final byte TYPECHAR_LABEL
Field-type symbol indicating a label (it more accurately a field separator symbol).
void close()
Flushes any pending data and closes the file.
void write(String label, float a)
Writes an atomic 32-bit float (big endian).
void write(String label, int[] a, int n)
Writes the first n elements of a one-dimensional array of 32-bit integers (big endian).
void writeLabel(String label)
Starts a new field by writing its label.
static final byte TYPECHAR_STRING
Field-type symbol indicating String data.
void write(String label, double[] a, int n)
Writes the first n elements of a one-dimensional array of 64-bit doubles (big endian).
void write(String label, String[][] a)
Writes a two-dimensional array of strings.
void write(String label, double[][] a)
Writes a two-dimensional array of 64-bit doubles (big endian).
void writeStringData(String s)
Utility method to write string data.
static final byte TYPECHAR_INTEGER
Field-type symbol indicating int data.
void write(String label, float[][] a)
Writes a two-dimensional array of 32-bit floats (big endian).
BinaryDataWriter(File file, boolean append)
Data will be output to the specified file.
void write(String label, double a)
Writes an atomic 64-bit double (big endian).
BinaryDataWriter(String filename, boolean append)
Data will be output to the file with the specified name.