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