Skip to content

Commit 182b862

Browse files
committed
Emulation for io, Collections and MessageDigestSpi
1 parent 76bec19 commit 182b862

9 files changed

Lines changed: 384 additions & 20 deletions

File tree

build_tools/doctool/src/com/google/doctool/custom/missing/gh10221.properties

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,6 @@ java.util.Arrays#compareUnsigned(int[], int, int, int[], int, int)\
1111
java.util.Arrays#compareUnsigned(long[], int, int, long[], int, int)\
1212
java.util.Arrays#compareUnsigned(short[], int, int, short[], int, int)\
1313
java.util.Arrays#compareUnsigned(int[], int[])\
14-
java.util.Collections#emptyEnumeration()\
15-
java.util.Collections#indexOfSubList(List, List)\
16-
java.util.Collections#lastIndexOfSubList(List, List)\
17-
java.util.Collections#unmodifiableNavigableSet(NavigableSet)\
18-
java.util.Collections#unmodifiableNavigableMap(NavigableMap)\
19-
java.util.Collections#emptySortedSet()\
20-
java.util.Collections#emptyNavigableSet()\
21-
java.util.Collections#emptySortedMap()\
22-
java.util.Collections#emptyNavigableMap()\
23-
java.io.InputStream#readAllBytes()\
24-
java.io.InputStream#readNBytes(int)\
25-
java.io.InputStream#readNBytes(byte[], int, int)\
26-
java.io.InputStream#transferTo(OutputStream)\
27-
java.io.InputStream#nullInputStream()\
28-
java.io.InputStream#skipNBytes(long)\
29-
java.io.PrintStream#writeBytes(byte[])\
30-
java.io.Reader#nullReader()\
31-
java.io.Reader#transferTo(Writer)\
32-
java.security.MessageDigestSpi#clone()\
3314
java.lang.Double#toHexString(double)\
3415
java.lang.Float#toHexString(float)\
3516
java.lang.System#lineSeparator()

user/super/com/google/gwt/emul/java/io/ByteArrayInputStream.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ public long skip(long byteCount) {
184184
return 0;
185185
}
186186
int temp = pos;
187-
pos = this.count - pos < byteCount ? this.count : (int) (pos + byteCount);
187+
pos = this.count < pos + byteCount ? this.count : (int) (pos + byteCount);
188188
return pos - temp;
189189
}
190190
}

user/super/com/google/gwt/emul/java/io/InputStream.java

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
import static javaemul.internal.InternalPreconditions.checkNotNull;
2323

