📄 telnetinputstream.java
字号:
package org.fife.net;
import java.io.FilterInputStream;
import java.io.InputStream;
import java.io.IOException;
/**
* An input stream that handles carriage returns and CR/LF pairs correctly,
* as specified by RFC 854.
*
* @author Robert Futrell
* @version 0.5
*/
public class TelnetInputStream extends FilterInputStream {
/**
* Whether the stream is being read as binary.
*/
private boolean binary;
/*****************************************************************************/
/**
* Constructor.
*
* @param in The input stream to read from.
* @param binary Whether or not data from <code>in</code> should be
* read as binary, or as ASCII.
*/
public TelnetInputStream(InputStream in, boolean binary) {
super(in);
this.binary = binary;
}
/*****************************************************************************/
/**
* Returns whether this stream is being read as binary.
*
* @return Whether this is a binary input stream.
*/
public boolean isBinary() {
return binary;
}
/*****************************************************************************/
/**
* Reads a single character from the input stream.
*
* @return The character read, or <code>-1</code> if the end of stream
* has been reached.
* @throws IOException If an I/O error occurs.
*/
public int read() throws IOException {
int ch = super.read();
if (binary)
return ch;
switch (ch) {
case '\r':
switch (ch = super.read()) {
case '\0': // Just a carriage return.
return '\r';
case '\n': // A newline.
return '\n';
case -1: // End of stream; this is an error.
throw new IOException("Unexpected end-of-input");
default: // Anything else is an error.
throw new IOException("Invalid char after CR: " +
((char)ch));
}
default:
return ch;
}
}
/*****************************************************************************/
/**
* Reads a given number of bytes from the input stream and stores them
* in the specified array.
*
* @param b The array in which to store the input.
* @param offset The offset into <code>b</code> at which to begin
* storing.
* @param length The number of bytes to store.
* @return The number of bytes actually read.
* @throws IOException If an I/O error occurs.
*/
public int read(byte[] b, int offset, int length) throws IOException {
if (binary)
return super.read(b, offset, length);
for (int i=0; i<length; i++) {
int ch = read();
switch (ch) {
case -1: // End of input reached.
return i==0 ? -1 : i;
default:
b[offset++] = (byte)ch;
}
}
return length;
}
/*****************************************************************************/
/**
* Attempts to read a line of text from the input stream. Since a
* telnet input stream always ends lines in CR/LF pairs, this method
* returns all characters in the stream until a CR/LF pair or the
* end-of-stream is reached. If no more bytes are available in the
* stream, <code>null</code> is returned.
*
* @return A string representing the next line in the input stream.
* @throws IOException If an I/O error occurs.
*/
public String readLine() throws IOException {
int ch = read();
if (ch==-1)
return null;
StringBuffer buf = new StringBuffer(80);
buf.append((char)ch);
while (true) {
ch = read();
switch (ch) {
case '\r':
ch = read();
switch (ch) {
case '\n':
return buf.toString();
case '\0':
buf.append('\r');
break;
case -1:
throw new IOException("Unexpected end of " +
"input");
default:
throw new IOException("Invalid char " +
"after CR: " + ((char)ch));
}
case -1:
return buf.toString();
default:
buf.append((char)ch);
}
}
}
/*****************************************************************************/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -