Java I/O
,Java IO and NIO
• Java allows to manage IO in two different ways
• IO
• NEW/NON Blocking IO
IO NIO
Stream oriented Buffer oriente
Blocking IO Non blocking I
Selectors
,Stream Oriented vs. Buffer Oriented
• The first big difference between Java NIO and IO is that IO is stre
oriented, where NIO is buffer oriented. So, what does that mean
• Java IO being stream oriented means that you read one or more
time, from a stream. What you do with the read bytes is up to y
are not cached anywhere. Furthermore, you cannot move forth
in the data in a stream. If you need to move forth and back in th
read from a stream, you will need to cache it in a buffer first.
• Java NIO's buffer oriented approach is slightly different. Data is
buffer from which it is later processed. You can move forth and
buffer as you need to. This gives you a bit more flexibility during
processing. However, you also need to check if the buffer contai
data you need in order to fully process it. And, you need to mak
when reading more data into the buffer, you do not overwrite d
buffer you have not yet processed.
, Blocking vs. Non-blocking IO
• Java IO's various streams are blocking. That means, that when a thread
a read() or write(), that thread is blocked until there is some data to rea
data is fully written. The thread can do nothing else in the meantime.
• Java NIO's non-blocking mode enables a thread to request reading data
channel, and only get what is currently available, or nothing at all, if no
currently available. Rather than remain blocked until data becomes ava
reading, the thread can go on with something else.
• The same is true for non-blocking writing. A thread can request that so
written to a channel, but not wait for it to be fully written. The thread c
on and do something else in the mean time.
• What threads spend their idle time on when not blocked in IO calls, is u
performing IO on other channels in the meantime. That is, a single thre
manage multiple channels of input and output.
,Java IO and NIO
• Java allows to manage IO in two different ways
• IO
• NEW/NON Blocking IO
IO NIO
Stream oriented Buffer oriente
Blocking IO Non blocking I
Selectors
,Stream Oriented vs. Buffer Oriented
• The first big difference between Java NIO and IO is that IO is stre
oriented, where NIO is buffer oriented. So, what does that mean
• Java IO being stream oriented means that you read one or more
time, from a stream. What you do with the read bytes is up to y
are not cached anywhere. Furthermore, you cannot move forth
in the data in a stream. If you need to move forth and back in th
read from a stream, you will need to cache it in a buffer first.
• Java NIO's buffer oriented approach is slightly different. Data is
buffer from which it is later processed. You can move forth and
buffer as you need to. This gives you a bit more flexibility during
processing. However, you also need to check if the buffer contai
data you need in order to fully process it. And, you need to mak
when reading more data into the buffer, you do not overwrite d
buffer you have not yet processed.
, Blocking vs. Non-blocking IO
• Java IO's various streams are blocking. That means, that when a thread
a read() or write(), that thread is blocked until there is some data to rea
data is fully written. The thread can do nothing else in the meantime.
• Java NIO's non-blocking mode enables a thread to request reading data
channel, and only get what is currently available, or nothing at all, if no
currently available. Rather than remain blocked until data becomes ava
reading, the thread can go on with something else.
• The same is true for non-blocking writing. A thread can request that so
written to a channel, but not wait for it to be fully written. The thread c
on and do something else in the mean time.
• What threads spend their idle time on when not blocked in IO calls, is u
performing IO on other channels in the meantime. That is, a single thre
manage multiple channels of input and output.