Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 21.3k
Expand file tree
/
Copy pathBufferedReader.java
More file actions
Latest commit
197 lines (165 loc) · 5.21 KB
/
Copy pathBufferedReader.java
File metadata and controls
197 lines (165 loc) · 5.21 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
packagecom.thealgorithms.io;
importjava.io.ByteArrayInputStream;
importjava.io.IOException;
importjava.io.InputStream;
/**
* Mimics the actions of the Original buffered reader
* implements other actions, such as peek(n) to lookahead,
* block() to read a chunk of size {BUFFER SIZE}
* <p>
* Author: Kumaraswamy B.G (Xoma Dev)
*/
publicclassBufferedReader {
privatestaticfinalintDEFAULT_BUFFER_SIZE = 5;
/**
* The maximum number of bytes the buffer can hold.
* Value is changed when encountered Eof to not
* cause overflow read of 0 bytes
*/
privateintbufferSize;
privatefinalbyte[] buffer;
/**
* posRead -> indicates the next byte to read
*/
privateintposRead = 0;
privateintbufferPos = 0;
privatebooleanfoundEof = false;
privateInputStreaminput;
publicBufferedReader(byte[] input) throwsIOException {
this(newByteArrayInputStream(input));
}
publicBufferedReader(InputStreaminput) throwsIOException {
this(input, DEFAULT_BUFFER_SIZE);
}
publicBufferedReader(InputStreaminput, intbufferSize) throwsIOException {
this.input = input;
if (input.available() == -1) {
thrownewIOException("Empty or already closed stream provided");
}
this.bufferSize = bufferSize;
buffer = newbyte[bufferSize];
}
/**
* Reads a single byte from the stream
*/
publicintread() throwsIOException {
if (needsRefill()) {
if (foundEof) {
return -1;
}
// the buffer is empty, or the buffer has
// been completely read and needs to be refilled
refill();
}
returnbuffer[posRead++] & 0xff; // read and un-sign it
}
/**
* Number of bytes not yet been read
*/
publicintavailable() throwsIOException {
intavailable = input.available();
if (needsRefill()) {
// since the block is already empty,
// we have no responsibility yet
returnavailable;
}
returnbufferPos - posRead + available;
}
/**
* Returns the next character
*/
publicintpeek() throwsIOException {
returnpeek(1);
}
/**
* Peeks and returns a value located at next {n}
*/
publicintpeek(intn) throwsIOException {
intavailable = available();
if (n >= available) {
thrownewIOException("Out of range, available %d, but trying with %d".formatted(available, n));
}
pushRefreshData();
if (n >= bufferSize) {
thrownewIllegalAccessError("Cannot peek %s, maximum upto %s (Buffer Limit)".formatted(n, bufferSize));
}
returnbuffer[n];
}
/**
* Removes the already read bytes from the buffer
* in-order to make space for new bytes to be filled up.
* <p>
* This may also do the job to read first time data (the whole buffer is empty)
*/
privatevoidpushRefreshData() throwsIOException {
for (inti = posRead, j = 0; i < bufferSize; i++, j++) {
buffer[j] = buffer[i];
}
bufferPos -= posRead;
posRead = 0;
// fill out the spaces that we've
// emptied
justRefill();
}
/**
* Reads one complete block of size {bufferSize}
* if found eof, the total length of an array will
* be that of what's available
*
* @return a completed block
*/
publicbyte[] readBlock() throwsIOException {
pushRefreshData();
byte[] cloned = newbyte[bufferSize];
// arraycopy() function is better than clone()
if (bufferPos >= 0) {
System.arraycopy(buffer, 0, cloned, 0,
// important to note that, bufferSize does not stay constant
// once the class is defined. See justRefill() function
bufferSize);
}
// we assume that already a chunk
// has been read
refill();
returncloned;
}
privatebooleanneedsRefill() {
returnbufferPos == 0 || posRead == bufferSize;
}
privatevoidrefill() throwsIOException {
posRead = 0;
bufferPos = 0;
justRefill();
}
privatevoidjustRefill() throwsIOException {
assertStreamOpen();
// try to fill in the maximum we can until
// we reach EOF
while (bufferPos < bufferSize) {
intread = input.read();
if (read == -1) {
// reached end-of-file, no more data left
// to be read
foundEof = true;
// rewrite the BUFFER_SIZE, to know that we've reached
// EOF when requested refill
bufferSize = bufferPos;
}
buffer[bufferPos++] = (byte) read;
}
}
privatevoidassertStreamOpen() {
if (input == null) {
thrownewIllegalStateException("Input Stream already closed!");
}
}
publicvoidclose() throwsIOException {
if (input != null) {
try {
input.close();
} finally {
input = null;
}
}
}
}