KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > kawa > TelnetOutputStream


1 package kawa; // For now
2
import java.io.*;
3
4 /**
5  * An input stream tha handles the telnet protocol.
6  * Basically, the byte value IAC is doubled.
7  * In addition, various utility methods are provided.
8  */

9
10 public class TelnetOutputStream extends FilterOutputStream
11 {
12   public TelnetOutputStream (OutputStream out)
13   {
14     super(out);
15   }
16
17   public void write (int value) throws IOException
18   {
19     if (value == Telnet.IAC)
20       out.write(value);
21     out.write(value);
22   }
23
24   public void write (byte[] b) throws IOException
25   {
26     write(b, 0, b.length);
27   }
28
29   public void write (byte[] b, int off, int len) throws IOException
30   {
31     int i;
32     int limit = off + len;
33     for (i = off; i < limit; i++)
34     {
35       if (b[i] == (byte) Telnet.IAC)
36     {
37       // Write from b[off] upto and including b[i].
38
out.write(b, off, i+1-off);
39       // Next time, start writing at b[i].
40
// This causes b[i] to be written twice, as needed by the protocol.
41
off = i;
42     }
43     }
44     // Write whatever is left.
45
out.write(b, off, limit-off);
46   }
47
48   public void writeCommand (int code) throws IOException
49   {
50     out.write(Telnet.IAC);
51     out.write(code);
52   }
53
54   public final void writeCommand (int code, int option) throws IOException
55   {
56     out.write(Telnet.IAC);
57     out.write(code);
58     out.write(option);
59   }
60
61   public final void writeDo (int option) throws IOException
62   {
63     writeCommand(Telnet.DO, option);
64   }
65
66   public final void writeDont (int option) throws IOException
67   {
68     writeCommand(Telnet.DONT, option);
69   }
70
71   public final void writeWill (int option) throws IOException
72   {
73     writeCommand(Telnet.WILL, option);
74   }
75
76   public final void writeWont (int option) throws IOException
77   {
78     writeCommand(Telnet.WONT, option);
79   }
80
81   public final void writeSubCommand (int option, byte[] command)
82      throws IOException
83   {
84     writeCommand(Telnet.SB, option);
85     write(command);
86     writeCommand(Telnet.SE);
87   }
88 }
89
Popular Tags