-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathPyIOFileFactory.java
More file actions
180 lines (141 loc) · 4.63 KB
/
PyIOFileFactory.java
File metadata and controls
180 lines (141 loc) · 4.63 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package org.python.modules;
import org.python.core.Py;
import org.python.core.PyFile;
import org.python.core.PyInteger;
import org.python.core.PyObject;
import org.python.core.PyString;
import org.python.core.PyType;
import org.python.core.__builtin__;
import org.python.core.Traverseproc;
import org.python.core.Visitproc;
// XXX - add support for StringIO, not just cStringIO
public class PyIOFileFactory {
private PyIOFileFactory() {
}
public static PyIOFile createIOFile(PyObject file) {
Object f = file.__tojava__(cStringIO.StringIO.class);
if (f != Py.NoConversion) {
return new cStringIOFile(file);
} else if (__builtin__.isinstance(file, FileType)) {
return new FileIOFile(file);
} else {
return new ObjectIOFile(file);
}
}
private static PyType FileType = PyType.fromClass(PyFile.class);
// Use a cStringIO as a file.
static class cStringIOFile implements PyIOFile {
cStringIO.StringIO file;
cStringIOFile(PyObject file) {
this.file = (cStringIO.StringIO) file.__tojava__(Object.class);
}
public void write(String str) {
file.write(str);
}
public void write(char ch) {
file.writeChar(ch);
}
public void flush() {
}
public String read(int len) {
return file.read(len).asString();
}
public String readlineNoNl() {
return file.readlineNoNl().asString();
}
}
// Use a PyFile as a file.
static class FileIOFile implements PyIOFile, Traverseproc {
PyFile file;
FileIOFile(PyObject file) {
this.file = (PyFile) file.__tojava__(PyFile.class);
if (this.file.getClosed()) {
throw Py.ValueError("I/O operation on closed file");
}
}
public void write(String str) {
file.write(str);
}
public void write(char ch) {
file.write(cStringIO.getString(ch));
}
public void flush() {
}
public String read(int len) {
return file.read(len).toString();
}
public String readlineNoNl() {
String line = file.readline().toString();
return line.substring(0, line.length() - 1);
}
/* Traverseproc implementation */
@Override
public int traverse(Visitproc visit, Object arg) {
return file == null ? 0 : visit.visit(file, arg);
}
@Override
public boolean refersDirectlyTo(PyObject ob) {
return ob != null && ob == file;
}
}
// Use any python object as a file.
static class ObjectIOFile implements PyIOFile, Traverseproc {
char[] charr = new char[1];
StringBuilder buff = new StringBuilder();
PyObject write;
PyObject read;
PyObject readline;
final int BUF_SIZE = 256;
ObjectIOFile(PyObject file) {
// this.file = file;
write = file.__findattr__("write");
read = file.__findattr__("read");
readline = file.__findattr__("readline");
}
public void write(String str) {
buff.append(str);
if (buff.length() > BUF_SIZE) {
flush();
}
}
public void write(char ch) {
buff.append(ch);
if (buff.length() > BUF_SIZE) {
flush();
}
}
public void flush() {
write.__call__(new PyString(buff.toString()));
buff.setLength(0);
}
public String read(int len) {
return read.__call__(new PyInteger(len)).toString();
}
public String readlineNoNl() {
String line = readline.__call__().toString();
return line.substring(0, line.length() - 1);
}
/* Traverseproc implementation */
@Override
public int traverse(Visitproc visit, Object arg) {
int retVal;
if (write != null) {
retVal = visit.visit(write, arg);
if (retVal != 0) {
return retVal;
}
}
if (read != null) {
retVal = visit.visit(read, arg);
if (retVal != 0) {
return retVal;
}
}
return readline == null ? 0 : visit.visit(readline, arg);
}
@Override
public boolean refersDirectlyTo(PyObject ob) {
return ob != null && (ob == write || ob == read || ob == readline);
}
}
}