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.io;
  17. import java.io.FilterOutputStream;
  18. import java.io.IOException;
  19. import java.io.OutputStream;
  20. /***
  21. * This class wraps an output stream, replacing all singly occurring
  22. * <LF> (linefeed) characters with <CR><LF> (carriage return
  23. * followed by linefeed), which is the NETASCII standard for representing
  24. * a newline.
  25. * You would use this class to implement ASCII file transfers requiring
  26. * conversion to NETASCII.
  27. * <p>
  28. * <p>
  29. * @author Daniel F. Savarese
  30. ***/
  31. public final class ToNetASCIIOutputStream extends FilterOutputStream
  32. {
  33. private boolean __lastWasCR;
  34. /***
  35. * Creates a ToNetASCIIOutputStream instance that wraps an existing
  36. * OutputStream.
  37. * <p>
  38. * @param output The OutputStream to wrap.
  39. ***/
  40. public ToNetASCIIOutputStream(OutputStream output)
  41. {
  42. super(output);
  43. __lastWasCR = false;
  44. }
  45. /***
  46. * Writes a byte to the stream. Note that a call to this method
  47. * may result in multiple writes to the underlying input stream in order
  48. * to convert naked newlines to NETASCII line separators.
  49. * This is transparent to the programmer and is only mentioned for
  50. * completeness.
  51. * <p>
  52. * @param ch The byte to write.
  53. * @exception IOException If an error occurs while writing to the underlying
  54. * stream.
  55. ***/
  56. public synchronized void write(int ch)
  57. throws IOException
  58. {
  59. switch (ch)
  60. {
  61. case '\r':
  62. __lastWasCR = true;
  63. out.write('\r');
  64. return ;
  65. case '\n':
  66. if (!__lastWasCR)
  67. out.write('\r');
  68. // Fall through
  69. default:
  70. __lastWasCR = false;
  71. out.write(ch);
  72. return ;
  73. }
  74. }
  75. /***
  76. * Writes a byte array to the stream.
  77. * <p>
  78. * @param buffer The byte array to write.
  79. * @exception IOException If an error occurs while writing to the underlying
  80. * stream.
  81. ***/
  82. public synchronized void write(byte buffer[])
  83. throws IOException
  84. {
  85. write(buffer, 0, buffer.length);
  86. }
  87. /***
  88. * Writes a number of bytes from a byte array to the stream starting from
  89. * a given offset.
  90. * <p>
  91. * @param buffer The byte array to write.
  92. * @param offset The offset into the array at which to start copying data.
  93. * @param length The number of bytes to write.
  94. * @exception IOException If an error occurs while writing to the underlying
  95. * stream.
  96. ***/
  97. public synchronized void write(byte buffer[], int offset, int length)
  98. throws IOException
  99. {
  100. while (length-- > 0)
  101. write(buffer[offset++]);
  102. }
  103. }