    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017
    018package org.apache.commons.net.telnet;
    019
    020/**
    021 * The TelnetCommand class cannot be instantiated and only serves as a storehouse for telnet command constants.
    022 *
    023 * @see org.apache.commons.net.telnet.Telnet
    024 * @see org.apache.commons.net.telnet.TelnetClient
    025 */
    026
    027public final class TelnetCommand {
    028    /** The maximum value a command code can have. This value is 255. */
    029    public static final int MAX_COMMAND_VALUE = 255;
    030
    031    /** Interpret As Command code. Value is 255 according to RFC 854. */
    032    public static final int IAC = 255;
    033
    034    /** Don't use option code. Value is 254 according to RFC 854. */
    035    public static final int DONT = 254;
    036
    037    /** Request to use option code. Value is 253 according to RFC 854. */
    038    public static final int DO = 253;
    039
    040    /** Refuse to use option code. Value is 252 according to RFC 854. */
    041    public static final int WONT = 252;
    042
    043    /** Agree to use option code. Value is 251 according to RFC 854. */
    044    public static final int WILL = 251;
    045
    046    /** Start subnegotiation code. Value is 250 according to RFC 854. */
    047    public static final int SB = 250;
    048
    049    /** Go Ahead code. Value is 249 according to RFC 854. */
    050    public static final int GA = 249;
    051
    052    /** Erase Line code. Value is 248 according to RFC 854. */
    053    public static final int EL = 248;
    054
    055    /** Erase Character code. Value is 247 according to RFC 854. */
    056    public static final int EC = 247;
    057
    058    /** Are You There code. Value is 246 according to RFC 854. */
    059    public static final int AYT = 246;
    060
    061    /** Abort Output code. Value is 245 according to RFC 854. */
    062    public static final int AO = 245;
    063
    064    /** Interrupt Process code. Value is 244 according to RFC 854. */
    065    public static final int IP = 244;
    066
    067    /** Break code. Value is 243 according to RFC 854. */
    068    public static final int BREAK = 243;
    069
    070    /** Data mark code. Value is 242 according to RFC 854. */
    071    public static final int DM = 242;
    072
    073    /** No Operation code. Value is 241 according to RFC 854. */
    074    public static final int NOP = 241;
    075
    076    /** End subnegotiation code. Value is 240 according to RFC 854. */
    077    public static final int SE = 240;
    078
    079    /** End of record code. Value is 239. */
    080    public static final int EOR = 239;
    081
    082    /** Abort code. Value is 238. */
    083    public static final int ABORT = 238;
    084
    085    /** Suspend process code. Value is 237. */
    086    public static final int SUSP = 237;
    087
    088    /** End of file code. Value is 236. */
    089    public static final int EOF = 236;
    090
    091    /** Synchronize code. Value is 242. */
    092    public static final int SYNCH = 242;
    093
    094    /** String representations of commands. */
    095    private static final String[] commandString = { "IAC", "DONT", "DO", "WONT", "WILL", "SB", "GA", "EL", "EC", "AYT", "AO", "IP", "BRK", "DMARK", "NOP", "SE",
    096            "EOR", "ABORT", "SUSP", "EOF" };
    097
    098    private static final int FIRST_COMMAND = IAC;
    099    private static final int LAST_COMMAND = EOF;
    100
    101    /**
    102     * Returns the string representation of the telnet protocol command corresponding to the given command code.
    103     *
    104     * @param code The command code of the telnet protocol command.
    105     * @return The string representation of the telnet protocol command.
    106     */
    107    public static String getCommand(final int code) {
    108        return commandString[FIRST_COMMAND - code];
    109    }
    110
    111    /**
    112     * Determines if a given command code is valid. Returns true if valid, false if not.
    113     *
    114     * @param code The command code to test.
    115     * @return True if the command code is valid, false if not.
    116     **/
    117    public static boolean isValidCommand(final int code) {
    118        return code <= FIRST_COMMAND && code >= LAST_COMMAND;
    119    }
    120
    121    /** Cannot be instantiated. */
    122    private TelnetCommand() {
    123    }
    124}