Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BufferedReader directly to byte[]

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,

like image 906
Eveli Avatar asked Feb 27 '13 08:02

Eveli


People also ask

Can BufferedReader read bytes?

BufferedReader in = new BufferedReader(new FileReader("foo.in")); will buffer the input from the specified file. Without buffering, each invocation of read() or readLine() could cause bytes to be read from the file, converted into characters, and then returned, which can be very inefficient.

What is difference between BufferedReader and InputStreamReader?

BufferedReader reads a couple of characters from the Input Stream and stores them in a buffer. InputStreamReader reads only one character from the input stream and the remaining characters still remain in the streams hence There is no buffer in this case.

Is BufferedReader faster than FileReader?

As BufferedReader uses buffer internally, this class is much faster than FileReader. BufferReader doesn't need to access the hard drive every time like FileReader and hence faster.


1 Answers

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...
}
like image 168
Jon Skeet Avatar answered Sep 28 '22 07:09

Jon Skeet