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 * 058 * @param nWidth - Window width. 059 * @param nHeight - Window Height 060 * @param initlocal - if set to true, a {@code WILL} is sent upon connection. 061 * @param initremote - if set to true, a {@code DO} is sent upon connection. 062 * @param acceptlocal - if set to true, any {@code DO} request is accepted. 063 * @param acceptremote - if set to true, any {@code WILL} request is accepted. 064 */ 065 public WindowSizeOptionHandler(final int nWidth, final int nHeight, final boolean initlocal, final boolean initremote, final boolean acceptlocal, 066 final boolean acceptremote) { 067 super(TelnetOption.WINDOW_SIZE, initlocal, initremote, acceptlocal, acceptremote); 068 069 width = nWidth; 070 height = nHeight; 071 } 072 073 /** 074 * Implements the abstract method of TelnetOptionHandler. This will send the client Height and Width to the server. 075 * 076 * @return array to send to remote system 077 */ 078 @Override 079 public int[] startSubnegotiationLocal() { 080 final int nCompoundWindowSize = width * 0x10000 + height; 081 int nResponseSize = 5; 082 int nIndex; 083 int nShift; 084 int nTurnedOnBits; 085 086 if (width % 0x100 == 0xFF) { 087 nResponseSize += 1; 088 } 089 090 if (width / 0x100 == 0xFF) { 091 nResponseSize += 1; 092 } 093 094 if (height % 0x100 == 0xFF) { 095 nResponseSize += 1; 096 } 097 098 if (height / 0x100 == 0xFF) { 099 nResponseSize += 1; 100 } 101 102 // 103 // allocate response array 104 // 105 final int[] response = new int[nResponseSize]; 106 107 // 108 // Build response array. 109 // --------------------- 110 // 1. put option name. 111 // 2. loop through Window size and fill the values, 112 // 3. duplicate 'ff' if needed. 113 // 114 115 response[0] = WINDOW_SIZE; // 1 // 116 117 for ( // 2 // 118 nIndex = 1, nShift = 24; nIndex < nResponseSize; nIndex++, nShift -= 8) { 119 nTurnedOnBits = 0xFF; 120 nTurnedOnBits <<= nShift; 121 response[nIndex] = (nCompoundWindowSize & nTurnedOnBits) >>> nShift; 122 123 if (response[nIndex] == 0xff) { // 3 // 124 nIndex++; 125 response[nIndex] = 0xff; 126 } 127 } 128 129 return response; 130 } 131 132}