JaRMoS  1.1
Java Reduced Model Simulations
 All Classes Namespaces Files Functions Variables Enumerator Groups Pages
LittleEndianDataInput.java
Go to the documentation of this file.
1 package jarmos.io;
2 
3 import java.io.DataInput;
4 import java.io.DataInputStream;
5 import java.io.IOException;
6 import java.io.InputStream;
7 
17 public class LittleEndianDataInput implements DataInput {
18 
19  DataInputStream in;
20 
27  public LittleEndianDataInput(InputStream in) {
28  this.in = new DataInputStream(in);
29  }
30 
31  @Override
32  protected void finalize() {
33  try {
34  this.in.close();
35  } catch (IOException e) {
36  e.printStackTrace();
37  }
38  }
39 
40  // Read a float array
45  public float[] readFloat(int _size) throws IOException {
46  float[] ofloat = new float[_size];
47  int i = 0;
48  byte[] tmp = new byte[4 * _size];
49  for (i = 0; i < 4 * _size; i++)
50  tmp[i] = in.readByte();
51  int accum;
52  for (int count = 0; count < _size; count++) {
53  accum = 0;
54  i = 0;
55  for (int shiftBy = 0; shiftBy < 32; shiftBy += 8) {
56  accum |= ((long) (tmp[i + count * 4] & 0xff)) << shiftBy;
57  i++;
58  }
59  ofloat[count] = Float.intBitsToFloat(accum);
60  }
61  return ofloat;
62  }
63 
67  @Override
68  public void readFully(byte[] b) throws IOException {
69  throw new RuntimeException("Not implemented.");
70  }
71 
75  @Override
76  public void readFully(byte[] b, int off, int len) throws IOException {
77  throw new RuntimeException("Not implemented.");
78 
79  }
80 
84  @Override
85  public int skipBytes(int n) throws IOException {
86  throw new RuntimeException("Not implemented.");
87  }
88 
92  @Override
93  public boolean readBoolean() throws IOException {
94  throw new RuntimeException("Not implemented.");
95  }
96 
100  @Override
101  public byte readByte() throws IOException {
102  throw new RuntimeException("Not implemented.");
103  }
104 
108  @Override
109  public int readUnsignedByte() throws IOException {
110  throw new RuntimeException("Not implemented.");
111  }
112 
116  @Override
117  public short readShort() throws IOException {
118  throw new RuntimeException("Not implemented.");
119  }
120 
124  @Override
125  public int readUnsignedShort() throws IOException {
126  throw new RuntimeException("Not implemented.");
127  }
128 
132  @Override
133  public char readChar() throws IOException {
134  throw new RuntimeException("Not implemented.");
135  }
136 
140  @Override
141  public int readInt() throws IOException {
142  int i = 0;
143  byte[] tmp = new byte[2];
144  for (i = 0; i < 2; i++)
145  tmp[i] = in.readByte();
146  int low = tmp[0] & 0xff;
147  int high = tmp[1] & 0xff;
148  return (int) (high << 8 | low);
149  }
150 
154  @Override
155  public long readLong() throws IOException {
156  int i = 0;
157  byte[] tmp = new byte[4];
158  for (i = 0; i < 4; i++)
159  tmp[i] = in.readByte();
160  long accum = 0;
161  i = 0;
162  for (int shiftBy = 0; shiftBy < 32; shiftBy += 8) {
163  accum |= ((long) (tmp[i] & 0xff)) << shiftBy;
164  i++;
165  }
166  return accum;
167  }
168 
172  @Override
173  public float readFloat() throws IOException {
174  int i = 0;
175  byte[] tmp = new byte[4];
176  for (i = 0; i < 4; i++)
177  tmp[i] = in.readByte();
178  int accum = 0;
179  i = 0;
180  for (int shiftBy = 0; shiftBy < 32; shiftBy += 8) {
181  accum |= ((long) (tmp[i] & 0xff)) << shiftBy;
182  i++;
183  }
184  return Float.intBitsToFloat(accum);
185  }
186 
190  @Override
191  public double readDouble() throws IOException {
192  int i = 0;
193  byte[] tmp = new byte[8];
194  for (i = 0; i < 8; i++)
195  tmp[i] = in.readByte();
196  long accum = 0;
197  i = 0;
198  for (int shiftBy = 0; shiftBy < 64; shiftBy += 8) {
199  accum |= ((long) (tmp[i] & 0xff)) << shiftBy;
200  i++;
201  }
202  return Double.longBitsToDouble(accum);
203  }
204 
208  @Override
209  public String readLine() throws IOException {
210  throw new RuntimeException("Not implemented.");
211  }
212 
216  @Override
217  public String readUTF() throws IOException {
218  throw new RuntimeException("Not implemented.");
219  }
220 }
Wraps the old BinaryReader (rbappmit) into a DataInput.
LittleEndianDataInput(InputStream in)
Takes an InputStream instance pointing to a binary file.
void readFully(byte[] b, int off, int len)