-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathPyStringIO.java
More file actions
419 lines (360 loc) · 11.2 KB
/
Copy pathPyStringIO.java
File metadata and controls
419 lines (360 loc) · 11.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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
package org.python.modules._io;
import org.python.core.Py;
import org.python.core.PyArray;
import org.python.core.PyBytes;
import org.python.core.PyIterator;
import org.python.core.PyList;
import org.python.core.PyLong;
import org.python.core.PyNewWrapper;
import org.python.core.PyObject;
import org.python.core.PyTuple;
import org.python.core.PyType;
import org.python.core.PyUnicode;
import org.python.expose.ExposedMethod;
import org.python.expose.ExposedNew;
import org.python.expose.ExposedType;
import org.python.modules.cStringIO;
@ExposedType(name = "_io.StringIO")
public class PyStringIO extends PyIterator {
public static final PyType TYPE = PyType.fromClass(PyStringIO.class);
public boolean softspace = false;
public boolean closed = false;
public int pos = 0;
public PyObject dict = null;
public PyObject readnl = new PyUnicode('\n');
private final StringBuilder buf;
public PyStringIO() {
buf = new StringBuilder();
}
public PyStringIO(String buffer) {
buf = new StringBuilder(buffer);
}
public PyStringIO(PyArray array) {
buf = new StringBuilder(array.tostring());
}
@ExposedNew
static PyObject StringIO___new__(PyNewWrapper new_, boolean init, PyType subtype,
PyObject[] args, String[] keywords) {
if (args.length > 0) {
return new PyStringIO(args[0].toString());
}
return new PyStringIO();
}
private void _complain_ifclosed() {
if (closed)
throw Py.ValueError("I/O operation on closed file");
}
private int _convert_to_int(long val) {
if (val > Integer.MAX_VALUE) {
throw Py.OverflowError("long int too large to convert to int");
}
return (int)val;
}
@ExposedMethod
public void StringIO__setattr__(String name, PyObject value) {
if (name == "softspace") {
softspace = value.__bool__();
return;
}
super.__setattr__(name, value);
}
public PyObject __next__() {
_complain_ifclosed();
PyBytes r = readline();
if (r.__len__() == 0)
return null;
return r;
}
/**
* Free the memory buffer.
*/
public void close() {
closed = true;
// No point in zeroing the buf, because it won't be reused.
// buf is a final variable, so can't set to null.
// Therefore, just leave it and let it be GC'ed when the enclosing object is GC'ed
// Or remove the final declaration
// buf = null;
}
/**
* Return false.
* @return false.
*/
public boolean isatty() {
_complain_ifclosed();
return false;
}
/**
* Position the file pointer to the absolute position.
* @param pos the position in the file.
*/
public void seek(long pos) {
seek(pos, os.SEEK_SET);
}
/**
* Position the file pointer to the position in the .
*
* @param pos
* the position in the file.
* @param mode
* 0=from the start, 1=relative, 2=from the end.
*/
public synchronized void seek(long pos, int mode) {
_complain_ifclosed();
switch (mode) {
case os.SEEK_CUR:
this.pos += pos;
break;
case os.SEEK_END:
this.pos = _convert_to_int(pos + buf.length());
break;
case os.SEEK_SET:
default:
this.pos = _convert_to_int(pos);
break;
}
}
/**
* Reset the file position to the beginning of the file.
*/
public synchronized void reset() {
pos = 0;
}
/**
* Return the file position.
* @return the position in the file.
*/
public synchronized int tell() {
_complain_ifclosed();
return pos;
}
/**
* Read all data until EOF is reached.
* An empty string is returned when EOF is encountered immediately.
* @return A string containing the data.
*/
public PyBytes read() {
return StringIO_read(-1);
}
/**
* Read at most size bytes from the file (less if the read hits EOF).
* If the size argument is negative, read all data until EOF is
* reached. An empty string is returned when EOF is encountered
* immediately.
* @param size the number of characters to read.
* @return A string containing the data read.
*/
@ExposedMethod
public synchronized PyBytes StringIO_read(long size) {
_complain_ifclosed();
_convert_to_int(size);
int len = buf.length();
String substr;
if (size < 0) {
substr = pos >= len ? "" : buf.substring(pos);
pos = len;
} else {
// ensure no overflow
int newpos = _convert_to_int(Math.min(pos + size, len));
substr = buf.substring(pos, newpos);
pos = newpos;
}
return new PyBytes(substr);
}
/**
* Read one entire line from the file. A trailing newline character
* is kept in the string (but may be absent when a file ends with
* an incomplete line).
* An empty string is returned when EOF is hit immediately.
* @return data from the file up to and including the newline.
*/
public PyBytes readline() {
return StringIO_readline(-1);
}
/**
* Read one entire line from the file. A trailing newline character
* is kept in the string (but may be absent when a file ends with an
* incomplete line).
* If the size argument is non-negative, it is a maximum byte count
* (including the trailing newline) and an incomplete line may be
* returned.
* @return data from the file up to and including the newline.
*/
@ExposedMethod
public synchronized PyBytes StringIO_readline(long size) {
_complain_ifclosed();
_convert_to_int(size);
int len = buf.length();
if (pos == len) {
return new PyBytes("");
}
int i = buf.indexOf("\n", pos);
int newpos = (i < 0) ? len : i + 1;
if (size >= 0) {
newpos = _convert_to_int(Math.min(newpos - pos, size) + pos);
}
String r = buf.substring(pos, newpos);
pos = newpos;
return new PyBytes(r);
}
/**
* Read and return a line without the trailing newline.
* Usind by PickleModule as an optimization.
*/
public synchronized PyBytes readlineNoNl() {
_complain_ifclosed();
int len = buf.length();
int i = buf.indexOf("\n", pos);
int newpos = (i < 0) ? len : i;
String r = buf.substring(pos, newpos);
pos = newpos;
if (pos < len) // Skip the newline
pos++;
return new PyBytes(r);
}
/**
* Read until EOF using readline() and return a list containing
* the lines thus read.
* @return a list of the lines.
*/
public PyObject readlines() {
return StringIO_readlines(0);
}
/**
* Read until EOF using readline() and return a list containing
* the lines thus read.
* @return a list of the lines.
*/
@ExposedMethod
public PyObject StringIO_readlines(long sizehint) {
_complain_ifclosed();
int sizehint_int = (int)sizehint;
int total = 0;
PyList lines = new PyList();
PyBytes line = readline();
while (line.__len__() > 0) {
lines.append(line);
total += line.__len__();
if (0 < sizehint_int && sizehint_int <= total)
break;
line = readline();
}
return lines;
}
/**
* truncate the file at the current position.
*/
public synchronized void truncate() {
buf.setLength(this.pos);
}
/**
* truncate the file at the position pos.
*/
@ExposedMethod
public synchronized void StringIO_truncate(long pos) {
if (pos < 0) {
throw Py.IOError("Negative size not allowed");
}
int pos_int = _convert_to_int(pos);
if (pos_int < 0)
pos_int = this.pos;
buf.setLength(pos_int);
this.pos = pos_int;
}
/**
* Write a string to the file.
* @param obj The data to write.
*/
public void write(PyObject obj) {
write(obj.toString());
}
public synchronized void write(String s) {
_complain_ifclosed();
int spos = pos;
int slen = buf.length();
if (spos == slen) {
buf.append(s);
buf.setLength(slen + s.length());
pos = spos + s.length();
return;
}
if (spos > slen) {
int l = spos - slen;
char[] bytes = new char[l];
for (int i = 0; i < l - 1; i++)
bytes[i] = '\0';
buf.append(bytes);
slen = spos;
}
int newpos = spos + s.length();
if (spos < slen) {
if (newpos > slen) {
buf.replace(spos, slen, s);
buf.append(s.substring(slen - spos));
slen = newpos;
} else {
buf.replace(spos, spos + s.length(), s);
}
} else {
buf.append(s);
slen = newpos;
}
buf.setLength(slen);
pos = newpos;
}
/**
* Write a char to the file. Used by PickleModule as an optimization.
* @param ch The data to write.
*/
public synchronized void writeChar(char ch) {
int len = buf.length();
if (len <= pos)
buf.setLength(pos + 1);
buf.setCharAt(pos++, ch);
}
/**
* Write a list of strings to the file.
*/
public void writelines(PyObject lines) {
for (PyObject line : lines.asIterable()) {
write(line);
}
}
/**
* Flush the internal buffer. Does nothing.
*/
public void flush() {
_complain_ifclosed();
}
/**
* Retrieve the entire contents of the ``file'' at any time
* before the StringIO object's close() method is called.
* @return the contents of the StringIO.
*/
public synchronized PyBytes getvalue() {
_complain_ifclosed();
return new PyBytes(buf.toString());
}
/** pickling support */
@ExposedMethod
public PyObject StringIO___setstate__(PyObject state) {
if (!(state instanceof PyTuple) || state.__len__() < 4) {
throw Py.ErrFormat(Py.TypeError, "%.200s.__setstate__ argument should be 4-tuple, got %.200s",
getType().fastGetName(), state.getType().fastGetName());
}
return this;
}
@ExposedMethod
public PyObject StringIO___getstate__() {
PyObject d = dict;
if (d == null) d = Py.None;
return new PyTuple(new PyUnicode(buf), readnl, new PyLong(pos), d);
}
// would be nicer if we directly imported from os, but crazy to do so
// since in python code itself
private static class os {
public static final int SEEK_SET = 0;
public static final int SEEK_CUR = 1;
public static final int SEEK_END = 2;
}
}