home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 14 / IOPROG_14.ISO / soft / sdkjava / dxma.exe / DXMA05.cab / samples / da / java / apps / chess / TokenInputStream.java < prev   
Encoding:
Java Source  |  1997-11-13  |  2.6 KB  |  131 lines

  1. import java.io.*;
  2.  
  3. /*
  4.  *
  5.  * TokenInputStream
  6.    This class will read until a token is met, and will 
  7.    eat all whitespace.
  8.  *
  9.  */
  10. public class TokenInputStream extends FilterInputStream
  11. {
  12.     protected InputStream in;
  13.      protected byte buf[] = new byte[256];
  14.      protected int buf_offset = 0;
  15.      protected int buf_left = 0;
  16.      protected char white[] = null;
  17.  
  18.      public TokenInputStream(InputStream input) {
  19.          super(input);
  20.          in = input;
  21.         setWhite("\r\n\t ");
  22.  
  23.      }
  24.      
  25.      public void setWhite(String s) {
  26.          white = new char[s.length()];
  27.          s.getChars(0,s.length(),white,0);         
  28.      }
  29.  
  30.         
  31.      public int read(char b[], int  off, int  len) throws IOException{
  32.             
  33.          int copied = 0;
  34.          if (buf_left == -1)
  35.              return -1;
  36.          
  37.                 
  38.          while (len > 0) {    
  39.             if (buf_left == 0) {
  40.             // copy more into buffer    
  41.                 buf_offset = 0;
  42.                 buf_left = in.read(buf, 0, buf.length);
  43.                 if (buf_left < 0)
  44.                     return copied;
  45.             }
  46.             
  47.             // for each char in the buffer
  48.             for (;buf_left > 0; buf_offset++, buf_left--){
  49.                 boolean isToken = false;
  50.                 // compare with white
  51.                 for (int j = 0; j < white.length; j++) {
  52.                     if (buf[buf_offset] == white[j]){
  53.                         isToken = true;
  54.                         break;
  55.                     }
  56.                 }
  57.                 if (isToken && (copied != 0))
  58.                     return copied;
  59.                 b[off] =(char) buf[buf_offset];
  60.                 off++;
  61.                 len--;
  62.                 copied++;
  63.             }    
  64.         }
  65.          return copied;
  66.      }
  67.                             
  68.      public char peek() throws IOException {
  69.  
  70.          while(true) {
  71.  
  72.              if (buf_left == 0) {
  73.                  // copy more into buffer    
  74.                  buf_offset = 0;
  75.                  buf_left = in.read(buf, 0, buf.length);
  76.              }
  77.              if (buf_left == -1)
  78.                  return 0;
  79.              
  80.              // for each char in the buffer
  81.              for (; buf_left > 0; buf_left --, buf_offset++){
  82.                  boolean token = false;
  83.                  // compare with white
  84.                  for (int j = 0; j < white.length; j++) {
  85.                      if (buf[buf_offset] == white[j]){
  86.                          token = true;
  87.                          break;
  88.                      }
  89.                  }
  90.                  // not a token
  91.                  if (!token)
  92.                      return (char)buf[buf_offset];        
  93.              }    
  94.          }    
  95.      }
  96.  
  97.     public void eatWhite() throws IOException{
  98.  
  99.         while(true) {
  100.  
  101.             if (buf_left == 0) {
  102.                  // copy more into buffer    
  103.                 buf_offset = 0;
  104.                 buf_left = in.read(buf, 0, buf.length);
  105.             }
  106.             if (buf_left == -1)
  107.                 return;
  108.             
  109.              // for each char in the buffer
  110.             for (; buf_left > 0; buf_left --, buf_offset++){
  111.                 // compare with white
  112.                 if (buf[buf_offset] == ' ' ||
  113.                     buf[buf_offset] == '\t' ||         
  114.                     buf[buf_offset] == '\r' ||         
  115.                     buf[buf_offset] == '\n'){
  116.                          continue;
  117.                 } else
  118.                     return;
  119.             }
  120.         }    
  121.     }    
  122. }
  123.  
  124.  
  125.  
  126.          
  127.  
  128.  
  129.  
  130.  
  131.