source: proiecte/SolarSim/Java/SolarSim/src/solarsim/io/MyInputStreamReader.java @ 152

Last change on this file since 152 was 152, checked in by (none), 14 years ago
File size: 4.2 KB
Line 
1package solarsim.io;
2
3import solarsim.common.Vector;
4import java.io.IOException;
5import java.io.InputStream;
6
7/**
8 * Clasa Wrapper pentru InputStream care permite citirea de valori care au fost scrise de programul in C
9 * @author Marius Ion
10 */
11public class MyInputStreamReader {
12
13    private InputStream in;
14
15    public MyInputStreamReader(InputStream in) {
16        this.in = in;
17    }
18
19    /**
20     * Citeste un double scris in binar (8 octeti) din streamul de intrare
21     * @return numarul citit citit
22     * @throws IOException cand s-a produs o eroare la citirea din InputStream-ul incapsulat
23     */
24    public double readDouble() throws IOException {
25        byte [] buf = new byte[8];
26        while( in.available() < 8 )
27            try {
28                Thread.currentThread().sleep(1000);
29            } catch (InterruptedException ex) {}
30        in.read(buf, 0, 8);
31        return arr2double(buf, 0);
32    }
33
34    /**
35     * Citeste un float scris in binar (4 octeti) din streamul de intrare
36     * @return numarul citit citit
37     * @throws IOException cand s-a produs o eroare la citirea din InputStream-ul incapsulat
38     */
39    public double readFloat() throws IOException {
40        byte [] buf = new byte[4];
41        while( in.available() < 4 )
42            try {
43                Thread.currentThread().sleep(1000);
44            } catch (InterruptedException ex) {}
45        in.read(buf, 0, 4);
46        return arr2float(buf, 0);
47    }
48
49    /**
50     * Citeste un int scris in binar (4 octeti) din streamul de intrare
51     * @return numarul citit
52     * @throws IOException cand s-a produs o eroare la citirea din InputStream-ul incapsulat
53     */
54    public int readInt() throws IOException {
55        byte [] buf = new byte[4];
56        while( in.available() < 4 )
57            try {
58                Thread.currentThread().sleep(1000);
59            } catch (InterruptedException ex) {}
60        in.read(buf, 0, 4);
61        return arr2int(buf);
62    }
63
64    /**
65     * Citeste un Vector scris in binar (3 double) din streamul de intrare
66     * @return Vectorul citit
67     * @throws IOException cand s-a produs o eroare la citirea din InputStream-ul incapsulat
68     */
69    public Vector readVector() throws IOException {
70        Vector v = new Vector();
71        v.x = readDouble();
72        v.y = readDouble();
73        v.z = readDouble();
74        readDouble();
75        return v;
76    }
77
78    /**
79     * Citeste un Vector scris in binar (3 float) din streamul de intrare
80     * @return Vectorul citit
81     * @throws IOException cand s-a produs o eroare la citirea din InputStream-ul incapsulat
82     */
83    public Vector readFloatVector() throws IOException {
84        Vector v = new Vector();
85        v.x = (double)readFloat();
86        v.y = (double)readFloat();
87        v.z = (double)readFloat();
88        readFloat();
89        return v;
90    }
91
92    private int arr2int (byte[] arr) {
93        int i = 0;
94        int len = 4;
95        int cnt = 0;
96        byte[] tmp = new byte[len];
97        for (i = 0; i < len; i++) {
98            tmp[cnt] = arr[i];
99            cnt++;
100        }
101        int accum = 0;
102        i = 0;
103        for ( int shiftBy = 0; shiftBy < 32; shiftBy += 8 ) {
104            accum |= ( (int)( tmp[i] & 0xff ) ) << shiftBy;
105            i++;
106        }
107        return accum;
108    }
109
110    private double arr2float (byte[] arr, int start) {
111        int i = 0;
112        int len = 4;
113        int cnt = 0;
114        byte[] tmp = new byte[len];
115        for (i = start; i < (start + len); i++) {
116            tmp[cnt] = arr[i];
117            cnt++;
118        }
119        int accum = 0;
120        i = 0;
121        for ( int shiftBy = 0; shiftBy < 32; shiftBy += 8 ) {
122            accum |= ( (long)( tmp[i] & 0xff ) ) << shiftBy;
123            i++;
124        }
125        return Float.intBitsToFloat(accum);
126    }
127
128    private double arr2double (byte[] arr, int start) {
129        int i = 0;
130        int len = 8;
131        int cnt = 0;
132        byte[] tmp = new byte[len];
133        for (i = start; i < (start + len); i++) {
134            tmp[cnt] = arr[i];
135            cnt++;
136        }
137        long accum = 0;
138        i = 0;
139        for ( int shiftBy = 0; shiftBy < 64; shiftBy += 8 ) {
140            accum |= ( (long)( tmp[i] & 0xff ) ) << shiftBy;
141            i++;
142        }
143        return Double.longBitsToDouble(accum);
144    }
145
146}
Note: See TracBrowser for help on using the repository browser.