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 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
47
48
49
50
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
107
108
109
110
111
112 @Override
113 public void write(byte buffer[]) throws IOException
114 {
115 write(buffer, 0, buffer.length);
116 }
117
118
119
120
121
122
123
124
125
126
127
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
140 @Override
141 public void flush() throws IOException
142 {
143 __client._flushOutputStream();
144 }
145
146
147 @Override
148 public void close() throws IOException
149 {
150 __client._closeOutputStream();
151 }
152 }