1 package org.simantics.scl.runtime.io;
\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
9 import gnu.trove.list.array.TByteArrayList;
\r
11 public class SclIO {
\r
13 private static final Charset UTF8 = Charset.forName("UTF-8");
\r
15 public static byte readByte(InputStream in) throws IOException {
\r
16 int ch1 = in.read();
\r
18 throw new EOFException();
\r
22 public static boolean readBoolean(InputStream in) throws IOException {
\r
23 int ch1 = in.read();
\r
25 throw new EOFException();
\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
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
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
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
76 public static float readFloat(InputStream in) throws IOException {
\r
77 return Float.intBitsToFloat(readInteger(in));
\r
80 public static double readDouble(InputStream in) throws IOException {
\r
81 return Double.longBitsToDouble(readLong(in));
\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
91 length += in.read()<<3;
\r
92 length += in.read()<<11;
\r
93 length += in.read()<<19;
\r
94 length += 0x10204080;
\r
98 length += in.read()<<4;
\r
99 length += in.read()<<12;
\r
100 length += in.read()<<20;
\r
101 length += 0x204080;
\r
106 length += in.read()<<5;
\r
107 length += in.read()<<13;
\r
113 length += in.read()<<6;
\r
120 private static byte[] readFully(InputStream in, int length) throws IOException {
\r
121 byte[] result = new byte[length];
\r
123 while(cur < length) {
\r
124 int c = in.read(result, cur, length-cur);
\r
126 throw new EOFException();
\r
132 public static byte[] readByteArray(InputStream in) throws IOException {
\r
133 int length = readLength(in);
\r
134 return readFully(in, length);
\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
145 public static String readString(InputStream in) throws IOException {
\r
146 return new String(readByteArray(in), UTF8);
\r
149 public static byte[] readAllByteArrayAndClose(InputStream in) throws IOException {
\r
151 TByteArrayList l = new TByteArrayList();
\r
152 byte[] buffer = new byte[1024];
\r
154 int c = in.read(buffer, 0, buffer.length);
\r
157 l.add(buffer, 0, c);
\r
159 return l.toArray();
\r
167 public static String readAllStringAndClose(InputStream in) throws IOException {
\r
168 return new String(readAllByteArrayAndClose(in), UTF8);
\r
171 public static void writeByte(OutputStream out, byte v) throws IOException {
\r
175 public static void writeBoolean(OutputStream out, boolean v) throws IOException {
\r
176 out.write(v ? 1 : 0);
\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
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
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
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
207 public static void writeFloat(OutputStream out, float v) throws IOException {
\r
208 writeInteger(out, Float.floatToRawIntBits(v));
\r
211 public static void writeDouble(OutputStream out, double v) throws IOException {
\r
212 writeLong(out, Double.doubleToRawLongBits(v));
\r
215 public static void writeLength(OutputStream out, int length) throws IOException {
\r
216 if(length < 0x80) {
\r
221 if(length < 0x4000) {
\r
222 out.write( ((length&0x3f) | 0x80) );
\r
223 out.write( (length>>>6) );
\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
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
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
253 public static void writeByteArray(OutputStream out, byte[] v) throws IOException {
\r
254 writeLength(out, v.length);
\r
258 public static void writeDoubleArray(OutputStream out, double[] v) throws IOException {
\r
259 writeLength(out, v.length);
\r
261 writeDouble(out, s);
\r
264 public static void writeString(OutputStream out, String v) throws IOException {
\r
265 writeByteArray(out, v.getBytes(UTF8));
\r
268 public static int ioSizeString(String v) {
\r
269 return ioSizeLength(v.length()) + v.getBytes().length;
\r
272 public static int ioSizeLength(int length) {
\r
275 if(length < 0x4080)
\r
277 if(length < 0x204080)
\r
279 if(length < 0x10204080)
\r