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

  1. package como.irc;
  2.  
  3. import java.io.*;
  4.  
  5. /**
  6.  * Converts not-liked IRC-Characters to characters that an IRC-Daemon
  7.  * understands.
  8.  */
  9. public class IRCOutputStream extends OutputStream {
  10.     /**
  11.      * The actual input stream.
  12.      */
  13.     protected OutputStream out;
  14.  
  15.     /**
  16.      * Creates an output stream filter, filtering out and replacing escapechars
  17.      * @param in    the input stream
  18.      */
  19.     public IRCOutputStream(OutputStream out) {
  20.     this.out= out;
  21.     }
  22.  
  23.     /**
  24.      * Writes a byte. Will block until the byte is actually
  25.      * written.
  26.      * @param b the byte
  27.      * @exception IOException If an I/O error has occurred.
  28.      */
  29.     public void write(int b) throws IOException {
  30.     
  31.     if (b == (int)'\r') { out.write('\\'); out.write('r'); }
  32.     else if (b == (int)'\n') { out.write('\\'); out.write('n'); }
  33.     else if (b == (int)'\0') { out.write('\\'); out.write('0'); }
  34.     else if (b == (int)'\\') { out.write('\\'); out.write('\\'); }
  35.     else out.write(b);
  36.     }
  37.  
  38.     /**
  39.      * Writes an array of bytes. Will block until the bytes
  40.      * are actually written.
  41.      * @param b    the data to be written
  42.      * @exception IOException If an I/O error has occurred.
  43.      */
  44.     public void write(byte b[]) throws IOException {
  45.     write(b, 0, b.length);
  46.     }
  47.  
  48.     /**
  49.      * Writes a sub array of bytes. To be efficient it should
  50.      * be overridden in a subclass. 
  51.      * @param b    the data to be written
  52.      * @param off    the start offset in the data
  53.      * @param len    the number of bytes that are written
  54.      * @exception IOException If an I/O error has occurred.
  55.      */
  56.     public void write(byte b[], int off, int len) throws IOException {
  57.     for (int i = 0 ; i < len ; i++) {
  58.         write(b[off + i]);
  59.     }
  60.     }
  61.  
  62.     /**
  63.      * Flushes the stream. This will write any buffered
  64.      * output bytes.
  65.      * @exception IOException If an I/O error has occurred.
  66.      */
  67.     public void flush() throws IOException {
  68.     out.flush();
  69.     }
  70.  
  71.     /**
  72.      * Closes the stream. This method must be called
  73.      * to release any resources associated with the
  74.      * stream.
  75.      * @exception IOException If an I/O error has occurred.
  76.      */
  77.     public void close() throws IOException {
  78.     flush();
  79.     out.close();
  80.     }
  81. }
  82.