0

Apache telnet uses InputStream, which I want to read as a String (or String like data).

How can I transform the InputStream into something easier (for me) to deal with, a StringBuffer or similar.

Thufir
  • 8,216
  • 28
  • 125
  • 273

3 Answers3

2

You can consider IOUtils.toString(is) from Apache Common IO

public static String toString(InputStream input)
                       throws IOException

from the doc:

This method buffers the input internally, so there is no need to use a BufferedInputStream.

rajesh
  • 3,247
  • 5
  • 31
  • 56
1

You cant transform the InputStream into StringBuffer. But if you want to make use of StringBuffer then you can read the input from InputStream as a int and append it to StringBuffer like below.

int i=fis.read(); //it will read Character from stream and returns ascii value of char.
StringBuffer sb=new StringBuffer();
sb.append((char)i); //This will cast int into character and append to StringBuffer.
Vimal Bera
  • 10,346
  • 4
  • 25
  • 47
  • maybe I'm doing it wrong? this seems to result in an int, number of characters or something, rather than the words I would expect. – Thufir Aug 28 '13 at 06:46
  • @Thufir yea. fis.read() reads bytes of data and parse into `int`. But you can read multiple character by specifying byte[] in read() method. Refer [this](http://docs.oracle.com/javase/6/docs/api/java/io/FileInputStream.html) – Vimal Bera Aug 28 '13 at 06:49
  • this would only print numbers, you wont get any characters. even with byte, you still get numbers. – happymeal Aug 28 '13 at 06:53
  • you have to cast it into char like this Character ch=(char)i to get character from FileInputStream. Check my answer again, I edited it. – Vimal Bera Aug 28 '13 at 06:56
0

This is partway there:

package teln;

import static java.lang.System.out;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.SocketException;
import org.apache.commons.net.telnet.TelnetClient;

public final class ConnectMUD {

    private static String EOL = "/[#]/";
    private static TelnetClient tc;

    public static void main(String[] args) throws SocketException, IOException {
        tc = new TelnetClient();
        tc.connect("some_mud.com", 123);
        readLines(tc.getInputStream());
    }

    private static void readLines(InputStream in) throws IOException {

        InputStreamReader is = new InputStreamReader(in);
        StringBuilder sb = new StringBuilder();
        BufferedReader br = new BufferedReader(is);
        String read = br.readLine();

        while (read != null) {
            out.println(read);
            sb.append(read);
            read = br.readLine();
            parseLine(read);
        }
    }

    private static void parseLine(String read) {
        login(read);
    }

    private static void login(String read) {
        //hmm, how do you know when you
        //get some funky, non-standard login?
        //look for an EOL of some sort?
    }
}

there's a potential for some very large strings there, but I'm just focused on functionality first. Just trying to start parsing lines for playing da MUD.

Thufir
  • 8,216
  • 28
  • 125
  • 273