BufferedReader directly to byte[]

Ekonion picture Ekonion · Feb 27, 2013 · Viewed 50.4k times · Source

is there any possibility my following BufferedReader is able to put the input directly into a byte[]?

public static Runnable reader() throws IOException {
    Log.e("Communication", "reader");
    din = new DataInputStream(sock.getInputStream());
    brdr = new BufferedReader(new InputStreamReader(din), 300);
    boolean done = false;
    while (!done) {
       try {
       char[] buffer = new char[200];
           int length = brdr.read(buffer, 0, 200);
           String message = new String(buffer, 0, length);
           btrar = message.getBytes("ISO-8859-1");                      
           int i=0;
           for (int counter = 0; counter < message.length(); counter++) {
              i++;  
              System.out.println(btrar[counter] + " = " + " btrar "  + i);
           }
    ...

thats the part of the reader, pls have a look.

I want the input directly to btrar,

Answer

Jon Skeet picture Jon Skeet · Feb 27, 2013

is there any possibility my following BufferedReader is able to put the input directly into a byte[]?

Any Reader is designed to let you read characters, not bytes. To read binary data, just use an InputStream - using BufferedInputStream to buffer it if you want.

It's not really clear what you're trying to do, but you can use something like:

BufferedInputStream input = new BufferedInputStream(sock.getInputStream());
while (!done) {
    // TODO: Rename btrar to something more meaningful
    int bytesRead = input.read(btrar);
    // Do something with the data...
}