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

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