Guest User

Untitled

a guest
Jun 25th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.04 KB | None | 0 0
  1. package poke;
  2. import java.io.Closeable;
  3. import java.io.IOException;
  4. import java.io.Reader;
  5.  
  6. public class RingBuf implements Closeable {
  7.     private static final int BUFSIZE = 64;
  8.     private char[] ringBuf = new char[BUFSIZE];
  9.     private int rbIdx = 0;
  10.    
  11.     Reader r;
  12.     public RingBuf(Reader reader) {
  13.         r = reader;
  14.     }
  15.     public int read() throws IOException {
  16.         int ch;
  17.         if ((ch = r.read()) == -1)
  18.             return -1;
  19.         ringBuf[rbIdx] = (char)ch;
  20.         rbIdx = (rbIdx + 1) % BUFSIZE;
  21.         return ch;
  22.     }
  23.     public boolean justRead(char[] needle) {
  24.         int rbi = rbIdx;
  25.         int i;
  26.         for (i=needle.length-1; i >= 0; i--) {
  27.             rbi = (rbi == 0) ? BUFSIZE - 1 : rbi - 1;
  28.             if (ringBuf[rbi] != needle[i])
  29.                 return false;
  30.         }
  31.         return true;
  32.     }
  33.     public String readUntil(char[] terminator) throws IOException {
  34.         StringBuilder sb = new StringBuilder();
  35.         int c;
  36.         while ((c = read()) != -1) {
  37.             if (justRead(terminator))
  38.                 return sb.toString();
  39.             sb.append((char)c);
  40.         }
  41.         return sb.toString();
  42.     }
  43.     public void close() throws IOException {
  44.         r.close();
  45.     }
  46. }
Add Comment
Please, Sign In to add comment