-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathTextIOBase.java
More file actions
332 lines (289 loc) · 8.99 KB
/
TextIOBase.java
File metadata and controls
332 lines (289 loc) · 8.99 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
/* Copyright (c) 2007 Jython Developers */
package org.python.core.io;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import org.python.core.BufferProtocol;
import org.python.core.Py;
import org.python.core.PyArray;
import org.python.core.PyBUF;
import org.python.core.PyBuffer;
import org.python.core.PyObject;
/**
* Base class for text I/O.
*
* This class provides a character and line based interface to stream
* I/O.
*
* @author Philip Jenvey
*/
public abstract class TextIOBase extends IOBase {
/** The size of chunks read for readline */
public static final int CHUNK_SIZE = 300;
/** Byte representation of the Carriage Return character */
protected static final byte CR_BYTE = 13;
/** The underlying buffered i/o stream */
protected BufferedIOBase bufferedIO;
/** The readahead buffer. Though the underlying stream is
* sometimes buffered, readahead is specific to the TextIO layer
* to mostly benefit readline processing */
protected ByteBuffer readahead;
/** Builds the final String returned from readline */
protected StringBuilder builder;
/** An interim buffer for builder; readline loops move bytes into
* this array to avoid StringBuilder.append method calls */
protected char[] interimBuilder;
/**
* Contruct a TextIOBase wrapping the given BufferedIOBase.
*
* @param bufferedIO a BufferedIOBase to wrap
*/
public TextIOBase(BufferedIOBase bufferedIO) {
this.bufferedIO = bufferedIO;
readahead = ByteBuffer.allocate(CHUNK_SIZE);
readahead.flip();
builder = new StringBuilder(CHUNK_SIZE);
interimBuilder = new char[CHUNK_SIZE];
}
/**
* Read and return up to size bytes, contained in a String.
*
* Returns an empty String on EOF
*
* @param size the number of bytes to read
* @return a String containing the bytes read
*/
public String read(int size) {
unsupported("read");
return null;
}
/**
* Read until EOF.
*
* @return a String containing the bytes read
*/
public String readall() {
unsupported("readall");
return null;
}
/**
* Read until size, newline or EOF.
*
* Returns an empty string if EOF is hit immediately.
*
* @param size the number of bytes to read
* @return a String containing the bytes read
*/
public String readline(int size) {
unsupported("read");
return null;
}
/**
* Read into the given PyObject that implements the Jython buffer API (with write access) or is
* a PyArray.
*
* @param buf a PyObject compatible with the buffer API
* @return the amount of data read as an int
*/
public int readinto(PyObject buf) {
if (buf instanceof PyArray) {
// PyArray has the buffer interface (for bytes) but this way we can read any type
PyArray array = (PyArray) buf;
String read = read(array.__len__() * array.getItemsize());
array.fromstring(0, read);
return read.length();
} else if (buf instanceof BufferProtocol) {
try (PyBuffer view = ((BufferProtocol)buf).getBuffer(PyBUF.FULL_RO)) {
if (view.isReadonly()) {
// More helpful than falling through to CPython message
throw Py.TypeError("cannot read into read-only " + buf.getType().fastGetName());
} else {
// Inefficiently, we have to go via a String
String read = read(view.getLen());
int n = read.length();
for (int i = 0; i < n; i++) {
view.storeAt((byte)read.charAt(i), i);
}
return read.length();
}
}
}
// No valid alternative worked
throw Py.TypeError("argument 1 must be read-write buffer, not "
+ buf.getType().fastGetName());
}
/**
* Write the given String to the IO stream.
*
* Returns the number of characters written.
*
* @param buf a String value
* @return the number of characters written as an int
*/
public int write(String buf) {
unsupported("write");
return -1;
}
@Override
public long truncate(long pos) {
long initialPos = tell();
flush();
pos = bufferedIO.truncate(pos);
// FileChannel resets the position to the truncated size if
// the position was larger, whereas Python expects the
// original position
if (initialPos > pos) {
seek(initialPos);
}
return pos;
}
@Override
public void flush() {
bufferedIO.flush();
}
@Override
public void close() {
bufferedIO.close();
}
@Override
public long seek(long pos, int whence) {
pos = bufferedIO.seek(pos, whence);
clearReadahead();
return pos;
}
@Override
public long tell() {
return bufferedIO.tell() - readahead.remaining();
}
@Override
public RawIOBase fileno() {
return bufferedIO.fileno();
}
@Override
public boolean isatty() {
return bufferedIO.isatty();
}
@Override
public boolean readable() {
return bufferedIO.readable();
}
@Override
public boolean writable() {
return bufferedIO.writable();
}
@Override
public boolean closed() {
return bufferedIO.closed();
}
@Override
public InputStream asInputStream() {
return bufferedIO.asInputStream();
}
@Override
public OutputStream asOutputStream() {
return bufferedIO.asOutputStream();
}
/**
* Return the known Newline types, as a PyObject, encountered
* while reading this file.
*
* Returns None for all modes except universal newline mode.
*
* @return a PyObject containing all encountered Newlines, or None
*/
public PyObject getNewlines() {
return Py.None;
}
/**
* Return true if the file pointer is currently at EOF.
*
* If the file pointer is not at EOF, the readahead will contain
* at least one byte after this method is called.
*
* @return true if the file pointer is currently at EOF
*/
protected boolean atEOF() {
return readahead.hasRemaining() ? false : readChunk() == 0;
}
/**
* Read a chunk of data of size CHUNK_SIZE into the readahead
* buffer. Returns the amount of data read.
*
* @return the amount of data read
*/
protected int readChunk() {
// Prepare the readahead for reading
readahead.clear();
if (readahead.remaining() > CHUNK_SIZE) {
// Limit potential full reads on a resized readahead to CHUNK_SIZE
readahead.limit(readahead.position() + CHUNK_SIZE);
}
bufferedIO.read1(readahead);
readahead.flip();
return readahead.remaining();
}
/**
* Read a chunk of data of the given size into the readahead
* buffer. Returns the amount of data read.
*
* Enforces a minimum size of CHUNK_SIZE.
*
* @param size the desired size of the chunk
* @return the amount of data read
*/
protected int readChunk(int size) {
// Prepare the readahead for reading
if (size > CHUNK_SIZE) {
// More than we can hold; reallocate a larger readahead
readahead = ByteBuffer.allocate(size);
} else {
size = CHUNK_SIZE;
readahead.clear().limit(size);
}
bufferedIO.readinto(readahead);
readahead.flip();
return readahead.remaining();
}
/**
* Restore the readahead to its original size (CHUNK_SIZE) if it
* was previously resized.
*
* The readahead contents are preserved. This method assumes it
* contains a number of remaining elements less than or equal to
* CHUNK_SIZE.
*
*/
protected void packReadahead() {
if (readahead.capacity() > CHUNK_SIZE) {
ByteBuffer old = readahead;
readahead = ByteBuffer.allocate(CHUNK_SIZE);
readahead.put(old);
readahead.flip();
}
}
/**
* Clear and reset the readahead buffer.
*
*/
protected void clearReadahead() {
readahead.clear().flip();
}
/**
* Return the String result of the builder, and reset it/perform
* cleanup on it.
*
* @return the result of builder.toString()
*/
protected String drainBuilder() {
String result = builder.toString();
if (builder.capacity() > CHUNK_SIZE) {
// The builder was resized; potentially to a large
// value. Create a smaller one so the old one can be
// garbage collected
builder = new StringBuilder(CHUNK_SIZE);
} else {
builder.setLength(0);
}
return result;
}
}