WindowSizeOptionHandler.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.apache.commons.net.telnet;

  18. /**
  19.  * Implements the telnet window size option RFC 1073.
  20.  *
  21.  * @since 2.0
  22.  */
  23. public class WindowSizeOptionHandler extends TelnetOptionHandler {
  24.     /**
  25.      * Window size option
  26.      */
  27.     protected static final int WINDOW_SIZE = 31;

  28.     /**
  29.      * Horizontal Size
  30.      */
  31.     private int width = 80;

  32.     /**
  33.      * Vertical Size
  34.      */
  35.     private int height = 24;

  36.     /**
  37.      * Constructor for the WindowSizeOptionHandler. Initial and accept behavior flags are set to false
  38.      *
  39.      * @param nWidth  - Window width.
  40.      * @param nHeight - Window Height
  41.      */
  42.     public WindowSizeOptionHandler(final int nWidth, final int nHeight) {
  43.         super(TelnetOption.WINDOW_SIZE, false, false, false, false);

  44.         width = nWidth;
  45.         height = nHeight;
  46.     }

  47.     /**
  48.      * Constructor for the WindowSizeOptionHandler. Allows defining desired initial setting for local/remote activation of this option and behavior in case a
  49.      * local/remote activation request for this option is received.
  50.      *
  51.      * @param nWidth       - Window width.
  52.      * @param nHeight      - Window Height
  53.      * @param initlocal    - if set to true, a {@code WILL} is sent upon connection.
  54.      * @param initremote   - if set to true, a {@code DO} is sent upon connection.
  55.      * @param acceptlocal  - if set to true, any {@code DO} request is accepted.
  56.      * @param acceptremote - if set to true, any {@code WILL} request is accepted.
  57.      */
  58.     public WindowSizeOptionHandler(final int nWidth, final int nHeight, final boolean initlocal, final boolean initremote, final boolean acceptlocal,
  59.             final boolean acceptremote) {
  60.         super(TelnetOption.WINDOW_SIZE, initlocal, initremote, acceptlocal, acceptremote);

  61.         width = nWidth;
  62.         height = nHeight;
  63.     }

  64.     /**
  65.      * Implements the abstract method of TelnetOptionHandler. This will send the client Height and Width to the server.
  66.      *
  67.      * @return array to send to remote system
  68.      */
  69.     @Override
  70.     public int[] startSubnegotiationLocal() {
  71.         final int nCompoundWindowSize = width * 0x10000 + height;
  72.         int nResponseSize = 5;
  73.         int nIndex;
  74.         int nShift;
  75.         int nTurnedOnBits;

  76.         if (width % 0x100 == 0xFF) {
  77.             nResponseSize += 1;
  78.         }

  79.         if (width / 0x100 == 0xFF) {
  80.             nResponseSize += 1;
  81.         }

  82.         if (height % 0x100 == 0xFF) {
  83.             nResponseSize += 1;
  84.         }

  85.         if (height / 0x100 == 0xFF) {
  86.             nResponseSize += 1;
  87.         }

  88.         //
  89.         // allocate response array
  90.         //
  91.         final int[] response = new int[nResponseSize];

  92.         //
  93.         // Build response array.
  94.         // ---------------------
  95.         // 1. put option name.
  96.         // 2. loop through Window size and fill the values,
  97.         // 3. duplicate 'ff' if needed.
  98.         //

  99.         response[0] = WINDOW_SIZE; // 1 //

  100.         for ( // 2 //
  101.                 nIndex = 1, nShift = 24; nIndex < nResponseSize; nIndex++, nShift -= 8) {
  102.             nTurnedOnBits = 0xFF;
  103.             nTurnedOnBits <<= nShift;
  104.             response[nIndex] = (nCompoundWindowSize & nTurnedOnBits) >>> nShift;

  105.             if (response[nIndex] == 0xff) { // 3 //
  106.                 nIndex++;
  107.                 response[nIndex] = 0xff;
  108.             }
  109.         }

  110.         return response;
  111.     }

  112. }