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 final class TelnetOutputStream extends OutputStream
35 {
36 private final TelnetClient __client;
37
38 private final boolean __convertCRtoCRLF = true;
39 private boolean __lastWasCR = false;
40
41 TelnetOutputStream(TelnetClient client)
42 {
43 __client = client;
44 }
45
46
47
48
49
50
51
52
53
54 @Override
55 public void write(int ch) throws IOException
56 {
57
58 synchronized (__client)
59 {
60 ch &= 0xff;
61
62 if (__client._requestedWont(TelnetOption.BINARY))
63 {
64 if (__lastWasCR)
65 {
66 if (__convertCRtoCRLF)
67 {
68 __client._sendByte('\n');
69 if (ch == '\n')
70 {
71 __lastWasCR = false;
72 return ;
73 }
74 }
75 else if (ch != '\n')
76 {
77 __client._sendByte('\0');
78 }
79 }
80
81 __lastWasCR = false;
82
83 switch (ch)
84 {
85 case '\r':
86 __client._sendByte('\r');
87 __lastWasCR = true;
88 break;
89 case TelnetCommand.IAC:
90 __client._sendByte(TelnetCommand.IAC);
91 __client._sendByte(TelnetCommand.IAC);
92 break;
93 default:
94 __client._sendByte(ch);
95 break;
96 }
97 }
98 else if (ch == TelnetCommand.IAC)
99 {
100 __client._sendByte(ch);
101 __client._sendByte(TelnetCommand.IAC);
102 } else {
103 __client._sendByte(ch);
104 }
105 }
106 }
107
108
109
110
111
112
113
114
115
116 @Override
117 public void write(byte buffer[]) throws IOException
118 {
119 write(buffer, 0, buffer.length);
120 }
121
122
123
124
125
126
127
128
129
130
131
132
133 @Override
134 public void write(byte buffer[], int offset, int length) throws IOException
135 {
136 synchronized (__client)
137 {
138 while (length-- > 0) {
139 write(buffer[offset++]);
140 }
141 }
142 }
143
144
145 @Override
146 public void flush() throws IOException
147 {
148 __client._flushOutputStream();
149 }
150
151
152 @Override
153 public void close() throws IOException
154 {
155 __client._closeOutputStream();
156 }
157 }