home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / in4wjcxu / src / como / irc / ircinputstream.java.org < prev    next >
Encoding:
Text File  |  1996-08-14  |  4.1 KB  |  139 lines

  1. package como.irc;
  2.  
  3. import java.io.InputStream;
  4. import java.io.IOException;
  5.  
  6. /**
  7.  */
  8. public class IRCInputStream extends InputStream {
  9.     /**
  10.      * The actual input stream.
  11.      */
  12.     protected InputStream in;
  13.  
  14.     /**
  15.      * Creates an input stream filter, filtering out and replacing escapechars
  16.      * @param in    the input stream
  17.      */
  18.     public IRCInputStream(InputStream in) {
  19.     this.in = in;
  20.    }
  21.  
  22.     /**
  23.      * Reads a byte. Will block if no input is available.
  24.      * @return     the byte read, or -1 if the end of the
  25.      *        stream is reached.
  26.      * @exception IOException If an I/O error has occurred.
  27.      */
  28.     public int read() throws IOException {
  29.     int r= in.read();
  30.     if (r == '\\')  {
  31.     r = in.read() ;
  32.     if (r == 'r') r = '\r';
  33.     if (r == 'n') r = '\n';
  34.     if (r == '0') r = '\0';
  35.     // if r = // just leave it.
  36.     }
  37.     System.out.println("="+r);
  38.     return r;
  39.     }
  40.  
  41.     /**
  42.      * Reads into an array of bytes.
  43.      * Blocks until some input is available.
  44.      * @param b    the buffer into which the data is read
  45.      * @return  the actual number of bytes read, -1 is
  46.      *         returned when the end of the stream is reached.
  47.      * @exception IOException If an I/O error has occurred.
  48.      */
  49.     public int read(byte b[]) throws IOException {
  50.     return read(b, 0, b.length);
  51.     }
  52.  
  53.     /**
  54.      * Reads into an array of bytes.
  55.      * Blocks until some input is available.
  56.      * This method should be overridden in a subclass for
  57.      * efficiency (the default implementation reads 1 byte
  58.      * at a time).
  59.      * @param b    the buffer into which the data is read
  60.      * @param off the start offset of the data
  61.      * @param len the maximum number of bytes read
  62.      * @return  the actual number of bytes read, -1 is
  63.      *         returned when the end of the stream is reached.
  64.      * @exception IOException If an I/O error has occurred.
  65.      */
  66.     public int read(byte b[], int off, int len) throws IOException {
  67.     int i=0;
  68.     for (i=0;i<len;i++)
  69.         b[off+i] = (byte)read();
  70.     return i;
  71.     }
  72.  
  73.     /**
  74.      * Skips bytes of input. 
  75.      * @param n     bytes to be skipped
  76.      * @return    actual number of bytes skipped
  77.      * @exception IOException If an I/O error has occurred.
  78.      */
  79.     public long skip(long n) throws IOException {
  80.     byte[] b = new byte[(int)n];
  81.     return read(b,0,(int)n);
  82.     }
  83.  
  84.     /**
  85.      * Returns the number of bytes that can be read.
  86.      * without blocking.
  87.      * @return the number of available bytes
  88.      */
  89.     public int available() throws IOException {
  90.     return in.available();
  91.     }
  92.  
  93.     /**
  94.      * Closes the input stream. Must be called
  95.      * to release any resources associated with
  96.      * the stream.
  97.      * @exception IOException If an I/O error has occurred.
  98.      */
  99.     public void close() throws IOException {
  100.     in.close();
  101.     }
  102.  
  103.     /**
  104.      * Marks the current position in the input stream.  A subsequent
  105.      * call to reset() will reposition the stream at the last
  106.      * marked position so that subsequent reads will re-read
  107.      * the same bytes.  The stream promises to allow readlimit bytes
  108.      * to be read before the mark position gets invalidated.
  109.      * @param readlimit the maximum limit of bytes allowed tobe read before the
  110.      * mark position becomes invalid.
  111.      */
  112.     public synchronized void mark(int readlimit) {
  113.     in.mark(readlimit);
  114.     }
  115.  
  116.     /**
  117.      * Repositions the stream to the last marked position.  If the
  118.      * stream has not been marked, or if the mark has been invalidated,
  119.      * an IOException is thrown.  Stream marks are intended to be used in
  120.      * situations where you need to read ahead a little to see what's in
  121.      * the stream.  Often this is most easily done by invoking some
  122.      * general parser.  If the stream is of the type handled by the
  123.      * parse, it just chugs along happily.  If the stream is *not* of
  124.      * that type, the parser should toss an exception when it fails,
  125.      * which, if it happens within readlimit bytes, allows the outer
  126.      * code to reset the stream and try another parser.
  127.      */
  128.     public synchronized void reset() throws IOException {
  129.     in.reset();
  130.     }
  131.  
  132.     /**
  133.      * Returns true if this stream type supports mark/reset
  134.      */
  135.     public boolean markSupported() {
  136.     return false; // in.markSupported();
  137.     }
  138. }
  139.