Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package poke;
- import java.io.Closeable;
- import java.io.IOException;
- import java.io.Reader;
- public class RingBuf implements Closeable {
- private static final int BUFSIZE = 64;
- private char[] ringBuf = new char[BUFSIZE];
- private int rbIdx = 0;
- Reader r;
- public RingBuf(Reader reader) {
- r = reader;
- }
- public int read() throws IOException {
- int ch;
- if ((ch = r.read()) == -1)
- return -1;
- ringBuf[rbIdx] = (char)ch;
- rbIdx = (rbIdx + 1) % BUFSIZE;
- return ch;
- }
- public boolean justRead(char[] needle) {
- int rbi = rbIdx;
- int i;
- for (i=needle.length-1; i >= 0; i--) {
- rbi = (rbi == 0) ? BUFSIZE - 1 : rbi - 1;
- if (ringBuf[rbi] != needle[i])
- return false;
- }
- return true;
- }
- public String readUntil(char[] terminator) throws IOException {
- StringBuilder sb = new StringBuilder();
- int c;
- while ((c = read()) != -1) {
- if (justRead(terminator))
- return sb.toString();
- sb.append((char)c);
- }
- return sb.toString();
- }
- public void close() throws IOException {
- r.close();
- }
- }
Add Comment
Please, Sign In to add comment