001: /*
002: * Copyright 2001-2005 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.net.telnet;
017:
018: import java.io.IOException;
019: import java.io.OutputStream;
020:
021: /***
022: *
023: * <p>
024: *
025: * <p>
026: * <p>
027: * @author Daniel F. Savarese
028: ***/
029:
030: final class TelnetOutputStream extends OutputStream {
031: private TelnetClient __client;
032: private boolean __convertCRtoCRLF = true;
033: private boolean __lastWasCR = false;
034:
035: TelnetOutputStream(TelnetClient client) {
036: __client = client;
037: }
038:
039: /***
040: * Writes a byte to the stream.
041: * <p>
042: * @param ch The byte to write.
043: * @exception IOException If an error occurs while writing to the underlying
044: * stream.
045: ***/
046: public void write(int ch) throws IOException {
047:
048: synchronized (__client) {
049: ch &= 0xff;
050:
051: if (__client._requestedWont(TelnetOption.BINARY)) {
052: if (__lastWasCR) {
053: if (__convertCRtoCRLF) {
054: __client._sendByte('\n');
055: if (ch == '\n') {
056: __lastWasCR = false;
057: return;
058: }
059: } else if (ch != '\n')
060: __client._sendByte('\0');
061: }
062:
063: __lastWasCR = false;
064:
065: switch (ch) {
066: case '\r':
067: __client._sendByte('\r');
068: __lastWasCR = true;
069: break;
070: case TelnetCommand.IAC:
071: __client._sendByte(TelnetCommand.IAC);
072: __client._sendByte(TelnetCommand.IAC);
073: break;
074: default:
075: __client._sendByte(ch);
076: break;
077: }
078: } else if (ch == TelnetCommand.IAC) {
079: __client._sendByte(ch);
080: __client._sendByte(TelnetCommand.IAC);
081: } else
082: __client._sendByte(ch);
083: }
084: }
085:
086: /***
087: * Writes a byte array to the stream.
088: * <p>
089: * @param buffer The byte array to write.
090: * @exception IOException If an error occurs while writing to the underlying
091: * stream.
092: ***/
093: public void write(byte buffer[]) throws IOException {
094: write(buffer, 0, buffer.length);
095: }
096:
097: /***
098: * Writes a number of bytes from a byte array to the stream starting from
099: * a given offset.
100: * <p>
101: * @param buffer The byte array to write.
102: * @param offset The offset into the array at which to start copying data.
103: * @param length The number of bytes to write.
104: * @exception IOException If an error occurs while writing to the underlying
105: * stream.
106: ***/
107: public void write(byte buffer[], int offset, int length)
108: throws IOException {
109: synchronized (__client) {
110: while (length-- > 0)
111: write(buffer[offset++]);
112: }
113: }
114:
115: /*** Flushes the stream. ***/
116: public void flush() throws IOException {
117: __client._flushOutputStream();
118: }
119:
120: /*** Closes the stream. ***/
121: public void close() throws IOException {
122: __client._closeOutputStream();
123: }
124: }
|