Merge branch 'master' of https://source.jalview.org/git/jalviewjs.git
[jalviewjs.git] / site / j2s / java / io / DataInputStream.js
1 Clazz.load (["java.io.DataInput", "$.FilterInputStream"], "java.io.DataInputStream", ["java.io.EOFException", "$.PushbackInputStream", "$.UTFDataFormatException", "java.lang.Double", "$.Float", "$.IndexOutOfBoundsException"], function () {
2 c$ = Clazz.decorateAsClass (function () {
3 this.bytearr = null;
4 this.chararr = null;
5 this.readBuffer = null;
6 this.lineBuffer = null;
7 Clazz.instantialize (this, arguments);
8 }, java.io, "DataInputStream", java.io.FilterInputStream, java.io.DataInput);
9 Clazz.prepareFields (c$, function () {
10 this.bytearr =  Clazz.newByteArray (80, 0);
11 this.chararr =  Clazz.newCharArray (80, '\0');
12 this.readBuffer =  Clazz.newByteArray (8, 0);
13 });
14 Clazz.overrideMethod (c$, "read", 
15 function (b, off, len) {
16     if (arguments.length == 1) { off = 0; len = b.length; }
17 return this.$in.read (b, off, len);
18 }, "~A,~N,~N");
19 Clazz.defineMethod (c$, "readFully", 
20 function (b, off, len) {
21 if (len < 0) throw  new IndexOutOfBoundsException ();
22 var n = 0;
23 while (n < len) {
24 var count = this.$in.read (b, off + n, len - n);
25 if (count < 0) throw  new java.io.EOFException ();
26 n += count;
27 }
28 }, "~A,~N,~N");
29 Clazz.overrideMethod (c$, "skipBytes", 
30 function (n) {
31 var total = 0;
32 var cur = 0;
33 while ((total < n) && ((cur = this.$in.skip (n - total)) > 0)) {
34 total += cur;
35 }
36 return total;
37 }, "~N");
38 Clazz.overrideMethod (c$, "readBoolean", 
39 function () {
40 var ch = this.$in.readByteAsInt ();
41 if (ch < 0) throw  new java.io.EOFException ();
42 return (ch != 0);
43 });
44 Clazz.overrideMethod (c$, "readByte", 
45 function () {
46 var ch = this.$in.readByteAsInt ();
47 if (ch < 0) throw  new java.io.EOFException ();
48 return (ch);
49 });
50 Clazz.overrideMethod (c$, "readUnsignedByte", 
51 function () {
52 var ch = this.$in.readByteAsInt ();
53 if (ch < 0) throw  new java.io.EOFException ();
54 return ch;
55 });
56 Clazz.overrideMethod (c$, "readShort", 
57 function () {
58 var ch1 = this.$in.readByteAsInt ();
59 var ch2 = this.$in.readByteAsInt ();
60 if ((ch1 | ch2) < 0) throw  new java.io.EOFException ();
61 var n = ((ch1 << 8) + (ch2 << 0));
62 {
63 return (n > 0x7FFF ? n - 0x10000 : n);
64 }});
65 Clazz.defineMethod (c$, "readUnsignedShort", 
66 function () {
67 var ch1 = this.$in.readByteAsInt ();
68 var ch2 = this.$in.readByteAsInt ();
69 if ((ch1 | ch2) < 0) throw  new java.io.EOFException ();
70 return (ch1 << 8) + (ch2 << 0);
71 });
72 Clazz.overrideMethod (c$, "readChar", 
73 function () {
74 var ch1 = this.$in.readByteAsInt ();
75 var ch2 = this.$in.readByteAsInt ();
76 if ((ch1 | ch2) < 0) throw  new java.io.EOFException ();
77 return String.fromCharCode ((ch1 << 8) + (ch2 << 0));
78 });
79 Clazz.overrideMethod (c$, "readInt", 
80 function () {
81 var ch1 = this.$in.readByteAsInt ();
82 var ch2 = this.$in.readByteAsInt ();
83 var ch3 = this.$in.readByteAsInt ();
84 var ch4 = this.$in.readByteAsInt ();
85 if ((ch1 | ch2 | ch3 | ch4) < 0) throw  new java.io.EOFException ();
86 var n = ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
87 {
88 return (n > 0x7FFFFFFF ? n - 0x100000000 : n);
89 }});
90 Clazz.overrideMethod (c$, "readLong", 
91 function () {
92 this.readFully (this.readBuffer, 0, 8);
93 return ((this.readBuffer[0] << 56) + ((this.readBuffer[1] & 255) << 48) + ((this.readBuffer[2] & 255) << 40) + ((this.readBuffer[3] & 255) << 32) + ((this.readBuffer[4] & 255) << 24) + ((this.readBuffer[5] & 255) << 16) + ((this.readBuffer[6] & 255) << 8) + ((this.readBuffer[7] & 255) << 0));
94 });
95 Clazz.overrideMethod (c$, "readFloat", 
96 function () {
97 return Float.intBitsToFloat (this.readInt ());
98 });
99 Clazz.overrideMethod (c$, "readDouble", 
100 function () {
101 return Double.longBitsToDouble (this.readLong ());
102 });
103 Clazz.overrideMethod (c$, "readLine", 
104 function () {
105 var buf = this.lineBuffer;
106 if (buf == null) {
107 buf = this.lineBuffer =  Clazz.newCharArray (128, '\0');
108 }var room = buf.length;
109 var offset = 0;
110 var c;
111 loop : while (true) {
112 switch (c = this.$in.readByteAsInt ()) {
113 case -1:
114 case '\n':
115 break loop;
116 case '\r':
117 var c2 = this.$in.readByteAsInt ();
118 if ((c2 != 10) && (c2 != -1)) {
119 if (!(Clazz.instanceOf (this.$in, java.io.PushbackInputStream))) {
120 this.$in =  new java.io.PushbackInputStream (this.$in, 1);
121 }(this.$in).unreadByte (c2);
122 }break loop;
123 default:
124 if (--room < 0) {
125 buf =  Clazz.newCharArray (offset + 128, '\0');
126 room = buf.length - offset - 1;
127 System.arraycopy (this.lineBuffer, 0, buf, 0, offset);
128 this.lineBuffer = buf;
129 }buf[offset++] = String.fromCharCode (c);
130 break;
131 }
132 }
133 if ((c == -1) && (offset == 0)) {
134 return null;
135 }return String.copyValueOf (buf, 0, offset);
136 });
137 Clazz.overrideMethod (c$, "readUTF", 
138 function () {
139 return java.io.DataInputStream.readUTFBytes (this, -1);
140 });
141 c$.readUTFBytes = Clazz.defineMethod (c$, "readUTFBytes", 
142 function ($in, utflen) {
143 var isByteArray = (utflen >= 0);
144 if (!isByteArray) utflen = $in.readUnsignedShort ();
145 var bytearr = null;
146 var chararr = null;
147 if (Clazz.instanceOf ($in, java.io.DataInputStream)) {
148 var dis = $in;
149 if (dis.bytearr.length < utflen) {
150 dis.bytearr =  Clazz.newByteArray (isByteArray ? utflen : utflen * 2, 0);
151 dis.chararr =  Clazz.newCharArray (dis.bytearr.length, '\0');
152 }chararr = dis.chararr;
153 bytearr = dis.bytearr;
154 } else {
155 bytearr =  Clazz.newByteArray (utflen, 0);
156 chararr =  Clazz.newCharArray (utflen, '\0');
157 }var c;
158 var char2;
159 var char3;
160 var count = 0;
161 var chararr_count = 0;
162 $in.readFully (bytearr, 0, utflen);
163 while (count < utflen) {
164 c = bytearr[count] & 0xff;
165 if (c > 127) break;
166 count++;
167 chararr[chararr_count++] = String.fromCharCode (c);
168 }
169 while (count < utflen) {
170 c = bytearr[count] & 0xff;
171 switch (c >> 4) {
172 case 0:
173 case 1:
174 case 2:
175 case 3:
176 case 4:
177 case 5:
178 case 6:
179 case 7:
180 count++;
181 chararr[chararr_count++] = String.fromCharCode (c);
182 break;
183 case 12:
184 case 13:
185 count += 2;
186 if (count > utflen) throw  new java.io.UTFDataFormatException ("malformed input: partial character at end");
187 char2 = bytearr[count - 1];
188 if ((char2 & 0xC0) != 0x80) throw  new java.io.UTFDataFormatException ("malformed input around byte " + count);
189 chararr[chararr_count++] = String.fromCharCode (((c & 0x1F) << 6) | (char2 & 0x3F));
190 break;
191 case 14:
192 count += 3;
193 if (count > utflen) throw  new java.io.UTFDataFormatException ("malformed input: partial character at end");
194 char2 = bytearr[count - 2];
195 char3 = bytearr[count - 1];
196 if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80)) throw  new java.io.UTFDataFormatException ("malformed input around byte " + (count - 1));
197 chararr[chararr_count++] = String.fromCharCode (((c & 0x0F) << 12) | ((char2 & 0x3F) << 6) | ((char3 & 0x3F) << 0));
198 break;
199 default:
200 throw  new java.io.UTFDataFormatException ("malformed input around byte " + count);
201 }
202 }
203 return  String.instantialize (chararr, 0, chararr_count);
204 }, "java.io.DataInput,~N");
205 });