1 package org.simantics.scl.runtime.io;
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;
9 import gnu.trove.list.array.TByteArrayList;
13 private static final Charset UTF8 = Charset.forName("UTF-8");
15 public static byte readByte(InputStream in) throws IOException {
18 throw new EOFException();
22 public static boolean readBoolean(InputStream in) throws IOException {
25 throw new EOFException();
29 public static char readCharacter(InputStream in) throws IOException {
33 throw new EOFException();
34 return (char)((ch1 << 8) + ch2);
37 public static short readShort(InputStream in) throws IOException {
41 throw new EOFException();
42 return (short)((ch1 << 8) + ch2);
45 public static int readInteger(InputStream in) throws IOException {
50 if ((ch1 | ch2 | ch3 | ch4) < 0)
51 throw new EOFException();
52 return (ch1 << 24) + (ch2 << 16) + (ch3 << 8) + ch4;
55 public static long readLong(InputStream in) throws IOException {
64 if ((ch1 | ch2 | ch3 | ch4 | ch5 | ch6 | ch7 | ch8) < 0)
65 throw new EOFException();
66 return ((long)ch1 << 56) +
76 public static float readFloat(InputStream in) throws IOException {
77 return Float.intBitsToFloat(readInteger(in));
80 public static double readDouble(InputStream in) throws IOException {
81 return Double.longBitsToDouble(readLong(in));
84 public static int readLength(InputStream in) throws IOException {
85 int length = in.read()&0xff;
91 length += in.read()<<3;
92 length += in.read()<<11;
93 length += in.read()<<19;
98 length += in.read()<<4;
99 length += in.read()<<12;
100 length += in.read()<<20;
106 length += in.read()<<5;
107 length += in.read()<<13;
113 length += in.read()<<6;
120 private static byte[] readFully(InputStream in, int length) throws IOException {
121 byte[] result = new byte[length];
123 while(cur < length) {
124 int c = in.read(result, cur, length-cur);
126 throw new EOFException();
132 public static byte[] readByteArray(InputStream in) throws IOException {
133 int length = readLength(in);
134 return readFully(in, length);
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);
145 public static String readString(InputStream in) throws IOException {
146 return new String(readByteArray(in), UTF8);
149 public static byte[] readAllByteArrayAndClose(InputStream in) throws IOException {
151 TByteArrayList l = new TByteArrayList();
152 byte[] buffer = new byte[1024];
154 int c = in.read(buffer, 0, buffer.length);
167 public static String readAllStringAndClose(InputStream in) throws IOException {
168 return new String(readAllByteArrayAndClose(in), UTF8);
171 public static void writeByte(OutputStream out, byte v) throws IOException {
175 public static void writeBoolean(OutputStream out, boolean v) throws IOException {
176 out.write(v ? 1 : 0);
179 public static void writeCharacter(OutputStream out, char v) throws IOException {
180 out.write((int)(v >>> 8));
184 public static void writeShort(OutputStream out, short v) throws IOException {
185 out.write((int)(v >>> 8));
189 public static void writeInteger(OutputStream out, int v) throws IOException {
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));
207 public static void writeFloat(OutputStream out, float v) throws IOException {
208 writeInteger(out, Float.floatToRawIntBits(v));
211 public static void writeDouble(OutputStream out, double v) throws IOException {
212 writeLong(out, Double.doubleToRawLongBits(v));
215 public static void writeLength(OutputStream out, int length) throws IOException {
221 if(length < 0x4000) {
222 out.write( ((length&0x3f) | 0x80) );
223 out.write( (length>>>6) );
227 if(length < 0x200000) {
228 out.write( (length&0x1f) | 0xc0 );
229 out.write( length>>>5 );
230 out.write( length>>>13 );
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 );
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 );
253 public static void writeByteArray(OutputStream out, byte[] v) throws IOException {
254 writeLength(out, v.length);
258 public static void writeDoubleArray(OutputStream out, double[] v) throws IOException {
259 writeLength(out, v.length);
264 public static void writeString(OutputStream out, String v) throws IOException {
265 writeByteArray(out, v.getBytes(UTF8));
268 public static int ioSizeString(String v) {
269 return ioSizeLength(v.length()) + v.getBytes().length;
272 public static int ioSizeLength(int length) {
277 if(length < 0x204080)
279 if(length < 0x10204080)