-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathUniversalIOWrapper.java
More file actions
359 lines (302 loc) · 11.2 KB
/
UniversalIOWrapper.java
File metadata and controls
359 lines (302 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
/* Copyright (c) 2007 Jython Developers */
package org.python.core.io;
import java.nio.ByteBuffer;
import java.util.EnumSet;
import org.python.core.Py;
import org.python.core.PyObject;
import org.python.core.PyString;
import org.python.core.PyTuple;
/**
* A Buffered text stream in universal newlines mode.
*
* @author Philip Jenvey
*/
public class UniversalIOWrapper extends TextIOBase {
/** Whether the next character, if it's an LF, should be skipped
* (the previous character was a CR) */
private boolean skipNextLF = false;
/** The Newlines encountered in this file */
private EnumSet<Newline> newlineTypes = EnumSet.noneOf(Newline.class);
/**
* Contruct a UniversalIOWrapper wrapping the given
* BufferedIOBase.
*
* @param bufferedIO {@inheritDoc}
*/
public UniversalIOWrapper(BufferedIOBase bufferedIO) {
super(bufferedIO);
}
@Override
public String read(int size) {
if (size < 0) {
return readall();
}
// Avoid ByteBuffer (this.readahead) method calls in the inner
// loop by reading directly from the readahead's backing array
byte[] readaheadArray;
int readaheadPos;
char[] builderArray = new char[size];
int builderPos = 0;
do {
readaheadArray = readahead.array();
readaheadPos = readahead.position();
while (readaheadPos < readahead.limit() && builderPos < size) {
char next = (char)(readaheadArray[readaheadPos++] & 0xff);
switch (next) {
case '\r':
next = '\n';
// Don't skipNextLF (identify CR immediately) at
// EOF
if (readaheadPos == readahead.limit()) {
if (readChunk() == 0) {
// EOF
newlineTypes.add(Newline.CR);
builderArray[builderPos++] = next;
return new String(builderArray, 0, builderPos);
}
// Not EOF and readChunk replaced the
// readahead; reset the readahead info
readaheadArray = readahead.array();
readaheadPos = readahead.position();
}
skipNextLF = true;
break;
case '\n':
if (skipNextLF) {
skipNextLF = false;
newlineTypes.add(Newline.CRLF);
continue;
}
newlineTypes.add(Newline.LF);
break;
default:
if (skipNextLF) {
skipNextLF = false;
newlineTypes.add(Newline.CR);
}
}
builderArray[builderPos++] = next;
}
} while (builderPos < size && readChunk(size - builderPos) > 0);
// Finally reposition the readahead to where we ended. The
// position is invalid if the readahead is empty (at EOF;
// readChunk() returned 0)
if (readahead.hasRemaining()) {
readahead.position(readaheadPos);
}
// Shrink the readahead if it grew
packReadahead();
return new String(builderArray, 0, builderPos);
}
@Override
public String readall() {
// Read the remainder of file
ByteBuffer remaining = bufferedIO.readall();
// Create an array that accommodates the readahead and the
// remainder
char[] all = new char[readahead.remaining() + remaining.remaining()];
// Consume the readahead
int length = readLoop(readahead.array(), readahead.position(), all, 0,
readahead.remaining());
readahead.position(readahead.limit());
// Consume the remainder of the file
length += readLoop(remaining.array(), remaining.position(), all, length,
remaining.remaining());
// Handle skipNextLF at EOF
if (skipNextLF) {
skipNextLF = false;
newlineTypes.add(Newline.CR);
}
return new String(all, 0, length);
}
/**
* Read and convert the src byte array into the dest char array.
*
* Converts CR and CRLF to LF. No attempt is made to handle CRLF
* at EOF; skipNextLF may be toggled true after this method
* returns.
*
* @param src the source byte array
* @param srcPos starting position in the source array
* @param dest the destination char array
* @param destPos starting position in the destination array
* @param length the number of array elements to be copied
* @return the number of chars written to the destination array
*/
private int readLoop(byte[] src, int srcPos, char[] dest, int destPos, int length) {
int destStartPos = destPos;
int srcEndPos = srcPos + length;
while (srcPos < srcEndPos) {
char next = (char)(src[srcPos++] & 0xff);
switch (next) {
case '\r':
next = '\n';
skipNextLF = true;
break;
case '\n':
if (skipNextLF) {
skipNextLF = false;
newlineTypes.add(Newline.CRLF);
continue;
}
newlineTypes.add(Newline.LF);
break;
default:
if (skipNextLF) {
skipNextLF = false;
newlineTypes.add(Newline.CR);
}
}
dest[destPos++] = next;
}
return destPos - destStartPos;
}
@Override
public String readline(int size) {
// Avoid ByteBuffer (this.readahead) and StringBuilder
// (this.builder) method calls in the inner loop by reading
// directly from the readahead's backing array and writing to
// an interim char array (this.interimBuilder)
byte[] readaheadArray;
int readaheadPos;
int interimBuilderPos;
do {
readaheadArray = readahead.array();
readaheadPos = readahead.position();
interimBuilderPos = 0;
while (readaheadPos < readahead.limit() &&
(size < 0 || builder.length() + interimBuilderPos < size)) {
char next = (char)(readaheadArray[readaheadPos++] & 0xff);
switch (next) {
case '\r':
next = '\n';
// Don't skipNextLF (identify CR immediately) at
// EOF
if (readaheadPos == readahead.limit()) {
if (readChunk() == 0) {
// EOF
newlineTypes.add(Newline.CR);
interimBuilder[interimBuilderPos++] = next;
builder.append(interimBuilder, 0, interimBuilderPos);
// The readahead position is already valid
// (it's at 0)
return drainBuilder();
}
// Not EOF and readChunk replaced the
// readahead; reset the readahead info
readaheadPos = readahead.position();
}
skipNextLF = true;
interimBuilder[interimBuilderPos++] = next;
builder.append(interimBuilder, 0, interimBuilderPos);
// Reposition the readahead to where we ended
readahead.position(readaheadPos);
return drainBuilder();
case '\n':
if (skipNextLF) {
skipNextLF = false;
newlineTypes.add(Newline.CRLF);
continue;
}
newlineTypes.add(Newline.LF);
interimBuilder[interimBuilderPos++] = next;
builder.append(interimBuilder, 0, interimBuilderPos);
// Reposition the readahead to where we ended
readahead.position(readaheadPos);
return drainBuilder();
default:
if (skipNextLF) {
skipNextLF = false;
newlineTypes.add(Newline.CR);
}
}
interimBuilder[interimBuilderPos++] = next;
}
builder.append(interimBuilder, 0, interimBuilderPos);
} while ((size < 0 || builder.length() < size) && readChunk() > 0);
// Finally reposition the readahead to where we ended. The
// position is invalid if the readahead is empty (at EOF;
// readChunk() returned 0)
if (readahead.hasRemaining()) {
readahead.position(readaheadPos);
}
return drainBuilder();
}
@Override
public int write(String buf) {
// Universal newlines doesn't support writing; just raise the
// appropriate exception
checkClosed();
checkWritable();
return -1;
}
@Override
public long seek(long pos, int whence) {
pos = super.seek(pos, whence);
skipNextLF = false;
return pos;
}
@Override
public long tell() {
long pos = super.tell();
if (skipNextLF) {
// Look for a succeeding LF; if it exists, consume it and
// report the position as the beginning of the next
// newline
if (!atEOF()) {
int readaheadPos = readahead.position();
if (readahead.get(readaheadPos) == LF_BYTE) {
skipNextLF = false;
newlineTypes.add(Newline.CRLF);
readahead.position(++readaheadPos);
pos++;
}
}
}
return pos;
}
@Override
public PyObject getNewlines() {
int size = newlineTypes.size();
if (size == 0) {
return Py.None;
} else if (size == 1) {
Newline newline = newlineTypes.iterator().next();
return new PyString(newline.getValue());
}
PyObject[] newlines = new PyObject[size];
int i = 0;
for (Newline newline : newlineTypes) {
newlines[i++] = new PyString(newline.getValue());
}
return new PyTuple(newlines);
}
/**
* Newline types.
*
*/
private enum Newline {
/** Carriage return */
CR("\r"),
/** Line feed */
LF("\n"),
/** Carriage return line feed **/
CRLF("\r\n");
/** The String value */
private final String value;
/**
* Return the String value of this newline
*
* @return the newline character as a String
*/
public String getValue() { return value; }
/**
* Construct a new Newline.
*
* @param value the newline character as a String
*/
Newline(String value) {
this.value = value;
}
}
}