-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathTextIOInputStream.java
More file actions
60 lines (51 loc) · 1.45 KB
/
TextIOInputStream.java
File metadata and controls
60 lines (51 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/* Copyright (c) Jython Developers */
package org.python.core.io;
import java.io.InputStream;
import java.io.IOException;
/**
* An InputStream tie-in to a TextIOBase.
*/
public class TextIOInputStream extends InputStream {
private TextIOBase textIO;
/**
* Creates an InputStream wrapper to a given TextIOBase.
*
* @param textIO a TextIOBase
*/
public TextIOInputStream(TextIOBase textIO) {
this.textIO = textIO;
}
@Override
public int read() throws IOException {
String result = textIO.read(1);
if (result.length() == 0) {
return -1;
}
return (int)result.charAt(0);
}
@Override
public int read(byte[] b, int off, int len) throws IOException {
if (b == null) {
throw new NullPointerException();
} else if ((off < 0) || (off > b.length) || (len < 0) || ((off + len) > b.length)
|| ((off + len) < 0)) {
throw new IndexOutOfBoundsException();
} else if (len == 0) {
return 0;
}
String result = textIO.read(len);
len = result.length();
for (int i = 0; i < len; i++) {
b[off + i] = (byte)result.charAt(i);
}
return len == 0 ? -1 : len;
}
@Override
public void close() throws IOException {
textIO.close();
}
@Override
public long skip(long n) throws IOException {
return textIO.seek(n, 1);
}
}