-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathcStringIO.java
More file actions
504 lines (447 loc) · 16.4 KB
/
cStringIO.java
File metadata and controls
504 lines (447 loc) · 16.4 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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
/*
* Copyright 1998 Finn Bock.
*
* This program contains material copyrighted by:
* Copyright (c) 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
* The Netherlands.
*/
// cStringIO with StringBuilder semantics - don't use without using external
// synchronization. Java does provide other alternatives.
package org.python.modules;
import org.python.core.Py;
import org.python.core.PyArray;
import org.python.core.PyIterator;
import org.python.core.PyList;
import org.python.core.PyObject;
import org.python.core.PyString;
import org.python.core.PyUnicode;
import org.python.core.PyType;
/**
* This module implements a file-like class, StringIO, that reads and
* writes a string buffer (also known as memory files).
* See the description on file objects for operations.
* @author Finn Bock, bckfnn@pipmail.dknet.dk
* @version cStringIO.java,v 1.10 1999/05/20 18:03:20 fb Exp
*/
public class cStringIO {
public static final String __doc__ =
"A simple fast partial StringIO replacement.\n" +
"\n" +
"This module provides a simple useful replacement for\n" +
"the StringIO module that is written in Java. It does not provide the\n" +
"full generality of StringIO, but it provides enough for most\n" +
"applications and is especially useful in conjunction with the\n" +
"pickle module.\n" +
"\n" +
"Usage:\n" +
"\n" +
" from cStringIO import StringIO\n" +
"\n" +
" an_output_stream=StringIO()\n" +
" an_output_stream.write(some_stuff)\n" +
" ...\n" +
" value=an_output_stream.getvalue()\n" +
"\n" +
" an_input_stream=StringIO(a_string)\n" +
" spam=an_input_stream.readline()\n" +
" spam=an_input_stream.read(5)\n" +
" an_input_stream.seek(0) # OK, start over\n" +
" spam=an_input_stream.read() # and read it all\n" +
" \n" +
"If someone else wants to provide a more complete implementation,\n" +
"go for it. :-) \n" +
"\n" +
"cStringIO.java,v 1.10 1999/05/20 18:03:20 fb Exp\n" +
"Python-level doc was inserted on 2017/02/01, copied from\n" +
"cStringIO.c,v 1.29 1999/06/15 14:10:27 jim Exp\n";
// 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;
}
public static PyType InputType = PyType.fromClass(StringIO.class);
public static PyType OutputType = PyType.fromClass(StringIO.class);
public static final String __doc__StringIO =
"StringIO([s]) -- Return a StringIO-like stream for reading or writing";
public static StringIO StringIO() {
return new StringIO();
}
/**
* Create a StringIO object, initialized by the value.
* @param buffer The initial value.
* @return a new StringIO object.
*/
public static StringIO StringIO(CharSequence buffer) {
return new StringIO(buffer);
}
/**
* Create a StringIO object, initialized by an array's byte stream.
* @param array The initial value, from an array.
* @return a new StringIO object.
*/
public static StringIO StringIO(PyArray array) {
return new StringIO(array);
}
/**
* The StringIO object
* @see cStringIO#StringIO()
* @see cStringIO#StringIO(CharSequence)
*/
public static class StringIO extends PyIterator {
public boolean softspace = false;
public boolean closed = false;
public int pos = 0;
private final StringBuilder buf;
public StringIO() {
buf = new StringBuilder();
}
public StringIO(CharSequence buffer) {
buf = new StringBuilder(buffer instanceof PyUnicode ?
((PyUnicode) buffer).encode() : buffer);
}
public StringIO(PyArray array) {
buf = new StringBuilder(array.tostring());
}
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;
}
@Override
public void __setattr__(String name, PyObject value) {
if (name == "softspace") {
softspace = value.__nonzero__();
return;
}
super.__setattr__(name, value);
}
@Override
public PyObject __iternext__() {
_complain_ifclosed();
PyString r = readline();
if (r.__len__() == 0) {
return null;
}
return r;
}
public static final String __doc__close = "close(): explicitly release resources held.";
/**
* 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;
}
public static final String __doc__isatty = "isatty(): always returns 0";
/**
* Return false.
* @return false.
*/
public boolean isatty() {
_complain_ifclosed();
return false;
}
public static final String __doc__seek =
"seek(position) -- set the current position\n" +
"seek(position, mode) -- mode 0: absolute; 1: relative; 2: relative to EOF";
/**
* 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;
}
}
public static final String __doc__reset =
"reset() -- Reset the file position to the beginning";
/**
* Reset the file position to the beginning of the file.
*/
public synchronized void reset() {
pos = 0;
}
public static final String __doc__tell = "tell() -- get the current position.";
/**
* Return the file position.
* @return the position in the file.
*/
public synchronized int tell() {
_complain_ifclosed();
return pos;
}
public static final String __doc__read =
"read([s]) -- Read s characters, or the rest of the string";
/**
* Read all data until EOF is reached.
* An empty string is returned when EOF is encountered immediately.
* @return A string containing the data.
*/
public PyString read() {
return 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.
*/
public synchronized PyString 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 PyString(substr);
}
public static final String __doc__readline = "readline() -- Read one line";
/**
* 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 PyString readline() {
return 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.
*/
public synchronized PyString readline(long size) {
_complain_ifclosed();
_convert_to_int(size);
int len = buf.length();
if (pos == len) {
return new PyString("");
}
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 PyString(r);
}
/**
* Read and return a line without the trailing newline.
* Usind by cPickle as an optimization.
*/
public synchronized PyString 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) {
pos++;
}
return new PyString(r);
}
public static final String __doc__readlines = "readlines() -- Read all lines";
/**
* Read until EOF using readline() and return a list containing
* the lines thus read.
* @return a list of the lines.
*/
public PyObject readlines() {
return readlines(0);
}
/**
* Read until EOF using readline() and return a list containing
* the lines thus read.
* @return a list of the lines.
*/
public PyObject readlines(long sizehint) {
_complain_ifclosed();
int sizehint_int = (int)sizehint;
int total = 0;
PyList lines = new PyList();
PyString line = readline();
while (line.__len__() > 0) {
lines.append(line);
total += line.__len__();
if (0 < sizehint_int && sizehint_int <= total) {
break;
}
line = readline();
}
return lines;
}
public static final String __doc__truncate =
"truncate(): truncate the file at the current position.";
/**
* truncate the file at the current position.
*/
public synchronized void truncate() {
buf.setLength(this.pos);
}
/**
* truncate the file at the position pos.
*/
public synchronized void 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;
}
public static final String __doc__write =
"write(s) -- Write a string to the file" +
"\n\nNote (hack:) writing None resets the buffer";
/**
* 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 cPickle 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);
}
public static final String __doc__writelines =
"writelines(sequence_of_strings) -> None. Write the strings to the file.\n" +
"\n" +
"Note that newlines are not added. The sequence can be any iterable object\n" +
"producing strings. This is equivalent to calling write() for each string.";
/**
* Write a list of strings to the file.
*/
public void writelines(PyObject lines) {
for (PyObject line : lines.asIterable()) {
write(line);
}
}
public static final String __doc__flush = "flush(): does nothing.";
/**
* Flush the internal buffer. Does nothing.
*/
public void flush() {
_complain_ifclosed();
}
public static final String __doc__getvalue =
"getvalue([use_pos]) -- Get the string value." +
"\n" +
"If use_pos is specified and is a true value, then the string returned\n" +
"will include only the text up to the current file position.\n";
/**
* 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 PyString getvalue() {
_complain_ifclosed();
return new PyString(buf.toString());
}
}
private static String[] strings = new String[256];
static String getString(char ch) {
if (ch > 255) {
return new String(new char[] { ch });
}
String s = strings[ch];
if (s == null) {
s = new String(new char[] { ch });
strings[ch] = s;
}
return s;
}
}