JAL-1807 Bob's JalviewJS prototype first commit
[jalviewjs.git] / site / j2s / java / io / InputStreamReader.js
1 Clazz.load (["java.io.Reader"], "java.io.InputStreamReader", ["java.lang.NullPointerException"], function () {\r
2 c$ = Clazz.decorateAsClass (function () {\r
3 this.$in = null;\r
4 this.isOpen = true;\r
5 this.charsetName = null;\r
6 this.isUTF8 = false;\r
7 this.bytearr = null;\r
8 this.pos = 0;\r
9 Clazz.instantialize (this, arguments);\r
10 }, java.io, "InputStreamReader", java.io.Reader);\r
11 Clazz.makeConstructor (c$, \r
12 function ($in) {\r
13 //just sets lock Clazz.superConstructor (this, java.io.InputStreamReader, [$in]);\r
14 this.$in = $in;\r
15 var charsetName = (arguments.length > 1 ? arguments[1] : "UTF-8");\r
16 \r
17 this.charsetName = charsetName;\r
18 if (!(this.isUTF8 = "UTF-8".equals (charsetName)) && !"ISO-8859-1".equals (charsetName)) throw  new NullPointerException ("charsetName");\r
19 }, "java.io.InputStream");\r
20 Clazz.defineMethod (c$, "getEncoding", \r
21 function () {\r
22 return this.charsetName;\r
23 });\r
24 Clazz.overrideMethod (c$, "read", \r
25 function (cbuf, offset, length) {\r
26 if (this.bytearr == null || this.bytearr.length < length) this.bytearr =  Clazz.newByteArray (length, 0);\r
27 var c;\r
28 var char2;\r
29 var char3;\r
30 var byteCount = 0;\r
31 var charCount = offset;\r
32 var byteLen = this.$in.read (this.bytearr, this.pos, length - this.pos);\r
33 var nAvail = this.$in.available ();\r
34 if (byteLen < 0) return -1;\r
35 var nMax = byteLen;\r
36 while (byteCount < nMax) {\r
37 c = this.bytearr[byteCount] & 0xff;\r
38 if (this.isUTF8) switch (c >> 4) {\r
39 case 0xC:\r
40 case 0xD:\r
41 if (byteCount + 1 >= byteLen) {\r
42 if (nAvail >= 1) {\r
43 nMax = byteCount;\r
44 continue;\r
45 }} else if (((char2 = this.bytearr[byteCount + 1]) & 0xC0) == 0x80) {\r
46 cbuf[charCount++] = String.fromCharCode (((c & 0x1F) << 6) | (char2 & 0x3F));\r
47 byteCount += 2;\r
48 continue;\r
49 }this.isUTF8 = false;\r
50 break;\r
51 case 0xE:\r
52 if (byteCount + 2 >= byteLen) {\r
53 if (nAvail >= 2) {\r
54 nMax = byteCount;\r
55 continue;\r
56 }} else if (((char2 = this.bytearr[byteCount + 1]) & 0xC0) == 0x80 && ((char3 = this.bytearr[byteCount + 2]) & 0xC0) == 0x80) {\r
57 cbuf[charCount++] = String.fromCharCode (((c & 0x0F) << 12) | ((char2 & 0x3F) << 6) | (char3 & 0x3F));\r
58 byteCount += 3;\r
59 continue;\r
60 }this.isUTF8 = false;\r
61 break;\r
62 }\r
63 byteCount++;\r
64 cbuf[charCount++] = String.fromCharCode (c);\r
65 }\r
66 this.pos = byteLen - byteCount;\r
67 for (var i = 0; i < this.pos; i++) {\r
68 this.bytearr[i] = this.bytearr[byteCount++];\r
69 }\r
70 return charCount - offset;\r
71 }, "~A,~N,~N");\r
72 Clazz.overrideMethod (c$, "ready", \r
73 function () {\r
74 return this.isOpen;\r
75 });\r
76 Clazz.overrideMethod (c$, "close", \r
77 function () {\r
78 this.$in.close ();\r
79 this.isOpen = false;\r
80 });\r
81 });\r