01: package kawa; // For now
02:
03: import java.io.*;
04:
05: /**
06: * An input stream tha handles the telnet protocol.
07: * Basically, the byte value IAC is doubled.
08: * In addition, various utility methods are provided.
09: */
10:
11: public class TelnetOutputStream extends FilterOutputStream {
12: public TelnetOutputStream(OutputStream out) {
13: super (out);
14: }
15:
16: public void write(int value) throws IOException {
17: if (value == Telnet.IAC)
18: out.write(value);
19: out.write(value);
20: }
21:
22: public void write(byte[] b) throws IOException {
23: write(b, 0, b.length);
24: }
25:
26: public void write(byte[] b, int off, int len) throws IOException {
27: int i;
28: int limit = off + len;
29: for (i = off; i < limit; i++) {
30: if (b[i] == (byte) Telnet.IAC) {
31: // Write from b[off] upto and including b[i].
32: out.write(b, off, i + 1 - off);
33: // Next time, start writing at b[i].
34: // This causes b[i] to be written twice, as needed by the protocol.
35: off = i;
36: }
37: }
38: // Write whatever is left.
39: out.write(b, off, limit - off);
40: }
41:
42: public void writeCommand(int code) throws IOException {
43: out.write(Telnet.IAC);
44: out.write(code);
45: }
46:
47: public final void writeCommand(int code, int option)
48: throws IOException {
49: out.write(Telnet.IAC);
50: out.write(code);
51: out.write(option);
52: }
53:
54: public final void writeDo(int option) throws IOException {
55: writeCommand(Telnet.DO, option);
56: }
57:
58: public final void writeDont(int option) throws IOException {
59: writeCommand(Telnet.DONT, option);
60: }
61:
62: public final void writeWill(int option) throws IOException {
63: writeCommand(Telnet.WILL, option);
64: }
65:
66: public final void writeWont(int option) throws IOException {
67: writeCommand(Telnet.WONT, option);
68: }
69:
70: public final void writeSubCommand(int option, byte[] command)
71: throws IOException {
72: writeCommand(Telnet.SB, option);
73: write(command);
74: writeCommand(Telnet.SE);
75: }
76: }
|