01: package org.python.core;
02:
03: import java.io.IOException;
04: import java.io.InputStream;
05:
06: public class FilelikeInputStream extends InputStream {
07:
08: private PyObject filelike;
09:
10: public FilelikeInputStream(PyObject filelike) {
11: this .filelike = filelike;
12: }
13:
14: public int read() throws IOException {
15: byte[] oneB = new byte[1];
16: int numread = read(oneB, 0, 1);
17: if (numread == -1) {
18: return -1;
19: }
20: return oneB[0];
21: }
22:
23: public int read(byte b[], int off, int len) throws IOException {
24: if (b == null) {
25: throw new NullPointerException();
26: } else if ((off < 0) || (off > b.length) || (len < 0)
27: || ((off + len) > b.length) || ((off + len) < 0)) {
28: throw new IndexOutOfBoundsException();
29: } else if (len == 0) {
30: return 0;
31: }
32: String result = ((PyString) filelike.__getattr__("read")
33: .__call__(new PyInteger(len))).string;
34: if (result.length() == 0) {
35: return -1;
36: }
37: System.arraycopy(result.getBytes(), 0, b, off, result.length());
38: return result.length();
39: }
40:
41: public void close() throws IOException {
42: filelike.__getattr__("close").__call__();
43: }
44: }
|