    View Javadoc
    1   package org.apache.commons.net.telnet;
    2
    3   /* ====================================================================
    4    * The Apache Software License, Version 1.1
    5    *
    6    * Copyright (c) 2001 The Apache Software Foundation.  All rights
    7    * reserved.
    8    *
    9    * Redistribution and use in source and binary forms, with or without
    10   * modification, are permitted provided that the following conditions
    11   * are met:
    12   *
    13   * 1. Redistributions of source code must retain the above copyright
    14   *    notice, this list of conditions and the following disclaimer.
    15   *
    16   * 2. Redistributions in binary form must reproduce the above copyright
    17   *    notice, this list of conditions and the following disclaimer in
    18   *    the documentation and/or other materials provided with the
    19   *    distribution.
    20   *
    21   * 3. The end-user documentation included with the redistribution,
    22   *    if any, must include the following acknowledgment:
    23   *       "This product includes software developed by the
    24   *        Apache Software Foundation (http://www.apache.org/)."
    25   *    Alternately, this acknowledgment may appear in the software itself,
    26   *    if and wherever such third-party acknowledgments normally appear.
    27   *
    28   * 4. The names "Apache" and "Apache Software Foundation" and
    29   *    "Apache Commons" must not be used to endorse or promote products
    30   *    derived from this software without prior written permission. For
    31   *    written permission, please contact apache@apache.org.
    32   *
    33   * 5. Products derived from this software may not be called "Apache",
    34   *    nor may "Apache" appear in their name, without
    35   *    prior written permission of the Apache Software Foundation.
    36   *
    37   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
    38   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
    39   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    40   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
    41   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    42   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    43   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
    44   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
    45   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
    46   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
    47   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    48   * SUCH DAMAGE.
    49   * ====================================================================
    50   *
    51   * This software consists of voluntary contributions made by many
    52   * individuals on behalf of the Apache Software Foundation.  For more
    53   * information on the Apache Software Foundation, please see
    54   * <http://www.apache.org/>;.
    55   */
    56
    57  import java.io.IOException;
    58  import java.io.OutputStream;
    59
    60  /****
    61   *
    62   * <p>
    63   *
    64   * <p>
    65   * <p>
    66   * @author Daniel F. Savarese
    67   ***/
    68
    69
    70  final class TelnetOutputStream extends OutputStream
    71  {
    72      private TelnetClient __client;
    73      private boolean __convertCRtoCRLF = true;
    74      private boolean __lastWasCR = false;
    75
    76      TelnetOutputStream(TelnetClient client)
    77      {
    78          __client = client;
    79      }
    80
    81
    82      /****
    83       * Writes a byte to the stream.
    84       * <p>
    85       * @param ch The byte to write.
    86       * @exception IOException If an error occurs while writing to the underlying
    87       *            stream.
    88       ***/
    89      public void write(int ch) throws IOException
    90      {
    91
    92          synchronized (__client)
    93          {
    94              ch &= 0xff;
    95
    96              if (__client._requestedWont(TelnetOption.BINARY))
    97              {
    98                  if (__lastWasCR)
    99                  {
    100                     if (__convertCRtoCRLF)
    101                     {
    102                         __client._sendByte('\n');
    103                         if (ch == '\n')
    104                         {
    105                             __lastWasCR = false;
    106                             return ;
    107                         }
    108                     }
    109                     else if (ch != '\n')
    110                         __client._sendByte('\0');
    111                 }
    112
    113                 __lastWasCR = false;
    114
    115                 switch (ch)
    116                 {
    117                 case '\r':
    118                     __client._sendByte('\r');
    119                     __lastWasCR = true;
    120                     break;
    121                 case TelnetCommand.IAC:
    122                     __client._sendByte(TelnetCommand.IAC);
    123                     __client._sendByte(TelnetCommand.IAC);
    124                     break;
    125                 default:
    126                     __client._sendByte(ch);
    127                     break;
    128                 }
    129             }
    130             else if (ch == TelnetCommand.IAC)
    131             {
    132                 __client._sendByte(ch);
    133                 __client._sendByte(TelnetCommand.IAC);
    134             }
    135             else
    136                 __client._sendByte(ch);
    137         }
    138     }
    139
    140
    141     /****
    142      * Writes a byte array to the stream.
    143      * <p>
    144      * @param buffer  The byte array to write.
    145      * @exception IOException If an error occurs while writing to the underlying
    146      *            stream.
    147      ***/
    148     public void write(byte buffer[]) throws IOException
    149     {
    150         write(buffer, 0, buffer.length);
    151     }
    152
    153
    154     /****
    155      * Writes a number of bytes from a byte array to the stream starting from
    156      * a given offset.
    157      * <p>
    158      * @param buffer  The byte array to write.
    159      * @param offset  The offset into the array at which to start copying data.
    160      * @param length  The number of bytes to write.
    161      * @exception IOException If an error occurs while writing to the underlying
    162      *            stream.
    163      ***/
    164     public void write(byte buffer[], int offset, int length) throws IOException
    165     {
    166         synchronized (__client)
    167         {
    168             while (length-- > 0)
    169                 write(buffer[offset++]);
    170         }
    171     }
    172
    173     /**** Flushes the stream. ***/
    174     public void flush() throws IOException
    175     {
    176         __client._flushOutputStream();
    177     }
    178
    179     /**** Closes the stream. ***/
    180     public void close() throws IOException
    181     {
    182         __client._closeOutputStream();
    183     }
    184 }

This page was automatically generated by Maven