JAL-1807 Bob's JalviewJS prototype first commit
[jalviewjs.git] / site / j2s / java / io / BufferedInputStream.js
1 Clazz.load (["java.io.FilterInputStream"], "java.io.BufferedInputStream", ["java.io.IOException", "java.lang.IndexOutOfBoundsException"], function () {\r
2 c$ = Clazz.decorateAsClass (function () {\r
3 this.buf = null;\r
4 this.count = 0;\r
5 this.pos = 0;\r
6 this.markpos = -1;\r
7 this.marklimit = 0;\r
8 Clazz.instantialize (this, arguments);\r
9 }, java.io, "BufferedInputStream", java.io.FilterInputStream);\r
10 Clazz.defineMethod (c$, "getInIfOpen", \r
11  function () {\r
12 var input = this.$in;\r
13 if (input == null) throw  new java.io.IOException ("Stream closed");\r
14 return input;\r
15 });\r
16 Clazz.defineMethod (c$, "getBufIfOpen", \r
17  function () {\r
18 var buffer = this.buf;\r
19 if (buffer == null) throw  new java.io.IOException ("Stream closed");\r
20 return buffer;\r
21 });\r
22 Clazz.overrideMethod (c$, "resetStream", \r
23 function () {\r
24 });\r
25 Clazz.makeConstructor (c$, \r
26 function ($in) {\r
27 Clazz.superConstructor (this, java.io.BufferedInputStream, [$in]);\r
28 this.buf =  Clazz.newByteArray (8192, 0);\r
29 }, "java.io.InputStream");\r
30 Clazz.defineMethod (c$, "fill", \r
31  function () {\r
32 var buffer = this.getBufIfOpen ();\r
33 if (this.markpos < 0) this.pos = 0;\r
34  else if (this.pos >= buffer.length) if (this.markpos > 0) {\r
35 var sz = this.pos - this.markpos;\r
36 System.arraycopy (buffer, this.markpos, buffer, 0, sz);\r
37 this.pos = sz;\r
38 this.markpos = 0;\r
39 } else if (buffer.length >= this.marklimit) {\r
40 this.markpos = -1;\r
41 this.pos = 0;\r
42 } else {\r
43 var nsz = this.pos * 2;\r
44 if (nsz > this.marklimit) nsz = this.marklimit;\r
45 var nbuf =  Clazz.newByteArray (nsz, 0);\r
46 System.arraycopy (buffer, 0, nbuf, 0, this.pos);\r
47 buffer = this.buf = nbuf;\r
48 }this.count = this.pos;\r
49 var n = this.getInIfOpen ().read (buffer, this.pos, buffer.length - this.pos);\r
50 if (n > 0) this.count = n + this.pos;\r
51 });\r
52 Clazz.overrideMethod (c$, "readByteAsInt", \r
53 function () {\r
54 if (this.pos >= this.count) {\r
55 this.fill ();\r
56 if (this.pos >= this.count) return -1;\r
57 }return this.getBufIfOpen ()[this.pos++] & 0xff;\r
58 });\r
59 Clazz.defineMethod (c$, "read1", \r
60  function (b, off, len) {\r
61 var avail = this.count - this.pos;\r
62 if (avail <= 0) {\r
63 if (len >= this.getBufIfOpen ().length && this.markpos < 0) {\r
64 return this.getInIfOpen ().read (b, off, len);\r
65 }this.fill ();\r
66 avail = this.count - this.pos;\r
67 if (avail <= 0) return -1;\r
68 }var cnt = (avail < len) ? avail : len;\r
69 System.arraycopy (this.getBufIfOpen (), this.pos, b, off, cnt);\r
70 this.pos += cnt;\r
71 return cnt;\r
72 }, "~A,~N,~N");\r
73 Clazz.overrideMethod (c$, "read", \r
74 function (b, off, len) {\r
75 if (arguments.length == 1) {\r
76   off = 0;\r
77   len  = b.length;\r
78 }\r
79 this.getBufIfOpen ();\r
80 if ((off | len | (off + len) | (b.length - (off + len))) < 0) {\r
81 throw  new IndexOutOfBoundsException ();\r
82 } else if (len == 0) {\r
83 return 0;\r
84 }var n = 0;\r
85 for (; ; ) {\r
86 var nread = this.read1 (b, off + n, len - n);\r
87 if (nread <= 0) return (n == 0) ? nread : n;\r
88 n += nread;\r
89 if (n >= len) return n;\r
90 var input = this.$in;\r
91 if (input != null && input.available () <= 0) return n;\r
92 }\r
93 }, "~A,~N,~N");\r
94 Clazz.overrideMethod (c$, "skip", \r
95 function (n) {\r
96 this.getBufIfOpen ();\r
97 if (n <= 0) {\r
98 return 0;\r
99 }var avail = this.count - this.pos;\r
100 if (avail <= 0) {\r
101 if (this.markpos < 0) return this.getInIfOpen ().skip (n);\r
102 this.fill ();\r
103 avail = this.count - this.pos;\r
104 if (avail <= 0) return 0;\r
105 }var skipped = (avail < n) ? avail : n;\r
106 this.pos += skipped;\r
107 return skipped;\r
108 }, "~N");\r
109 Clazz.overrideMethod (c$, "available", \r
110 function () {\r
111 var n = this.count - this.pos;\r
112 var avail = this.getInIfOpen ().available ();\r
113 return n > (2147483647 - avail) ? 2147483647 : n + avail;\r
114 });\r
115 Clazz.overrideMethod (c$, "mark", \r
116 function (readlimit) {\r
117 this.marklimit = readlimit;\r
118 this.markpos = this.pos;\r
119 }, "~N");\r
120 Clazz.overrideMethod (c$, "reset", \r
121 function () {\r
122 this.getBufIfOpen ();\r
123 if (this.markpos < 0) throw  new java.io.IOException ("Resetting to invalid mark");\r
124 this.pos = this.markpos;\r
125 });\r
126 Clazz.overrideMethod (c$, "markSupported", \r
127 function () {\r
128 return true;\r
129 });\r
130 Clazz.overrideMethod (c$, "close", \r
131 function () {\r
132 var input = this.$in;\r
133 this.$in = null;\r
134 if (input != null) input.close ();\r
135 return;\r
136 });\r
137 Clazz.defineStatics (c$,\r
138 "DEFAULT_BUFFER_SIZE", 8192);\r
139 });\r