Skip to content

Commit e9d28f7

Browse files
committed
Add UnsynchronizedBufferedReader.peek()
1 parent abc2c1b commit e9d28f7

2 files changed

Lines changed: 208 additions & 45 deletions

File tree

src/main/java/org/apache/commons/io/input/UnsynchronizedBufferedReader.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,35 @@ public boolean markSupported() {
209209
return true;
210210
}
211211

212+
/**
213+
* Returns the next character in the current reader without consuming it. So the next call to {@link #read()} will still return this value.
214+
*
215+
* @return the next character
216+
* @throws IOException If an I/O error occurs
217+
*/
218+
public int peek() throws IOException {
219+
mark(1);
220+
final int c = read();
221+
reset();
222+
return c;
223+
}
224+
225+
/**
226+
* Populates the buffer with the next {@code buf.length} characters in the current reader without consuming them. The next call to {@link #read()} will
227+
* still return the next value.
228+
*
229+
* @param buf the buffer to fill for the look ahead.
230+
* @return the buffer itself
231+
* @throws IOException If an I/O error occurs
232+
*/
233+
public int peek(final char[] buf) throws IOException {
234+
final int n = buf.length;
235+
mark(n);
236+
final int c = read(buf, 0, n);
237+
reset();
238+
return c;
239+
}
240+
212241
/**
213242
* Reads a single character from this reader and returns it with the two higher-order bytes set to 0. If possible, BufferedReader returns a character from
214243
* the buffer. If there are no characters available in the buffer, it fills the buffer and then returns a character. It returns -1 if there are no more
@@ -445,4 +474,5 @@ public long skip(final long amount) throws IOException {
445474
}
446475
return amount;
447476
}
477+
448478
}

0 commit comments

Comments
 (0)