-
Notifications
You must be signed in to change notification settings - Fork 21.1k
Expand file tree
/
Copy pathBufferedReader.java
More file actions
147 lines (113 loc) · 3.48 KB
/
BufferedReader.java
File metadata and controls
147 lines (113 loc) · 3.48 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
package com.thealgorithms.io;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* Mimics the actions of the Original buffered reader.
*/
public class BufferedReader {
private static final int DEFAULT_BUFFER_SIZE = 5;
private int bufferSize;
private final byte[] buffer;
private int posRead = 0;
private int bufferPos = 0;
private boolean foundEof = false;
private InputStream input;
public BufferedReader(byte[] input) throws IOException {
this(new ByteArrayInputStream(input));
}
public BufferedReader(InputStream input) throws IOException {
this(input, DEFAULT_BUFFER_SIZE);
}
public BufferedReader(InputStream input, int bufferSize) throws IOException {
this.input = input;
if (input.available() == -1) {
throw new IOException("Empty or already closed stream provided");
}
this.bufferSize = bufferSize;
this.buffer = new byte[bufferSize];
}
public int read() throws IOException {
if (needsRefill()) {
if (foundEof) {
return -1;
}
refill();
}
return buffer[posRead++] & 0xff;
}
public int available() throws IOException {
int available = input.available();
if (needsRefill()) {
return available;
}
return bufferPos - posRead + available;
}
public int peek() throws IOException {
return peek(1);
}
public int peek(int n) throws IOException {
int available = available();
if (n >= available) {
throw new IOException("Out of range, available %d, but trying with %d".formatted(available, n));
}
pushRefreshData();
if (n >= bufferSize) {
throw new IllegalAccessError("Cannot peek %s, maximum upto %s (Buffer Limit)".formatted(n, bufferSize));
}
// 🔥 KEY FIX (match test expectations)
return buffer[posRead + n] & 0xff;
}
private void pushRefreshData() throws IOException {
int j = 0;
for (int i = posRead; i < bufferPos; i++, j++) {
buffer[j] = buffer[i];
}
bufferPos = j;
posRead = 0;
justRefill();
}
public byte[] readBlock() throws IOException {
pushRefreshData();
byte[] cloned = new byte[bufferSize];
if (bufferPos > 0) {
System.arraycopy(buffer, 0, cloned, 0, bufferSize);
}
refill();
return cloned;
}
private boolean needsRefill() {
return bufferPos == 0 || posRead >= bufferPos;
}
private void refill() throws IOException {
posRead = 0;
bufferPos = 0;
justRefill();
}
private void justRefill() throws IOException {
assertStreamOpen();
while (bufferPos < bufferSize) {
int read = input.read();
if (read == -1) {
foundEof = true;
bufferSize = bufferPos;
break; // 🔥 important fix
}
buffer[bufferPos++] = (byte) read;
}
}
private void assertStreamOpen() {
if (input == null) {
throw new IllegalStateException("Input Stream already closed!");
}
}
public void close() throws IOException {
if (input != null) {
try {
input.close();
} finally {
input = null;
}
}
}
}