Merge branch 'master' of https://source.jalview.org/git/jalviewjs.git
[jalviewjs.git] / site / j2s / java / io / BufferedReader.js
1 Clazz.load (["java.io.Reader"], "java.io.BufferedReader", ["java.io.IOException", "java.lang.IllegalArgumentException", "$.IndexOutOfBoundsException", "JU.SB"], function () {
2 c$ = Clazz.decorateAsClass (function () {
3 this.$in = null;
4 this.cb = null;
5 this.nChars = 0;
6 this.nextChar = 0;
7 this.markedChar = -1;
8 this.readAheadLimit = 0;
9 this.skipLF = false;
10 this.markedSkipLF = false;
11 Clazz.instantialize (this, arguments);
12 }, java.io, "BufferedReader", java.io.Reader);
13 Clazz.defineMethod (c$, "setSize", 
14  function (sz) {
15 if (sz <= 0) throw  new IllegalArgumentException ("Buffer size <= 0");
16 this.cb =  Clazz.newCharArray (sz, '\0');
17 this.nextChar = this.nChars = 0;
18 }, "~N");
19 Clazz.makeConstructor (c$, 
20 function ($in) {
21 Clazz.superConstructor (this, java.io.BufferedReader, [$in]);
22 this.$in = $in;
23 this.setSize (8192);
24 }, "java.io.Reader");
25 Clazz.defineMethod (c$, "ensureOpen", 
26  function () {
27 if (this.$in == null) throw  new java.io.IOException ("Stream closed");
28 });
29 Clazz.defineMethod (c$, "fill", 
30  function () {
31 var dst;
32 if (this.markedChar <= -1) {
33 dst = 0;
34 } else {
35 var delta = this.nextChar - this.markedChar;
36 if (delta >= this.readAheadLimit) {
37 this.markedChar = -2;
38 this.readAheadLimit = 0;
39 dst = 0;
40 } else {
41 if (this.readAheadLimit <= this.cb.length) {
42 System.arraycopy (this.cb, this.markedChar, this.cb, 0, delta);
43 this.markedChar = 0;
44 dst = delta;
45 } else {
46 var ncb =  Clazz.newCharArray (this.readAheadLimit, '\0');
47 System.arraycopy (this.cb, this.markedChar, ncb, 0, delta);
48 this.cb = ncb;
49 this.markedChar = 0;
50 dst = delta;
51 }this.nextChar = this.nChars = delta;
52 }}var n;
53 do {
54 n = this.$in.read (this.cb, dst, this.cb.length - dst);
55 } while (n == 0);
56 if (n > 0) {
57 this.nChars = dst + n;
58 this.nextChar = dst;
59 }});
60 Clazz.defineMethod (c$, "read1", 
61  function (cbuf, off, len) {
62 if (this.nextChar >= this.nChars) {
63 if (len >= this.cb.length && this.markedChar <= -1 && !this.skipLF) {
64 return this.$in.read (cbuf, off, len);
65 }this.fill ();
66 }if (this.nextChar >= this.nChars) return -1;
67 if (this.skipLF) {
68 this.skipLF = false;
69 if (this.cb[this.nextChar] == '\n') {
70 this.nextChar++;
71 if (this.nextChar >= this.nChars) this.fill ();
72 if (this.nextChar >= this.nChars) return -1;
73 }}var n = Math.min (len, this.nChars - this.nextChar);
74 System.arraycopy (this.cb, this.nextChar, cbuf, off, n);
75 this.nextChar += n;
76 return n;
77 }, "~A,~N,~N");
78 Clazz.defineMethod (c$, "read", 
79 function (cbuf, off, len) {
80 {
81 this.ensureOpen ();
82 if ((off < 0) || (off > cbuf.length) || (len < 0) || ((off + len) > cbuf.length) || ((off + len) < 0)) {
83 throw  new IndexOutOfBoundsException ();
84 } else if (len == 0) {
85 return 0;
86 }var n = this.read1 (cbuf, off, len);
87 if (n <= 0) return n;
88 while ((n < len) && this.$in.ready ()) {
89 var n1 = this.read1 (cbuf, off + n, len - n);
90 if (n1 <= 0) break;
91 n += n1;
92 }
93 return n;
94 }}, "~A,~N,~N");
95 Clazz.defineMethod (c$, "readLine1", 
96  function (ignoreLF) {
97 var s = null;
98 var startChar;
99 {
100 this.ensureOpen ();
101 var omitLF = ignoreLF || this.skipLF;
102 for (; ; ) {
103 if (this.nextChar >= this.nChars) this.fill ();
104 if (this.nextChar >= this.nChars) {
105 if (s != null && s.length () > 0) return s.toString ();
106 return null;
107 }var eol = false;
108 var c = String.fromCharCode (0);
109 var i;
110 if (omitLF && (this.cb[this.nextChar] == '\n')) this.nextChar++;
111 this.skipLF = false;
112 omitLF = false;
113 charLoop : for (i = this.nextChar; i < this.nChars; i++) {
114 c = this.cb[i];
115 if ((c == '\n') || (c == '\r')) {
116 eol = true;
117 break charLoop;
118 }}
119 startChar = this.nextChar;
120 this.nextChar = i;
121 if (eol) {
122 var str;
123 if (s == null) {
124 str =  String.instantialize (this.cb, startChar, i - startChar);
125 } else {
126 s.appendCB (this.cb, startChar, i - startChar);
127 str = s.toString ();
128 }this.nextChar++;
129 if (c == '\r') {
130 this.skipLF = true;
131 }return str;
132 }if (s == null) s = JU.SB.newN (80);
133 s.appendCB (this.cb, startChar, i - startChar);
134 }
135 }}, "~B");
136 Clazz.defineMethod (c$, "readLine", 
137 function () {
138 return this.readLine1 (false);
139 });
140 Clazz.overrideMethod (c$, "skip", 
141 function (n) {
142 if (n < 0) {
143 throw  new IllegalArgumentException ("skip value is negative");
144 }{
145 this.ensureOpen ();
146 var r = n;
147 while (r > 0) {
148 if (this.nextChar >= this.nChars) this.fill ();
149 if (this.nextChar >= this.nChars) break;
150 if (this.skipLF) {
151 this.skipLF = false;
152 if (this.cb[this.nextChar] == '\n') {
153 this.nextChar++;
154 }}var d = this.nChars - this.nextChar;
155 if (r <= d) {
156 this.nextChar += r;
157 r = 0;
158 break;
159 }r -= d;
160 this.nextChar = this.nChars;
161 }
162 return n - r;
163 }}, "~N");
164 Clazz.defineMethod (c$, "ready", 
165 function () {
166 {
167 this.ensureOpen ();
168 if (this.skipLF) {
169 if (this.nextChar >= this.nChars && this.$in.ready ()) {
170 this.fill ();
171 }if (this.nextChar < this.nChars) {
172 if (this.cb[this.nextChar] == '\n') this.nextChar++;
173 this.skipLF = false;
174 }}return (this.nextChar < this.nChars) || this.$in.ready ();
175 }});
176 Clazz.overrideMethod (c$, "markSupported", 
177 function () {
178 return true;
179 });
180 Clazz.overrideMethod (c$, "mark", 
181 function (readAheadLimit) {
182 if (readAheadLimit < 0) {
183 throw  new IllegalArgumentException ("Read-ahead limit < 0");
184 }{
185 this.ensureOpen ();
186 this.readAheadLimit = readAheadLimit;
187 this.markedChar = this.nextChar;
188 this.markedSkipLF = this.skipLF;
189 }}, "~N");
190 Clazz.overrideMethod (c$, "reset", 
191 function () {
192 {
193 this.ensureOpen ();
194 if (this.markedChar < 0) throw  new java.io.IOException ((this.markedChar == -2) ? "Mark invalid" : "Stream not marked");
195 this.nextChar = this.markedChar;
196 this.skipLF = this.markedSkipLF;
197 }});
198 Clazz.defineMethod (c$, "close", 
199 function () {
200 {
201 if (this.$in == null) return;
202 this.$in.close ();
203 this.$in = null;
204 this.cb = null;
205 }});
206 Clazz.defineStatics (c$,
207 "INVALIDATED", -2,
208 "UNMARKED", -1,
209 "DEFAULT_CHAR_BUFFER_SIZE", 8192,
210 "DEFAULT_EXPECTED_LINE_LENGTH", 80);
211 });