    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 * Implements the telnet window size option RFC 1073.
    022 *
    023 * @since 2.0
    024 */
    025public class WindowSizeOptionHandler extends TelnetOptionHandler {
    026    /**
    027     * Window size option
    028     */
    029    protected static final int WINDOW_SIZE = 31;
    030
    031    /**
    032     * Horizontal Size
    033     */
    034    private int width = 80;
    035
    036    /**
    037     * Vertical Size
    038     */
    039    private int height = 24;
    040
    041    /**
    042     * Constructor for the WindowSizeOptionHandler. Initial and accept behavior flags are set to false
    043     *
    044     * @param nWidth  - Window width.
    045     * @param nHeight - Window Height
    046     */
    047    public WindowSizeOptionHandler(final int nWidth, final int nHeight) {
    048        super(TelnetOption.WINDOW_SIZE, false, false, false, false);
    049
    050        width = nWidth;
    051        height = nHeight;
    052    }
    053
    054    /**
    055     * Constructor for the WindowSizeOptionHandler. Allows defining desired initial setting for local/remote activation of this option and behavior in case a
    056     * local/remote activation request for this option is received.
    057     * <p>
    058     *
    059     * @param nWidth       - Window width.
    060     * @param nHeight      - Window Height
    061     * @param initlocal    - if set to true, a {@code WILL} is sent upon connection.
    062     * @param initremote   - if set to true, a {@code DO} is sent upon connection.
    063     * @param acceptlocal  - if set to true, any {@code DO} request is accepted.
    064     * @param acceptremote - if set to true, any {@code WILL} request is accepted.
    065     */
    066    public WindowSizeOptionHandler(final int nWidth, final int nHeight, final boolean initlocal, final boolean initremote, final boolean acceptlocal,
    067            final boolean acceptremote) {
    068        super(TelnetOption.WINDOW_SIZE, initlocal, initremote, acceptlocal, acceptremote);
    069
    070        width = nWidth;
    071        height = nHeight;
    072    }
    073
    074    /**
    075     * Implements the abstract method of TelnetOptionHandler. This will send the client Height and Width to the server.
    076     *
    077     * @return array to send to remote system
    078     */
    079    @Override
    080    public int[] startSubnegotiationLocal() {
    081        final int nCompoundWindowSize = width * 0x10000 + height;
    082        int nResponseSize = 5;
    083        int nIndex;
    084        int nShift;
    085        int nTurnedOnBits;
    086
    087        if (width % 0x100 == 0xFF) {
    088            nResponseSize += 1;
    089        }
    090
    091        if (width / 0x100 == 0xFF) {
    092            nResponseSize += 1;
    093        }
    094
    095        if (height % 0x100 == 0xFF) {
    096            nResponseSize += 1;
    097        }
    098
    099        if (height / 0x100 == 0xFF) {
    100            nResponseSize += 1;
    101        }
    102
    103        //
    104        // allocate response array
    105        //
    106        final int[] response = new int[nResponseSize];
    107
    108        //
    109        // Build response array.
    110        // ---------------------
    111        // 1. put option name.
    112        // 2. loop through Window size and fill the values,
    113        // 3. duplicate 'ff' if needed.
    114        //
    115
    116        response[0] = WINDOW_SIZE; // 1 //
    117
    118        for ( // 2 //
    119                nIndex = 1, nShift = 24; nIndex < nResponseSize; nIndex++, nShift -= 8) {
    120            nTurnedOnBits = 0xFF;
    121            nTurnedOnBits <<= nShift;
    122            response[nIndex] = (nCompoundWindowSize & nTurnedOnBits) >>> nShift;
    123
    124            if (response[nIndex] == 0xff) { // 3 //
    125                nIndex++;
    126                response[nIndex] = 0xff;
    127            }
    128        }
    129
    130        return response;
    131    }
    132
    133}