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