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