View Javadoc
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  
18  package org.apache.commons.net.telnet;
19  
20  /***
21   * Implements the telnet window size option RFC 1073.
22   * @version $Id: WindowSizeOptionHandler.java 1697293 2015-08-24 01:01:00Z sebb $
23   * @since 2.0
24   ***/
25  public class WindowSizeOptionHandler extends TelnetOptionHandler
26  {
27      /***
28       * Horizontal Size
29       ***/
30      private int m_nWidth = 80;
31  
32      /***
33       * Vertical Size
34       ***/
35      private int m_nHeight = 24;
36  
37      /***
38       * Window size option
39       ***/
40      protected static final int WINDOW_SIZE = 31;
41  
42      /***
43       * Constructor for the WindowSizeOptionHandler. Allows defining desired
44       * initial setting for local/remote activation of this option and
45       * behaviour in case a local/remote activation request for this
46       * option is received.
47       * <p>
48       * @param nWidth - Window width.
49       * @param nHeight - Window Height
50       * @param initlocal - if set to true, a WILL is sent upon connection.
51       * @param initremote - if set to true, a DO is sent upon connection.
52       * @param acceptlocal - if set to true, any DO request is accepted.
53       * @param acceptremote - if set to true, any WILL request is accepted.
54       ***/
55      public WindowSizeOptionHandler(
56          int nWidth,
57          int nHeight,
58          boolean initlocal,
59          boolean initremote,
60          boolean acceptlocal,
61          boolean acceptremote
62      ) {
63          super (
64              TelnetOption.WINDOW_SIZE,
65              initlocal,
66              initremote,
67              acceptlocal,
68              acceptremote
69          );
70  
71          m_nWidth = nWidth;
72          m_nHeight = nHeight;
73      }
74  
75      /***
76       * Constructor for the WindowSizeOptionHandler. Initial and accept
77       * behaviour flags are set to false
78       * <p>
79       * @param nWidth - Window width.
80       * @param nHeight - Window Height
81       ***/
82      public WindowSizeOptionHandler(
83          int nWidth,
84          int nHeight
85      ) {
86          super (
87              TelnetOption.WINDOW_SIZE,
88              false,
89              false,
90              false,
91              false
92          );
93  
94          m_nWidth = nWidth;
95          m_nHeight = nHeight;
96      }
97  
98      /***
99       * Implements the abstract method of TelnetOptionHandler.
100      * This will send the client Height and Width to the server.
101      * <p>
102      * @return array to send to remote system
103      ***/
104     @Override
105     public int[] startSubnegotiationLocal()
106     {
107         int nCompoundWindowSize = m_nWidth * 0x10000 + m_nHeight;
108         int nResponseSize = 5;
109         int nIndex;
110         int nShift;
111         int nTurnedOnBits;
112 
113         if ((m_nWidth % 0x100) == 0xFF) {
114             nResponseSize += 1;
115         }
116 
117         if ((m_nWidth / 0x100) == 0xFF) {
118             nResponseSize += 1;
119         }
120 
121         if ((m_nHeight % 0x100) == 0xFF) {
122             nResponseSize += 1;
123         }
124 
125         if ((m_nHeight / 0x100) == 0xFF) {
126             nResponseSize += 1;
127         }
128 
129         //
130         // allocate response array
131         //
132         int response[] = new int[nResponseSize];
133 
134         //
135         // Build response array.
136         // ---------------------
137         // 1. put option name.
138         // 2. loop through Window size and fill the values,
139         // 3.    duplicate 'ff' if needed.
140         //
141 
142         response[0] = WINDOW_SIZE;                          // 1 //
143 
144         for (                                               // 2 //
145             nIndex=1, nShift = 24;
146             nIndex < nResponseSize;
147             nIndex++, nShift -=8
148         ) {
149             nTurnedOnBits = 0xFF;
150             nTurnedOnBits <<= nShift;
151             response[nIndex] = (nCompoundWindowSize & nTurnedOnBits) >>> nShift;
152 
153             if (response[nIndex] == 0xff) {                 // 3 //
154                 nIndex++;
155                 response[nIndex] = 0xff;
156             }
157         }
158 
159         return response;
160     }
161 
162 }