]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.runtime/src/org/simantics/scl/runtime/io/SclIO.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.scl.runtime / src / org / simantics / scl / runtime / io / SclIO.java
1 package org.simantics.scl.runtime.io;
2
3 import java.io.EOFException;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.io.OutputStream;
7 import java.nio.charset.Charset;
8
9 import gnu.trove.list.array.TByteArrayList;
10
11 public class SclIO {
12
13     private static final Charset UTF8 = Charset.forName("UTF-8");
14     
15     public static byte readByte(InputStream in) throws IOException {
16         int ch1 = in.read();
17         if(ch1 < 0)
18             throw new EOFException();
19         return (byte)ch1;
20     }
21     
22     public static boolean readBoolean(InputStream in) throws IOException {
23         int ch1 = in.read();
24         if(ch1 < 0)
25             throw new EOFException();
26         return ch1 != 0;
27     }
28     
29     public static char readCharacter(InputStream in) throws IOException {
30         int ch1 = in.read();
31         int ch2 = in.read();
32         if ((ch1 | ch2) < 0)
33             throw new EOFException();
34         return (char)((ch1 << 8) + ch2);
35     }
36     
37     public static short readShort(InputStream in) throws IOException {
38         int ch1 = in.read();
39         int ch2 = in.read();
40         if ((ch1 | ch2) < 0)
41             throw new EOFException();
42         return (short)((ch1 << 8) + ch2);
43     }
44     
45     public static int readInteger(InputStream in) throws IOException {
46         int ch1 = in.read();
47         int ch2 = in.read();
48         int ch3 = in.read();
49         int ch4 = in.read();
50         if ((ch1 | ch2 | ch3 | ch4) < 0)
51             throw new EOFException();
52         return (ch1 << 24) + (ch2 << 16) + (ch3 << 8) + ch4;
53     }
54
55     public static long readLong(InputStream in) throws IOException {
56         int ch1 = in.read();
57         int ch2 = in.read();
58         int ch3 = in.read();
59         int ch4 = in.read();
60         int ch5 = in.read();
61         int ch6 = in.read();
62         int ch7 = in.read();
63         int ch8 = in.read();
64         if ((ch1 | ch2 | ch3 | ch4 | ch5 | ch6 | ch7 | ch8) < 0)
65             throw new EOFException();
66         return ((long)ch1 << 56) +
67                 ((long)ch2 << 48) +
68                 ((long)ch3 << 40) +
69                 ((long)ch4 << 32) +
70                 ((long)ch5 << 24) +
71                 (ch6 << 16) +
72                 (ch7 <<  8) +
73                 ch8;
74     }
75
76     public static float readFloat(InputStream in) throws IOException {
77         return Float.intBitsToFloat(readInteger(in));
78     }
79
80     public static double readDouble(InputStream in) throws IOException {
81         return Double.longBitsToDouble(readLong(in));
82     }
83
84     public static int readLength(InputStream in) throws IOException {
85         int length = in.read()&0xff; 
86         if(length >= 0x80) {
87             if(length >= 0xc0) {
88                 if(length >= 0xe0) {
89                     if(length >= 0xf0) {
90                         length &= 0x0f;
91                         length += in.read()<<3;
92                         length += in.read()<<11;
93                         length += in.read()<<19;
94                         length += 0x10204080;
95                     }
96                     else {
97                         length &= 0x1f;
98                         length += in.read()<<4;
99                         length += in.read()<<12;
100                         length += in.read()<<20;
101                         length += 0x204080;
102                     }
103                 }
104                 else {
105                     length &= 0x3f;
106                     length += in.read()<<5;
107                     length += in.read()<<13;
108                     length += 0x4080;
109                 }
110             }
111             else {
112                 length &= 0x7f;
113                 length += in.read()<<6;
114                 length += 0x80;
115             }
116         }
117         return length;
118     }    
119
120     private static byte[] readFully(InputStream in, int length) throws IOException {
121         byte[] result = new byte[length];
122         int cur = 0;
123         while(cur < length) {
124             int c = in.read(result, cur, length-cur);
125             if(c <= 0)
126                 throw new EOFException();
127             cur += c;
128         }
129         return result;
130     }
131     
132     public static byte[] readByteArray(InputStream in) throws IOException {
133         int length = readLength(in);
134         return readFully(in, length);
135     }
136
137     public static double[] readDoubleArray(InputStream in) throws IOException {
138         int length = readLength(in);
139         double[] result = new double[length];
140         for(int i=0;i<length;++i)
141             result[i] = readDouble(in);
142         return result;
143     }
144     
145     public static String readString(InputStream in) throws IOException {
146         return new String(readByteArray(in), UTF8);
147     }
148     
149     public static byte[] readAllByteArrayAndClose(InputStream in) throws IOException {
150         try {
151             TByteArrayList l = new TByteArrayList();
152             byte[] buffer = new byte[1024];
153             while(true) {
154                 int c = in.read(buffer, 0, buffer.length);
155                 if(c <= 0)
156                     break;
157                 l.add(buffer, 0, c);
158             }
159             return l.toArray();
160         }
161         finally {
162             if(in != null)
163                 in.close();
164         }
165     }
166     
167     public static String readAllStringAndClose(InputStream in) throws IOException {
168         return new String(readAllByteArrayAndClose(in), UTF8);
169     }
170     
171     public static void writeByte(OutputStream out, byte v) throws IOException {
172         out.write((int)v);
173     }
174     
175     public static void writeBoolean(OutputStream out, boolean v) throws IOException {
176         out.write(v ? 1 : 0);
177     }
178     
179     public static void writeCharacter(OutputStream out, char v) throws IOException {
180         out.write((int)(v >>> 8));
181         out.write((int)(v));
182     }
183     
184     public static void writeShort(OutputStream out, short v) throws IOException {
185         out.write((int)(v >>> 8));
186         out.write((int)(v));
187     }
188     
189     public static void writeInteger(OutputStream out, int v) throws IOException {
190         out.write(v >>> 24);
191         out.write(v >>> 16);
192         out.write(v >>>  8);
193         out.write(v);
194     }
195     
196     public static void writeLong(OutputStream out, long v) throws IOException {
197         out.write((int)(v >>> 56));
198         out.write((int)(v >>> 48));
199         out.write((int)(v >>> 40));
200         out.write((int)(v >>> 32));        
201         out.write((int)(v >>> 24));
202         out.write((int)(v >>> 16));
203         out.write((int)(v >>>  8));
204         out.write((int)v);
205     }
206     
207     public static void writeFloat(OutputStream out, float v) throws IOException {
208         writeInteger(out, Float.floatToRawIntBits(v));
209     }
210     
211     public static void writeDouble(OutputStream out, double v) throws IOException {
212         writeLong(out, Double.doubleToRawLongBits(v));
213     }
214     
215     public static void writeLength(OutputStream out, int length) throws IOException {      
216         if(length < 0x80) {
217             out.write(length);
218         }
219         else {
220             length -= 0x80;
221             if(length < 0x4000) {
222                 out.write( ((length&0x3f) | 0x80) );
223                 out.write( (length>>>6) );
224             }
225             else {
226                 length -= 0x4000;
227                 if(length < 0x200000) {
228                     out.write( (length&0x1f) | 0xc0 );
229                     out.write( length>>>5 );
230                     out.write( length>>>13 );  
231                 }
232                 else {
233                     length -= 0x200000;
234                     if(length < 0x10000000) {
235                         out.write( (length&0x0f) | 0xe0 );
236                         out.write( length>>>4 );
237                         out.write( length>>>12 );  
238                         out.write( length>>>20 );
239                     }
240                     else {
241                         length -= 0x10000000;
242                         out.write( ((length&0x07) | 0xf0) );
243                         out.write( length>>>3 );
244                         out.write( length>>>11 );  
245                         out.write( length>>>19 );
246                         out.write( length>>>27 );
247                     }
248                 }               
249             }
250         }   
251     }
252     
253     public static void writeByteArray(OutputStream out, byte[] v) throws IOException {
254         writeLength(out, v.length);
255         out.write(v);
256     }
257     
258     public static void writeDoubleArray(OutputStream out, double[] v) throws IOException {
259         writeLength(out, v.length);
260         for(double s : v)
261             writeDouble(out, s);
262     }
263     
264     public static void writeString(OutputStream out, String v) throws IOException {
265         writeByteArray(out, v.getBytes(UTF8));
266     }
267
268     public static int ioSizeString(String v) {
269         return ioSizeLength(v.length()) + v.getBytes().length;
270     }
271     
272     public static int ioSizeLength(int length) {
273         if(length < 0x80)
274             return 1;
275         if(length < 0x4080)
276             return 2;
277         if(length < 0x204080)
278             return 3;
279         if(length < 0x10204080)
280             return 4;
281         return 5;
282     }
283 }