-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathPyBufferedWriter.java
More file actions
72 lines (63 loc) · 2.2 KB
/
Copy pathPyBufferedWriter.java
File metadata and controls
72 lines (63 loc) · 2.2 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
package org.python.modules._io;
import org.python.core.BufferProtocol;
import org.python.core.BuiltinDocs;
import org.python.core.Py;
import org.python.core.PyBUF;
import org.python.core.PyLong;
import org.python.core.PyObject;
import org.python.core.PyType;
import org.python.expose.ExposedMethod;
import org.python.expose.ExposedType;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.channels.FileChannel;
@ExposedType(name = "_io.BufferedWriter")
public class PyBufferedWriter extends PyObject {
public static final PyType TYPE = PyType.fromClass(PyBufferedWriter.class);
private BufferedOutputStream output;
private FileChannel fileChannel;
public PyBufferedWriter(OutputStream out) {
output = new BufferedOutputStream(out);
}
public PyBufferedWriter(File file) {
try {
FileOutputStream fileOutputStream = new FileOutputStream(file);
output = new BufferedOutputStream(fileOutputStream);
fileChannel = fileOutputStream.getChannel();
} catch (FileNotFoundException e) {
throw Py.IOError(e);
}
}
@ExposedMethod(doc = BuiltinDocs.BufferedWriter_write_doc)
public final PyObject BufferedWriter_write(PyObject b) {
if (!(b instanceof BufferProtocol)) {
throw Py.TypeError("bytes-like object expected");
}
try {
output.write(((BufferProtocol) b).getBuffer(PyBUF.FULL_RO).getNIOByteBuffer().array());
return new PyLong(b.__len__());
} catch (IOException e) {
throw Py.IOError(e);
}
}
@ExposedMethod(doc = BuiltinDocs.BufferedWriter_flush_doc)
public final void BufferedWriter_flush() {
try {
output.flush();
} catch (IOException e) {
throw Py.IOError(e);
}
}
@ExposedMethod(doc = BuiltinDocs.BufferedWriter_tell_doc)
public final PyObject BufferedWriter_tell() {
try {
return new PyLong(fileChannel.position());
} catch (IOException e) {
throw Py.IOError(e);
}
}
}