1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.net.telnet;
19
20 import java.io.IOException;
21 import java.io.OutputStream;
22
23
24
25
26
27
28
29
30
31
32
33
34
35 final class TelnetOutputStream extends OutputStream
36 {
37 private final TelnetClient client;
38
39 private final boolean convertCRtoCRLF = true;
40 private boolean lastWasCR;
41
42 TelnetOutputStream(final TelnetClient client)
43 {
44 this.client = client;
45 }
46
47
48
49
50
51
52
53
54
55 @Override
56 public void write(int ch) throws IOException
57 {
58
59 synchronized (client)
60 {
61 ch &= 0xff;
62
63 if (client.requestedWont(TelnetOption.BINARY))
64 {
65 if (lastWasCR)
66 {
67 if (convertCRtoCRLF)
68 {
69 client.sendByte('\n');
70 if (ch == '\n')
71 {
72 lastWasCR = false;
73 return ;
74 }
75 }
76 else if (ch != '\n')
77 {
78 client.sendByte('\0');
79 }
80 }
81
82 switch (ch)
83 {
84 case '\r':
85 client.sendByte('\r');
86 lastWasCR = true;
87 break;
88 case '\n':
89 if (!lastWasCR) {
90 client.sendByte('\r');
91 }
92 client.sendByte(ch);
93 lastWasCR = false;
94 break;
95 case TelnetCommand.IAC:
96 client.sendByte(TelnetCommand.IAC);
97 client.sendByte(TelnetCommand.IAC);
98 lastWasCR = false;
99 break;
100 default:
101 client.sendByte(ch);
102 lastWasCR = false;
103 break;
104 }
105 }
106 else if (ch == TelnetCommand.IAC)
107 {
108 client.sendByte(ch);
109 client.sendByte(TelnetCommand.IAC);
110 } else {
111 client.sendByte(ch);
112 }
113 }
114 }
115
116
117
118
119
120
121
122
123
124 @Override
125 public void write(final byte buffer[]) throws IOException
126 {
127 write(buffer, 0, buffer.length);
128 }
129
130
131
132
133
134
135
136
137
138
139
140
141 @Override
142 public void write(final byte buffer[], int offset, int length) throws IOException
143 {
144 synchronized (client)
145 {
146 while (length-- > 0) {
147 write(buffer[offset++]);
148 }
149 }
150 }
151
152
153 @Override
154 public void flush() throws IOException
155 {
156 client.flushOutputStream();
157 }
158
159
160 @Override
161 public void close() throws IOException
162 {
163 client.closeOutputStream();
164 }
165 }