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