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...
}
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, 2022Comments
-
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 about 11 years@Ekonion: Yes, using a
Reader
when you want binary data is wrong. -
Eveli about 11 yearshmm.. 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 about 11 years@Ekonion: Well the code I've given you is reading into
btrar
, which I assume is declared somewhere else, as abyte[]
. It's not at all clear what you want compared with what I've given you... -
Eveli about 11 yearsif it is reading into
btrar
, what isbytesRead
for? Maybe a mistelling of mine. The incomeing data should put into btrar.. the name BytesToReceiveARray comes therefrom. -
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 variableincomingData
or something similar - it's fine that there's a meaning forbtrar
, but you couldn't possibly expect anyone else to understand that meaning from just the name. -
Eveli about 11 yearsomg... how many.. obvious.. xD sorry. I expected, you dont know the meaning, so i told you ^^
-
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 about 11 yearsi will remember that and change names of "nothingsaying" vars to "selfexplaining" ^^
-
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 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 almost 8 years@ceph3us: No, you should ask a new question.