~/ learn/ comp-348/ cards/ InputStream and the shape of a read
1 of 4

The copy loop: read into a buffer, write only what arrived

The copy loop: read into a buffer, write only what arrived

Answer

byte[] buf = new byte[8192]; int count; while ((count = in.read(buf)) != -1) { out.write(buf, 0, count); } out.flush();

The three-argument write is the whole point: writing buf without the count would send 8,192 bytes every pass, padding the output with stale bytes from the previous read. This loop is how every file and every response body in the course gets copied.

Harold 4e ch2 §Input Streams

space flip · ← → navigate · esc to exit
NORMAL ~/memra/library/a89a358a-3595-48c9-8546-04a1886b0016/flashcard utf-8 LF