Java Reference
In-Depth Information
receiving and displaying data in the background. */
class Handler extends Thread {
StreamConnection connection;
InputStream in;
OutputStream out;
boolean leave;
/** Establishes a connection to the given URI */
public Handler (String uri) throws IOException {
connection = (StreamConnection) Connector.open
(uri, Connector.READ_WRITE, true);
out = connection.openOutputStream();
in = connection.openInputStream();
show ("opened: "+uri + "\r");
}
/** Like in.read(), but additional performs telnet
parameter negotiations. */
public int read() throws IOException {
while (true) {
int i = in.read();
if (i != 0x0ff) return i;
int cmd = in.read();
if (cmd == 0x0ff)
return 0x0ff;
int opt = in.read();
if (cmd == 0xfd || cmd == 0x0fb) {
out.write (0x0ff);
out.write (cmd == 0xfd ? 252 : 254);
out.write (opt);
out.flush();
}
}
}
/** Collects incoming data in the background and
shows it if the buffer size reaches 1 k or
no more data is available at the moment. */
public void run() {
StringBuffer buf = new StringBuffer();
try {
while (!leave) {
do {
int i = in.read();
if (i == -1) disconnect();
Search WWH ::




Custom Search