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