-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathBufferedReader.java
More file actions
162 lines (138 loc) · 4.14 KB
/
BufferedReader.java
File metadata and controls
162 lines (138 loc) · 4.14 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
/* Copyright (c) 2007 Jython Developers */
package org.python.core.io;
import java.nio.ByteBuffer;
/**
* Buffer for a readable sequential RawIO object.
*
* @author Philip Jenvey
*/
public class BufferedReader extends BufferedIOMixin {
/** The underlying buffer */
protected ByteBuffer buffer;
/**
* Construct a BufferedReader of bufferSize, wrapping the given
* RawIOBase.
*
* @param rawIO {@inheritDoc}
* @param bufferSize {@inheritDoc}
*/
public BufferedReader(RawIOBase rawIO, int bufferSize) {
super(rawIO, bufferSize);
rawIO.checkReadable();
buffer = ByteBuffer.allocate(this.bufferSize);
clear();
}
@Override
public int readinto(ByteBuffer bytes) {
return readinto(bytes, true);
}
@Override
protected int readinto(ByteBuffer bytes, boolean buffered) {
int size = bytes.remaining();
if (size == 0) {
return 0;
}
if (buffer.remaining() >= size) {
// Fulfill the read entirely from the buffer
int bufferLimit = buffer.limit();
buffer.limit(buffer.position() + size);
bytes.put(buffer);
buffer.limit(bufferLimit);
return size;
}
// Drain the buffer then request more from the RawIO
bytes.put(buffer);
long read;
if (buffered) {
// Only attempt one read. The buffering layer should not wait
// for more data (block) to fulfill the entire read.
// Use scatter I/O read operation to read (potentially) farther
// than suggested in `bytes.remaining()` to fill in internal
// buffer `buffer`, that can be used to fulfill future read requests.
buffer.clear();
read = rawIO.readinto(new ByteBuffer[]{bytes, buffer});
read -= buffer.flip().limit();
} else {
// Clear internal buffer and only read up to specified size
// (implicit in `bytes.remaining()`) from the underlying stream
clear();
read = rawIO.readinto(bytes);
}
// This is an int after subtracting the buffer size anyway
return (int) read;
}
@Override
public ByteBuffer readall() {
ByteBuffer remaining = rawIO.readall();
if (!buffer.hasRemaining()) {
return remaining;
}
ByteBuffer all = ByteBuffer.allocate(buffer.remaining() + remaining.remaining());
all.put(buffer);
clear();
all.put(remaining);
all.flip();
return all;
}
@Override
public ByteBuffer peek(int size) {
if (buffer.remaining() < Math.min(size, bufferSize)) {
// Prepare to fill the buffer
if (buffer.position() == 0) {
buffer.limit(buffer.capacity());
} else {
buffer.compact();
}
rawIO.readinto(buffer);
buffer.flip();
}
return buffer;
}
@Override
public int read1(ByteBuffer bytes) {
int size = bytes.remaining();
if (size == 0) {
return 0;
}
if (bufferSize > 0) {
peek(1);
int bufferedSize = buffer.remaining();
if (bufferedSize < size) {
bytes.limit(bytes.position() + bufferedSize);
}
}
return readinto(bytes);
}
@Override
public long tell() {
return rawIO.tell() - buffer.remaining();
}
@Override
public long seek(long pos, int whence) {
if (whence == 1) {
pos -= buffer.remaining();
}
pos = rawIO.seek(pos, whence);
clear();
return pos;
}
@Override
public boolean buffered() {
return buffer.hasRemaining();
}
@Override
public void clear() {
buffer.clear().limit(0);
}
@Override
public int write(ByteBuffer bytes) {
// Never writable; just raise the appropriate exception
checkClosed();
checkWritable();
return -1;
}
@Override
public boolean writable() {
return false;
}
}