TelnetOutputStream.java 1.9 KB

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