JaRMoS  1.1
Java Reduced Model Simulations
 All Classes Namespaces Files Functions Variables Enumerator Groups Pages
MathObjectReader.java
Go to the documentation of this file.
1 package jarmos.io;
2 
3 import jarmos.MathFactory;
4 
5 import java.io.DataInput;
6 import java.io.DataInputStream;
7 import java.io.FileInputStream;
8 import java.io.FileNotFoundException;
9 import java.io.IOException;
10 import java.io.InputStream;
11 
12 import org.apache.commons.math.linear.MatrixIndexException;
13 import org.apache.commons.math.linear.RealMatrix;
14 import org.apache.commons.math.linear.RealVector;
15 
30 public class MathObjectReader {
31 
38  public enum MachineFormats {
41  }
42 
48  public class MathReaderException extends Exception {
49 
50  private static final long serialVersionUID = -3505742802789851382L;
51 
56  public MathReaderException(String msg, Exception inner) {
57  super(msg, inner);
58  }
59  }
60 
67 
74  private DataInput getDataInput(InputStream in) {
75  switch (MachineFormat) {
76  case BigEndian:
77  return new DataInputStream(in);
78  case LittleEndian:
79  return new LittleEndianDataInput(in);
80  default:
81  return null;
82  }
83  }
84 
94  public RealMatrix readMatrix(InputStream in) throws IOException {
95  return MathFactory.createRealMatrix(readRawDoubleMatrix(in));
96  }
97 
108  public RealMatrix readMatrix(String file) throws IOException, FileNotFoundException {
109  return readMatrix(new FileInputStream(file));
110  }
111 
112  private double[][] readRawDoubleMatrix(DataInput rd, int rows, int cols) throws MatrixIndexException, IOException {
113  double[][] res = new double[rows][];
114  for (int i = 0; i < rows; i++) {
115  res[i] = readRawDoubleVector(rd, cols);
116  }
117  return res;
118  }
119 
129  public double[][] readRawDoubleMatrix(InputStream in) throws IOException {
130  int rows = -1;
131  int cols = -1;
132  double[][] res = null;
133  try {
134  DataInput di = getDataInput(in);
135  rows = di.readInt();
136  cols = di.readInt();
137  res = readRawDoubleMatrix(di, rows, cols);
138  } finally {
139  in.close();
140  }
141  return res;
142  }
143 
156  public double[][] readRawDoubleMatrix(InputStream in, int rows, int cols) throws IOException {
157  return readRawDoubleMatrix(getDataInput(in), rows, cols);
158  }
159 
160  private double[] readRawDoubleVector(DataInput rd, int size) throws MatrixIndexException, IOException {
161  double[] res = new double[size];
162  for (int i = 0; i < size; i++) {
163  res[i] = rd.readDouble();
164  }
165  return res;
166  }
167 
177  public double[] readRawDoubleVector(InputStream in) throws IOException {
178  double[] res = null;
179  try {
180  DataInput di = getDataInput(in);
181  res = readRawDoubleVector(di, di.readInt());
182  } finally {
183  in.close();
184  }
185  return res;
186  }
187 
188  private float[] readRawFloatVector(DataInput di, int size) throws IOException {
189  float[] res = new float[size];
190  for (int i = 0; i < size; i++) {
191  res[i] = di.readFloat();
192  }
193  return res;
194  }
195 
196  public float[] readRawFloatVector(InputStream in) throws IOException {
197  float[] res = null;
198  try {
199  DataInput di = getDataInput(in);
200  res = readRawFloatVector(in, di.readInt());
201  } finally {
202  in.close();
203  }
204  return res;
205  }
206 
218  public float[] readRawFloatVector(InputStream in, int size) throws IOException {
219  return readRawFloatVector(getDataInput(in), size);
220  }
221 
232  public RealVector readVector(InputStream in) throws IOException {
233  return MathFactory.createRealVector(readRawDoubleVector(in));
234  }
235 
246  public short[] readRawShortVector(InputStream in) throws IOException {
247  short[] res = null;
248  try {
249  DataInput di = getDataInput(in);
250  res = readRawShortVector(di, di.readInt());
251  } finally {
252  in.close();
253  }
254  return res;
255  }
256 
257  private short[] readRawShortVector(DataInput di, int size) throws IOException {
258  short[] res = new short[size];
259  for (int i = 0; i < size; i++) {
260  res[i] = di.readShort();
261  }
262  return res;
263  }
264 
274  public RealVector readVector(String filename) throws FileNotFoundException, IOException {
275  return readVector(new FileInputStream(filename));
276  }
277 
278 }
Factory method to create new RealMatrix instances.
float[] readRawFloatVector(InputStream in, int size)
This method exists due to compatibility with the old rbAppMIT models.
short[] readRawShortVector(InputStream in)
Reads a short array/vector from the given input stream, autodetecting its size from the first bytes r...
Wraps the old BinaryReader (rbappmit) into a DataInput.
RealVector readVector(String filename)
Reads a real vector including dimension from a given binary file.
double[][] readRawDoubleMatrix(InputStream in, int rows, int cols)
Reads a double matrix of size rows*cols from the input stream.
float[] readRawFloatVector(InputStream in)
Reading matrices and vectors with a bunch of convenient overloads for different sources and output fo...
double[] readRawDoubleVector(InputStream in)
Reads a real vector from a binary input stream, including dimension detection.
RealMatrix readMatrix(InputStream in)
Reads a matrix from an InputStream, pointing to a binary file.
Enum for both known machine formats.
MathReaderException(String msg, Exception inner)
RealVector readVector(InputStream in)
Reads a real vector including dimension from a given input stream which points to a binary file...
double[][] readRawDoubleMatrix(InputStream in)
Reads a real matrix as double[][] array from a given binary input stream, including dimension detecti...
RealMatrix readMatrix(String file)
Reads a matrix from a given binary file in the file system (accessible via java.io, i.e.
MachineFormats MachineFormat
Determines which machine format to use.