Commit 5d7c4150 authored by shengnan hu's avatar shengnan hu
Browse files

init

parents
Pipeline #4715 failed with stage
in 30 seconds
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.net.telnet;
import java.io.IOException;
import java.io.OutputStream;
/**
* Wraps an output stream.
* <p>
* In binary mode, the only conversion is to double IAC.
* <p>
* In ASCII mode, if convertCRtoCRLF is true (currently always true), any CR is converted to CRLF.
* IACs are doubled.
* Also a bare LF is converted to CRLF and a bare CR is converted to CR\0
* <p>
***/
final class TelnetOutputStream extends OutputStream
{
private final TelnetClient __client;
// TODO there does not appear to be any way to change this value - should it be a ctor parameter?
private final boolean __convertCRtoCRLF = true;
private boolean __lastWasCR = false;
TelnetOutputStream(TelnetClient client)
{
__client = client;
}
/***
* Writes a byte to the stream.
* <p>
* @param ch The byte to write.
* @exception IOException If an error occurs while writing to the underlying
* stream.
***/
@Override
public void write(int ch) throws IOException
{
synchronized (__client)
{
ch &= 0xff;
if (__client._requestedWont(TelnetOption.BINARY)) // i.e. ASCII
{
if (__lastWasCR)
{
if (__convertCRtoCRLF)
{
__client._sendByte('\n');
if (ch == '\n') // i.e. was CRLF anyway
{
__lastWasCR = false;
return ;
}
} // __convertCRtoCRLF
else if (ch != '\n')
{
__client._sendByte('\0'); // RFC854 requires CR NUL for bare CR
}
}
switch (ch)
{
case '\r':
__client._sendByte('\r');
__lastWasCR = true;
break;
case '\n':
if (!__lastWasCR) { // convert LF to CRLF
__client._sendByte('\r');
}
__client._sendByte(ch);
__lastWasCR = false;
break;
case TelnetCommand.IAC:
__client._sendByte(TelnetCommand.IAC);
__client._sendByte(TelnetCommand.IAC);
__lastWasCR = false;
break;
default:
__client._sendByte(ch);
__lastWasCR = false;
break;
}
} // end ASCII
else if (ch == TelnetCommand.IAC)
{
__client._sendByte(ch);
__client._sendByte(TelnetCommand.IAC);
} else {
__client._sendByte(ch);
}
}
}
/***
* Writes a byte array to the stream.
* <p>
* @param buffer The byte array to write.
* @exception IOException If an error occurs while writing to the underlying
* stream.
***/
@Override
public void write(byte buffer[]) throws IOException
{
write(buffer, 0, buffer.length);
}
/***
* Writes a number of bytes from a byte array to the stream starting from
* a given offset.
* <p>
* @param buffer The byte array to write.
* @param offset The offset into the array at which to start copying data.
* @param length The number of bytes to write.
* @exception IOException If an error occurs while writing to the underlying
* stream.
***/
@Override
public void write(byte buffer[], int offset, int length) throws IOException
{
synchronized (__client)
{
while (length-- > 0) {
write(buffer[offset++]);
}
}
}
/*** Flushes the stream. ***/
@Override
public void flush() throws IOException
{
__client._flushOutputStream();
}
/*** Closes the stream. ***/
@Override
public void close() throws IOException
{
__client._closeOutputStream();
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.net.telnet;
/***
* Implements the telnet terminal type option RFC 1091.
***/
public class TerminalTypeOptionHandler extends TelnetOptionHandler
{
/***
* Terminal type
***/
private final String termType;
/***
* Terminal type option
***/
protected static final int TERMINAL_TYPE = 24;
/***
* Send (for subnegotiation)
***/
protected static final int TERMINAL_TYPE_SEND = 1;
/***
* Is (for subnegotiation)
***/
protected static final int TERMINAL_TYPE_IS = 0;
/***
* Constructor for the TerminalTypeOptionHandler. Allows defining desired
* initial setting for local/remote activation of this option and
* behaviour in case a local/remote activation request for this
* option is received.
* <p>
* @param termtype - terminal type that will be negotiated.
* @param initlocal - if set to true, a WILL is sent upon connection.
* @param initremote - if set to true, a DO is sent upon connection.
* @param acceptlocal - if set to true, any DO request is accepted.
* @param acceptremote - if set to true, any WILL request is accepted.
***/
public TerminalTypeOptionHandler(String termtype,
boolean initlocal,
boolean initremote,
boolean acceptlocal,
boolean acceptremote)
{
super(TelnetOption.TERMINAL_TYPE, initlocal, initremote,
acceptlocal, acceptremote);
termType = termtype;
}
/***
* Constructor for the TerminalTypeOptionHandler. Initial and accept
* behaviour flags are set to false
* <p>
* @param termtype - terminal type that will be negotiated.
***/
public TerminalTypeOptionHandler(String termtype)
{
super(TelnetOption.TERMINAL_TYPE, false, false, false, false);
termType = termtype;
}
/***
* Implements the abstract method of TelnetOptionHandler.
* <p>
* @param suboptionData - the sequence received, without IAC SB &amp; IAC SE
* @param suboptionLength - the length of data in suboption_data
* <p>
* @return terminal type information
***/
@Override
public int[] answerSubnegotiation(int suboptionData[], int suboptionLength)
{
if ((suboptionData != null) && (suboptionLength > 1)
&& (termType != null))
{
if ((suboptionData[0] == TERMINAL_TYPE)
&& (suboptionData[1] == TERMINAL_TYPE_SEND))
{
int response[] = new int[termType.length() + 2];
response[0] = TERMINAL_TYPE;
response[1] = TERMINAL_TYPE_IS;
for (int ii = 0; ii < termType.length(); ii++)
{
response[ii + 2] = termType.charAt(ii);
}
return response;
}
}
return null;
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.net.telnet;
/***
* Implements the telnet window size option RFC 1073.
* @version $Id: WindowSizeOptionHandler.java 1697293 2015-08-24 01:01:00Z sebb $
* @since 2.0
***/
public class WindowSizeOptionHandler extends TelnetOptionHandler
{
/***
* Horizontal Size
***/
private int m_nWidth = 80;
/***
* Vertical Size
***/
private int m_nHeight = 24;
/***
* Window size option
***/
protected static final int WINDOW_SIZE = 31;
/***
* Constructor for the WindowSizeOptionHandler. Allows defining desired
* initial setting for local/remote activation of this option and
* behaviour in case a local/remote activation request for this
* option is received.
* <p>
* @param nWidth - Window width.
* @param nHeight - Window Height
* @param initlocal - if set to true, a WILL is sent upon connection.
* @param initremote - if set to true, a DO is sent upon connection.
* @param acceptlocal - if set to true, any DO request is accepted.
* @param acceptremote - if set to true, any WILL request is accepted.
***/
public WindowSizeOptionHandler(
int nWidth,
int nHeight,
boolean initlocal,
boolean initremote,
boolean acceptlocal,
boolean acceptremote
) {
super (
TelnetOption.WINDOW_SIZE,
initlocal,
initremote,
acceptlocal,
acceptremote
);
m_nWidth = nWidth;
m_nHeight = nHeight;
}
/***
* Constructor for the WindowSizeOptionHandler. Initial and accept
* behaviour flags are set to false
* <p>
* @param nWidth - Window width.
* @param nHeight - Window Height
***/
public WindowSizeOptionHandler(
int nWidth,
int nHeight
) {
super (
TelnetOption.WINDOW_SIZE,
false,
false,
false,
false
);
m_nWidth = nWidth;
m_nHeight = nHeight;
}
/***
* Implements the abstract method of TelnetOptionHandler.
* This will send the client Height and Width to the server.
* <p>
* @return array to send to remote system
***/
@Override
public int[] startSubnegotiationLocal()
{
int nCompoundWindowSize = m_nWidth * 0x10000 + m_nHeight;
int nResponseSize = 5;
int nIndex;
int nShift;
int nTurnedOnBits;
if ((m_nWidth % 0x100) == 0xFF) {
nResponseSize += 1;
}
if ((m_nWidth / 0x100) == 0xFF) {
nResponseSize += 1;
}
if ((m_nHeight % 0x100) == 0xFF) {
nResponseSize += 1;
}
if ((m_nHeight / 0x100) == 0xFF) {
nResponseSize += 1;
}
//
// allocate response array
//
int response[] = new int[nResponseSize];
//
// Build response array.
// ---------------------
// 1. put option name.
// 2. loop through Window size and fill the values,
// 3. duplicate 'ff' if needed.
//
response[0] = WINDOW_SIZE; // 1 //
for ( // 2 //
nIndex=1, nShift = 24;
nIndex < nResponseSize;
nIndex++, nShift -=8
) {
nTurnedOnBits = 0xFF;
nTurnedOnBits <<= nShift;
response[nIndex] = (nCompoundWindowSize & nTurnedOnBits) >>> nShift;
if (response[nIndex] == 0xff) { // 3 //
nIndex++;
response[nIndex] = 0xff;
}
}
return response;
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.net.util;
import java.io.Serializable;
import java.util.EventListener;
import java.util.Iterator;
import java.util.concurrent.CopyOnWriteArrayList;
/**
*/
public class ListenerList implements Serializable, Iterable<EventListener>
{
private static final long serialVersionUID = -1934227607974228213L;
private final CopyOnWriteArrayList<EventListener> __listeners;
public ListenerList()
{
__listeners = new CopyOnWriteArrayList<EventListener>();
}
public void addListener(EventListener listener)
{
__listeners.add(listener);
}
public void removeListener(EventListener listener)
{
__listeners.remove(listener);
}
public int getListenerCount()
{
return __listeners.size();
}
/**
* Return an {@link Iterator} for the {@link EventListener} instances.
*
* @return an {@link Iterator} for the {@link EventListener} instances
* @since 2.0
* TODO Check that this is a good defensive strategy
*/
@Override
public Iterator<EventListener> iterator() {
return __listeners.iterator();
}
}
#Generated by Git-Commit-Id-Plugin
git.build.version=3.6.4
git.closest.tag.commit.count=
git.closest.tag.name=
git.commit.id=35f73a652cf4efc30cc57df74dbab7d5c1de757a
git.commit.id.abbrev=35f73a6
git.commit.id.describe=35f73a6
git.commit.id.describe-short=35f73a6
git.commit.message.full=ci-set-3
git.commit.message.short=ci-set-3
git.commit.user.email=shengnan.hu@ustchcs.com
git.commit.user.name=shengnan.hu
git.dirty=false
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment