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