2 * Jexer - Java Text User Interface
4 * License: LGPLv3 or later
6 * This module is licensed under the GNU Lesser General Public License
7 * Version 3. Please see the file "COPYING" in this directory for more
8 * information about the GNU Lesser General Public License Version 3.
10 * Copyright (C) 2015 Kevin Lamonte
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public License
14 * as published by the Free Software Foundation; either version 3 of
15 * the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this program; if not, see
24 * http://www.gnu.org/licenses/, or write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
28 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
33 import java
.io
.OutputStream
;
34 import java
.io
.IOException
;
36 import static jexer
.net
.TelnetSocket
.*;
39 * TelnetOutputStream works with TelnetSocket to perform the telnet protocol.
41 public final class TelnetOutputStream
extends OutputStream
{
44 * The root TelnetSocket that has my telnet protocol state.
46 private TelnetSocket master
;
49 * The raw socket's OutputStream.
51 private OutputStream output
;
54 * Package private constructor.
56 * @param master the master TelnetSocket
57 * @param output the underlying socket's OutputStream
59 TelnetOutputStream(final TelnetSocket master
, final OutputStream output
) {
64 // OutputStream interface -------------------------------------------------
67 * Closes this output stream and releases any system resources associated
70 * @throws IOException if an I/O error occurs
73 public void close() throws IOException
{
81 * Flushes this output stream and forces any buffered output bytes to be
84 * @throws IOException if an I/O error occurs
87 public void flush() throws IOException
{
88 if ((master
.binaryMode
== false) && (writeCR
== true)) {
89 // The last byte sent to this.write() was a CR, which was never
90 // actually sent. So send the CR in ascii mode, then flush.
91 // CR <anything> -> CR NULL
100 * Writes b.length bytes from the specified byte array to this output
104 * @throws IOException if an I/O error occurs
107 public void write(final byte[] b
) throws IOException
{
108 writeImpl(b
, 0, b
.length
);
112 * Writes len bytes from the specified byte array starting at offset off
113 * to this output stream.
116 * @param off the start offset in the data.
117 * @param len the number of bytes to write.
118 * @throws IOException if an I/O error occurs
121 public void write(final byte[] b
, final int off
,
122 final int len
) throws IOException
{
124 writeImpl(b
, off
, len
);
128 * Writes the specified byte to this output stream.
130 * @param b the byte to write.
131 * @throws IOException if an I/O error occurs
134 public void write(final int b
) throws IOException
{
135 byte [] bytes
= new byte[1];
137 writeImpl(bytes
, 0, 1);
141 * Writes b.length bytes from the specified byte array to this output
142 * stream. Note package private access.
145 * @throws IOException if an I/O error occurs
147 void rawWrite(final byte[] b
) throws IOException
{
148 output
.write(b
, 0, b
.length
);
151 // Telnet protocol --------------------------------------------------------
154 * When true, the last byte the caller passed to write() was a CR.
156 private boolean writeCR
= false;
159 * Writes len bytes from the specified byte array starting at offset off
160 * to this output stream.
163 * @param off the start offset in the data.
164 * @param len the number of bytes to write.
165 * @throws IOException if an I/O error occurs
167 private void writeImpl(final byte[] b
, final int off
,
168 final int len
) throws IOException
{
170 byte [] writeBuffer
= new byte[Math
.max(len
, 4)];
171 int writeBufferI
= 0;
173 for (int i
= 0; i
< len
; i
++) {
174 if (writeBufferI
>= writeBuffer
.length
- 4) {
175 // Flush what we have generated so far and reset the buffer,
176 // because the next byte could generate up to 4 output bytes
177 // (CR <something> <IAC> <IAC>).
178 output
.write(writeBuffer
, 0, writeBufferI
);
182 // Pull the next byte
183 byte ch
= b
[i
+ off
];
185 if (master
.binaryMode
== true) {
187 if (ch
== TELNET_IAC
) {
189 writeBuffer
[writeBufferI
++] = (byte)TELNET_IAC
;
190 writeBuffer
[writeBufferI
++] = (byte)TELNET_IAC
;
192 // Anything else -> just send
193 writeBuffer
[writeBufferI
++] = ch
;
198 // Non-binary mode: more complicated. We use writeCR to handle
199 // the case that the last byte of b was a CR.
201 // Bare carriage return -> CR NUL
203 if (writeCR
== true) {
204 // Flush the previous CR to the stream.
205 // CR <anything> -> CR NULL
206 writeBuffer
[writeBufferI
++] = (byte)C_CR
;
207 writeBuffer
[writeBufferI
++] = (byte)C_NUL
;
210 } else if (ch
== C_LF
) {
211 if (writeCR
== true) {
213 writeBuffer
[writeBufferI
++] = (byte)C_CR
;
214 writeBuffer
[writeBufferI
++] = (byte)C_LF
;
218 writeBuffer
[writeBufferI
++] = ch
;
220 } else if (ch
== TELNET_IAC
) {
221 if (writeCR
== true) {
222 // CR <anything> -> CR NULL
223 writeBuffer
[writeBufferI
++] = (byte)C_CR
;
224 writeBuffer
[writeBufferI
++] = (byte)C_NUL
;
228 writeBuffer
[writeBufferI
++] = (byte)TELNET_IAC
;
229 writeBuffer
[writeBufferI
++] = (byte)TELNET_IAC
;
231 if (writeCR
== true) {
232 // CR <anything> -> CR NULL
233 writeBuffer
[writeBufferI
++] = (byte)C_CR
;
234 writeBuffer
[writeBufferI
++] = (byte)C_NUL
;
237 // Normal character */
238 writeBuffer
[writeBufferI
++] = ch
;
242 } // while (i < userbuf.length)
244 if (writeBufferI
> 0) {
245 // Flush what we have generated so far and reset the buffer.
246 output
.write(writeBuffer
, 0, writeBufferI
);