BufferedReader directly to byte[]

53,610

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...
}
Share:
53,610
Eveli
Author by

Eveli

Did Android a few years ago, changed now to Apple, starting from 0 again ^^help is appreciated <3

Updated on July 09, 2022

Comments

  • Eveli
    Eveli almost 2 years

    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,

  • Jon Skeet
    Jon Skeet about 11 years
    @Ekonion: Yes, using a Reader when you want binary data is wrong.
  • Eveli
    Eveli about 11 years
    hmm.. dont seems to be what i want.. see, i want the String message = new String(buffer, 0, length); not as a string. This should be a byte[]. That is what i try to achieve
  • Jon Skeet
    Jon Skeet about 11 years
    @Ekonion: Well the code I've given you is reading into btrar, which I assume is declared somewhere else, as a byte[]. It's not at all clear what you want compared with what I've given you...
  • Eveli
    Eveli about 11 years
    if it is reading into btrar, what is bytesRead for? Maybe a mistelling of mine. The incomeing data should put into btrar.. the name BytesToReceiveARray comes therefrom.
  • Jon Skeet
    Jon Skeet about 11 years
    @Ekonion: bytesRead indicates how many bytes have been read. (It may not fill the whole byte array.) I'd name the variable incomingData or something similar - it's fine that there's a meaning for btrar, but you couldn't possibly expect anyone else to understand that meaning from just the name.
  • Eveli
    Eveli about 11 years
    omg... how many.. obvious.. xD sorry. I expected, you dont know the meaning, so i told you ^^
  • Jon Skeet
    Jon Skeet about 11 years
    @Ekonion: My point is that a name should be meaningful without a separate explanation. But I'm glad the code has fixed your problem.
  • Eveli
    Eveli about 11 years
    i will remember that and change names of "nothingsaying" vars to "selfexplaining" ^^
  • ceph3us
    ceph3us almost 8 years
    @JonSkeet - how about mixed data ? i want to read from socket stream which contains HTTP [RFC2616] header and binary data as body (eg JPG file) ? so i have created a CRLFBytesReader to read a header line by line and now i want to get rest of remaining bytes from Reader InputStream???
  • Jon Skeet
    Jon Skeet almost 8 years
    @ceph3us: I have no idea what you mean by "BOS of Reader", and it's not really clear what you're asking. Perhaps you should ask a new question. Your CRLFBytesReader would have to be careful to buffer any binary data as binary data though.
  • Jon Skeet
    Jon Skeet almost 8 years
    @ceph3us: No, you should ask a new question.