-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathPyBufferedReader.java
More file actions
114 lines (102 loc) · 3.36 KB
/
Copy pathPyBufferedReader.java
File metadata and controls
114 lines (102 loc) · 3.36 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package org.python.modules._io;
import com.google.common.io.ByteStreams;
import org.python.core.BuiltinDocs;
import org.python.core.Py;
import org.python.core.PyBytes;
import org.python.core.PyLong;
import org.python.core.PyObject;
import org.python.expose.ExposedMethod;
import org.python.expose.ExposedType;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.channels.FileChannel;
@ExposedType(name = "_io.BufferedReader")
public class PyBufferedReader extends PyObject {
private BufferedInputStream input;
private FileChannel fileChannel;
public PyBufferedReader(InputStream in) {
super(TYPE);
input = new BufferedInputStream(in);
if (in instanceof FileInputStream) {
fileChannel = ((FileInputStream) in).getChannel();
}
}
public PyBufferedReader(File file) {
super(TYPE);
try {
FileInputStream fileInputStream = new FileInputStream(file);
input = new BufferedInputStream(fileInputStream);
fileChannel = fileInputStream.getChannel();
} catch (FileNotFoundException e) {
throw Py.IOError(e);
}
}
@ExposedMethod(names = {"read", "read1"}, defaults = {"-1"}, doc = BuiltinDocs.BufferedReader_read_doc)
public final PyObject BufferedReader_read(PyObject sizeObj) {
int size = sizeObj.asInt();
if (size < -1) {
throw Py.ValueError("invalid number of bytes to read");
}
byte[] buf;
if (size > 0) {
buf = new byte[size];
try {
int n = input.read(buf);
return new PyBytes(new String(buf, 0, n));
} catch (IOException e) {
throw Py.IOError(e);
}
}
try {
buf = ByteStreams.toByteArray(input);
return new PyBytes(new String(buf));
} catch (IOException e) {
throw Py.IOError(e);
}
}
@ExposedMethod(defaults = {"0"}, doc = BuiltinDocs.BufferedReader_peek_doc)
public final PyObject BufferedReader_peek(PyObject sizeObj) {
int size = sizeObj.asInt();
byte[] buf = new byte[size];
try {
int n = input.read(buf);
input.mark(n);
input.reset();
return new PyBytes(new String(buf, 0, n));
} catch (IOException e) {
throw Py.IOError(e);
}
}
@ExposedMethod
public final PyObject BufferedReader_tell() {
try {
return new PyLong(fileChannel.position());
} catch (IOException e) {
throw Py.IOError(e);
}
}
@ExposedMethod(defaults = {"0"}, doc = BuiltinDocs.BufferedReader_seek_doc)
public final PyObject BufferedReader_seek(PyObject pos, PyObject whence) {
try {
return new PyLong(fileChannel.position());
} catch (IOException e) {
throw Py.IOError(e);
}
}
@ExposedMethod
public final PyObject BufferedReader_readable() {
return Py.True;
}
@ExposedMethod
public final PyObject BufferedReader_writable() {
return Py.False;
}
@ExposedMethod
public final PyObject BufferedReader_seekable() {
return Py.newBoolean(fileChannel != null);
}
}