3 ms·
It is rather non-trivial to write a program to copy input to output if one requires proper handling of a termination signal when the code flushes the already re
by _0w8t 5y ago
It is rather non-trivial to write a program to copy input to output if one requires proper handling of a termination signal when the code flushes the already read data without waiting for more and exits without loosing any buffered data. Knowing how to do that is a good sign that the person really knows the language and it’s libraries.
- throwaway2037 5y agoCan this be done with vanilla Java? If yes, can you share a snip of code? I would curious to learn! Using POSIX C, I think I know the solution.
- xmodem 5y agoYou need to register an event handler with `Runtime.getRuntime().addShutdownHook()`.
- _0w8t 5y agoAs another comment has written one needs Runtime.getRuntime().addShutdownHook(). The hook then, for example, closes the input stream to ensure that it’s reader is woken up from a blocking read and then waits until the already read data is written. In turn this requires to know how synchronization works.
- Scarbutt 5y agoWouldn't just wrapping System.in with the Scanner class handle this?