~/ learn/ comp-348/ cards/ Where bytes become text: readers, writers, encodings
1 of 5

Decode a socket into lines with an explicit charset

Decode a socket into lines with an explicit charset

Answer

BufferedReader in = new BufferedReader( new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8)); String line = in.readLine();

InputStreamReader is the crossing from bytes to characters and the only place the charset is named; BufferedReader adds readLine(), which returns the line without its terminator and null at end of stream.

Harold 4e ch2 §Readers and Writers

space flip · ← → navigate · esc to exit
NORMAL ~/memra/library/175587e6-9194-46bc-af02-02b104582e02/flashcard utf-8 LF