0

Looking at this standard code except for copying bytes, I'd like to know, how the streams hold their internal state.

InputStream in = new FileInputStream(...);
InputStream out = new FileOutputStream(...);
int readLength = 0;
byte[] buf = new byte[1024];

while ( (readLength = in.read(buf)) >= 0)
    out.write(buf, 0, readLength);

I assume that both streams hold an internal pointer or counter to the current location in the file, that gets updated on read/write by the length of the read/written chunk. But neither the documentation nor the Oracle tutorials mention this and leave the reader to deduce on his own, that the stream starts each read/write at a "current" location. Before I start digging in OpenJDK, I thought to try asking here first.

Ondrej Sotolar
  • 1,352
  • 1
  • 19
  • 29
  • 1
    If you check the code, you'll see that this is delegated to a native code function present in the platform specific implementation. Probably, they just use the read/write pointer of the underlying native file descriptor. – mtj Jul 02 '20 at 10:08
  • 1
    I know it is not FileInputStream or FileOutputStream, but this oracle old document may help you. https://docs.oracle.com/javase/6/docs/api/java/io/RandomAccessFile.html – Pankaj Jul 02 '20 at 10:14
  • 1
    They don't need pointers. Read and write in any operating system operate on the next position in the file. They just provide standard sequential I/O. No pointers, or special documentation, required. – user207421 Jul 02 '20 at 10:25

0 Answers0