24+
import java.util.Arrays;
25+
2426
/**
2527
* A readable source of bytes.
2628
*
@@ -203,6 +205,69 @@ public int read(byte[] buffer, int byteOffset, int byteCount) throws IOException
203205
return byteCount;
204206
}
205207

208+
public byte[] readAllBytes() throws IOException {
209+
return readNBytes(0, Integer.MAX_VALUE);
210+
}
211+
212+
public byte[] readNBytes(int len) throws IOException {
213+
return readNBytes(0, len);
214+
}
215+
216+
public byte[] readNBytes(int off, int len) throws IOException {
217+
if (len == 0) {
218+
return new byte[0];
219+
}
220+
skipNBytes(off);
221+
int capacity = Math.min(len, Math.max(2048, available()));
222+
byte[] buffer = new byte[capacity];
223+
int pos = 0;
224+
while (pos <= len) {
225+
int chunk = read(buffer, pos, capacity - pos);
226+
if (chunk == 0) {
227+
break;
228+
}
229+
pos += chunk;
230+
if (pos == capacity) {
231+
capacity = Math.min(len, 2 * capacity);
232+
buffer = Arrays.copyOf(buffer, capacity);
233+
}
234+
}
235+
236+
return pos < len ? Arrays.copyOf(buffer, pos) : buffer;
237+
}
238+
239+
public int readNBytes(byte[] buffer, int off, int len) throws IOException {
240+
if (len == 0) {
241+
return 0;
242+
}
243+
skipNBytes(off);
244+
int pos = 0;
245+
while (pos <= len) {
246+
int chunk = read(buffer, pos, len);
247+
if (chunk == 0) {
248+
break;
249+
}
250+
pos += chunk;
251+
}
252+
253+
return pos;
254+
}
255+
256+
public void skipNBytes(long n) throws IOException {
257+
long skipped = 0;
258+
while (skipped < n) {
259+
long chunk = skip(n);
260+
if (chunk == 0) {
261+
if (read() >= 0) {
262+
skipped++;
263+
} else {
264+
throw new IOException("End of stream reached");
265+
}
266+
}
267+
skipped += chunk;
268+
}
269+
}
270+
206271
/**
207272
* Resets this stream to the last marked location. Throws an
208273
* {@code IOException} if the number of bytes read since the mark has been
@@ -256,4 +321,31 @@ public long skip(long byteCount) throws IOException {
256321
}
257322
return skipped;
258323
}
324+
325+
public void transferTo(OutputStream writer) throws IOException {
326+
byte[] buffer = new byte[2048];
327+
int read;
328+
while ((read = read(buffer)) > 0) {
329+
writer.write(buffer, 0, read);
330+
}
331+
}
332+
333+
public static InputStream nullInputStream() {
334+
return new InputStream() {
335+
private boolean closed;
336+
337+
@Override
338+
public int read() throws IOException {
339+
if (closed) {
340+
throw new IOException("Already closed");
341+
}
342+
return -1;
343+
}
344+
345+
@Override
346+
public void close() {
347+
closed = true;
348+
}
349+
};
350+
}
259351
}

user/super/com/google/gwt/emul/java/io/PrintStream.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,10 @@ public void close() {
145145
}
146146
}
147147

148+
public void writeBytes(byte[] buffer) {
149+
write(buffer, 0, buffer.length);
150+
}
151+
148152
@Override
149153
public void write(byte[] buffer, int offset, int length) {
150154
// Force buffer null check first!

user/super/com/google/gwt/emul/java/io/Reader.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,31 @@ public long skip(long n) throws IOException {
9999
}
100100
return n - remaining;
101101
}
102+
103+
public void transferTo(Writer writer) throws IOException {
104+
char[] buffer = new char[1024];
105+
int read;
106+
while ((read = read(buffer)) > 0) {
107+
writer.write(buffer, 0, read);
108+
}
109+
}
110+
111+
public static Reader nullReader() {
112+
return new Reader() {
113+
private boolean closed;
114+
115+
@Override
116+
public int read(char[] cbuf, int off, int len) throws IOException {
117+
if (closed) {
118+
throw new IOException("Already closed");
119+
}
120+
return 0;
121+
}
122+
123+
@Override
124+
public void close() throws IOException {
125+
closed = true;
126+
}
127+
};
128+
}
102129
}

user/super/com/google/gwt/emul/java/security/MessageDigest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
import static javaemul.internal.Coercions.ensureInt;
1919

20+
import java.util.Arrays;
21+
2022
/**
2123
* Message Digest algorithm - <a href=
2224
* "http://java.sun.com/j2se/1.4.2/docs/api/java/security/MessageDigest.html"
@@ -265,6 +267,15 @@ protected void engineReset() {
265267
remainder = 64;
266268
}
267269

270+
public Object clone() {
271+
Md5Digest copy = new Md5Digest();
272+
copy.buffer = Arrays.copyOf(buffer, buffer.length);
273+
copy.state = Arrays.copyOf(state, state.length);
274+
copy.counter = counter;
275+
copy.remainder = remainder;
276+
return copy;
277+
}
278+
268279
@Override
269280
protected void engineUpdate(byte input) {
270281
// TODO(jat): better implementation

user/super/com/google/gwt/emul/java/security/MessageDigestSpi.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ protected int engineDigest(byte[] buf, int offset, int len)
3636
System.arraycopy(digest, 0, buf, offset, digest.length);
3737
return digest.length;
3838
}
39+
40+
public Object clone() throws CloneNotSupportedException {
41+
throw new CloneNotSupportedException();
42+
}
3943

4044
protected int engineGetDigestLength() {
4145
return 0;

user/super/com/google/gwt/emul/java/security/SHA256Digest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,16 @@ protected void engineReset() {
6666
byteCounter = 0;
6767
}
6868

69+
public Object clone() {
70+
SHA256Digest copy = new SHA256Digest();
71+
copy.wordBuffer = Arrays.copyOf(wordBuffer, wordBuffer.length);
72+
copy.blockBuffer = Arrays.copyOf(blockBuffer, blockBuffer.length);
73+
copy.wordOff = wordOff;
74+
copy.byteCounter = byteCounter;
75+
copy.blockOff = blockOff;
76+
return copy;
77+
}
78+
6979
@Override
7080
protected void engineUpdate(byte input) {
7181
wordBuffer[wordOff] = input;

0 commit comments

Comments
 (0)