1. /*
  2. * Copyright 2001-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.apache.commons.net.telnet;
  17. import java.io.IOException;
  18. import java.io.OutputStream;
  19. /***
  20. *
  21. * <p>
  22. *
  23. * <p>
  24. * <p>
  25. * @author Daniel F. Savarese
  26. ***/
  27. final class TelnetOutputStream extends OutputStream
  28. {
  29. private TelnetClient __client;
  30. private boolean __convertCRtoCRLF = true;
  31. private boolean __lastWasCR = false;
  32. TelnetOutputStream(TelnetClient client)
  33. {
  34. __client = client;
  35. }
  36. /***
  37. * Writes a byte to the stream.
  38. * <p>
  39. * @param ch The byte to write.
  40. * @exception IOException If an error occurs while writing to the underlying
  41. * stream.
  42. ***/
  43. public void write(int ch) throws IOException
  44. {
  45. synchronized (__client)
  46. {
  47. ch &= 0xff;
  48. if (__client._requestedWont(TelnetOption.BINARY))
  49. {
  50. if (__lastWasCR)
  51. {
  52. if (__convertCRtoCRLF)
  53. {
  54. __client._sendByte('\n');
  55. if (ch == '\n')
  56. {
  57. __lastWasCR = false;
  58. return ;
  59. }
  60. }
  61. else if (ch != '\n')
  62. __client._sendByte('\0');
  63. }
  64. __lastWasCR = false;
  65. switch (ch)
  66. {
  67. case '\r':
  68. __client._sendByte('\r');
  69. __lastWasCR = true;
  70. break;
  71. case TelnetCommand.IAC:
  72. __client._sendByte(TelnetCommand.IAC);
  73. __client._sendByte(TelnetCommand.IAC);
  74. break;
  75. default:
  76. __client._sendByte(ch);
  77. break;
  78. }
  79. }
  80. else if (ch == TelnetCommand.IAC)
  81. {
  82. __client._sendByte(ch);
  83. __client._sendByte(TelnetCommand.IAC);
  84. }
  85. else
  86. __client._sendByte(ch);
  87. }
  88. }
  89. /***
  90. * Writes a byte array to the stream.
  91. * <p>
  92. * @param buffer The byte array to write.
  93. * @exception IOException If an error occurs while writing to the underlying
  94. * stream.
  95. ***/
  96. public void write(byte buffer[]) throws IOException
  97. {
  98. write(buffer, 0, buffer.length);
  99. }
  100. /***
  101. * Writes a number of bytes from a byte array to the stream starting from
  102. * a given offset.
  103. * <p>
  104. * @param buffer The byte array to write.
  105. * @param offset The offset into the array at which to start copying data.
  106. * @param length The number of bytes to write.
  107. * @exception IOException If an error occurs while writing to the underlying
  108. * stream.
  109. ***/
  110. public void write(byte buffer[], int offset, int length) throws IOException
  111. {
  112. synchronized (__client)
  113. {
  114. while (length-- > 0)
  115. write(buffer[offset++]);
  116. }
  117. }
  118. /*** Flushes the stream. ***/
  119. public void flush() throws IOException
  120. {
  121. __client._flushOutputStream();
  122. }
  123. /*** Closes the stream. ***/
  124. public void close() throws IOException
  125. {
  126. __client._closeOutputStream();
  127. }
  128. }