Skip to content

Instantly share code, notes, and snippets.

@simonwoo
Last active January 18, 2016 15:12
Show Gist options
  • Select an option

  • Save simonwoo/46443f5fe015097eee26 to your computer and use it in GitHub Desktop.

Select an option

Save simonwoo/46443f5fe015097eee26 to your computer and use it in GitHub Desktop.

the principle of a program reading data from a source and writing it to some destination: Source -> Program -> Destination

The most typical sources and destinations of data are these:

  • Files
  • Pipes
  • Network Connections
  • In-memory Buffers (e.g. arrays)
  • System.in, System.out, System.error

IO Streams are a core concept in Java IO. A stream is a conceptually endless flow of data. You can either read from a stream or write to a stream. A stream is connected to a data source or a data destination. Streams in Java IO can be either byte based (reading and writing bytes) or character based (reading and writing characters). An InputStream or Reader is linked to a source of data. An OutputStream or Writer is linked to a destination of data.

Source -> InputStream/Reader -> Program
Program -> outputStream/Writer -> Destination

Java IO classes are divided by input, output, being byte based or character based, and any more specific purpose they may be addressing, like buffering, parsing etc.

Java NIO(New IO)是一个可以替代标准Java IO API的IO API(从Java 1.4开始),Java NIO提供了与标准IO不同的IO工作方式。

  • Java NIO: Channels and Buffers(通道和缓冲区).标准的IO基于字节流和字符流进行操作的,而NIO是基于通道(Channel)和缓冲区(Buffer)进行操作,数据总是从通道读取到缓冲区中,或者从缓冲区写入到通道中。
  • Java NIO: Non-blocking IO(非阻塞IO).JavaNIO可以让你非阻塞的使用IO,例如:当线程从通道读取数据到缓冲区时,线程还是可以进行其他事情。当数据被写入到缓冲区时,线程可以继续处理它。从缓冲区写入通道也类似。
  • Java NIO: Selectors(选择器).JavaNIO引入了选择器的概念,选择器用于监听多个通道的事件(比如:连接打开,数据到达)。因此,单个的线程可以监听多个数据通道。
Channel -> Buffer
Channel <- Buffer

Channel:

  • FileChannel
  • DatagramChannel
  • SocketChannel
  • ServerSocketChannel

Buffer:

  • ByteBuffer
  • CharBuffer
  • DoubleBuffer
  • FloatBuffer
  • IntBuffer
  • LongBuffer
  • ShortBuffer

Selector允许单线程处理多个 Channel。如果你的应用打开了多个连接(通道),但每个连接的流量都很低,使用Selector就会很方便。 这是一个selector处理三个channel的例子。

                    Thread
                      |
                    Selector
                      |
              --------|--------
              |       |       |
          Channel   Channel  Channel

使用Buffer读写数据一般遵循以下四个步骤:

  • 写入数据到Buffer
  • 调用flip()方法
  • 从Buffer中读取数据 -调用clear()方法或者compact()方法

当向buffer写入数据时,buffer会记录下写了多少数据。一旦要读取数据,需要通过flip()方法将Buffer从写模式切换到读模式。在读模式下,可以读取之前写入到buffer的所有数据。

一旦读完了所有的数据,就需要清空缓冲区,让它可以再次被写入。有两种方式能清空缓冲区:调用clear()或compact()方法。clear()方法会清空整个缓冲区。compact()方法只会清除已经读过的数据。任何未读的数据都被移到缓冲区的起始处,新写入的数据将放到缓冲区未读数据的后面。

缓冲区本质上是一块可以写入数据,然后可以从中读取数据的内存。这块内存被包装成NIO Buffer对象,并提供了一组方法,用来方便的访问该块内存。

为了理解Buffer的工作原理,需要熟悉它的三个属性:

  • capacity
  • position
  • limit

flip方法将Buffer从写模式切换到读模式。调用flip()方法会将position设回0,并将limit设置成之前position的值。换句话说,position现在用于标记读的位置,limit表示之前写进了多少个byte、char等 —— 现在能读取多少个byte、char等。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment