Reading and writing from/to the same file using InputStream and OutputStream

I am creating a program which needs to read and write data to a file concurrently.

The main question is this: If the reads and writes will be performed using InputStream and OutputStream classes running on a separate thread, what are the catches and edge cases that I will need to be aware of in order to prevent data corruption?

So far I only notice one, which is in case of the writer being faster than the reader, the later has to block (upon receiving the value -1) until the next item has been available.

To better illustrate the scenario, the program is written in Scala and I will use a separate scala.concurrent.Future instance for my reading and writing operations. I have configured these Futures to be run on a ThreadPoolExecutor.

val in: InputStream = FileInputStream("file.bin")
val out: OutputStream = FileOutputStream("file.bin")
val infiniteData: Stream[Int] = ???
val outIter: Iterator[Int] = infiniteData.iterator // data to be written
val inBuffer: mutable.List[Int] = ??? // data read will be buffered here
val attemptWrite = Future {
    while(true) out.write(buffer.next())
}

val read = Future {
    while(true) inBuffer += in.read()
}

NOTE: In case anyone is wondering if I have considered other, non-InputStream based approach, the answer is yes, I have but unfortunately it’s not possible in this project since the program uses libraries that only works with InputStream and OutputStream.


Source: java

Leave a Reply