Java-IO given NIO

Java-IO / NIO original address


Java's I / O can be divided into the following categories:

  • Disk operations: File
  • Byte operation: InputStream and OutputStream
  • Character Operation: Reader and Writer
  • Object operations: Serializable
  • Network Operation: Socket
  • New input / output: NIO

IO stream classification:

  • It can be divided into input / output streams.
  • Following the procedure can be divided into byte stream and character stream
  • In accordance with the character stream it can be divided into the node and the process flow stream
  • InputStream / Reader base class for all input streams, the former is the input stream of bytes, which is the character input stream.
  • OutPutStream / Writer base class for all output streams, the former is output byte stream, the output stream Houze character.

main content

First, the characteristics of the difference between NIO / NIO and IO's

  • IO is a stream-oriented, NIO buffer is facing.
  • IO flow is blocked, NIO flow is not blocked.
  • There NIO selector, but no IO.

Second, read data and write data the way

  • Data read from the channel: create a buffer, and then read the data request channel.
  • Writing data from the channel: create a buffer, stuffing data, write data and request channel.

Three, NIO core components

  • Buffer
  • Channels
  • Selectors

Buffer (buffer)

1 Introduction

  • Java NIO Buffers and NIO Channel for interaction. Channel to read data from the buffers where
    the data is written to the Buffer Channels.
  • Buffer is essentially a memory area.
  • We must master three attributes, namely: capacity capacity, position location, limit restrictions.

2. The common approach

  • Buffer chear()
  • Buffer flip()
  • Buffer rewind()
  • Buffer position(int newPosition)

Channel (Channel)

1 Introduction

  • All in all IO NIO from Channel began.
  • NIO difference between the Channel and a stream.
    2. Use FileChannel (omitted).
    3. Use SocketChannel and ServerSocketChannel (omitted).
    4. Use DatagramChannel (omitted).
    5. Scatter / Gather
  • Scatter: a distribution information read from the Channel to the N buffers (Buffer).
  • Gather: Buffer inside the N content to the order of a Channel.
    Data transmission between the 6-channel
  • If a channel is NIO FileChannel type, he can transmit data directly to another Channel.
  • transferFrom (): transferFrom method of transferring data from the source to the channel FileChannel
  • transferTo (): transferTo FileChannel method to transfer the data to another channel.

Selector (selector)

1 Introduction

  • NIO realized Reactor model of IO multiplexing, a county polling by using a time Selector way to monitor multiple channels (channel) on, so that a county can handle multiple events.
  • By configuring channel Channel listening to non-blocking, then when the IO events on Channel has not yet arrived, would not block has been waiting for, but continues in rotation other Channel, to find whether the state is readable, writable.
  • The advantage of using the Selector wherein: county to use fewer channels can be handled, and compared to the use of multiple threads, avoiding the overhead of the packet switching line caused.
  • It should be noted that only can be configured as a non-blocking socket Channel, and FileChannel can not, as a non-blocking configuration FileChannel does not make sense.

2. Use

  • Creating a selector

    Selector selector=Selector.open();
  • To register Channel Selector (Channel must be non-blocking)

    ServerSocketChannel ssChannel=ServerSocketChannel.open();
    ssChannel.configureBlocking(false);
    ssChannel.register(selector,SelectionKey.OP_ACCEPT);

to sum up

Stream Buffer-oriented and oriented

  • The biggest difference between Java NIO and IO is IO-oriented flow (Stream), NIO is block-oriented (Buffer), so what does this mean?
  面向流意味着从流中一次可以读取一个或多个字节,拿到读取的这些做什么你说了算,这里没有任何缓存(这里指的是使用流没有任何缓存,接收或者发送的数据是缓存到操作系统中的,流就像一根水管从操作系统的缓存中读取数据)而且只能顺序从流中读取数据,如果需要跳过一些字节或者再读取已经读过的字节,你必须将从流中读取的数据先缓存起来。

  面向块的处理方式有些不同,数据是先被 读/写到 buffer 中的,根据需要你可以控制读取什么位置的数据。这在处理的过程中给用户多了一些灵活性,然而,你需要额外做的工作是检查你需要的数据是否已经全部到了 buffer 中,你还需要保证当有更多的数据进入 buffer 中时,buffer 中未处理的数据不会被覆盖

Blocking and non-blocking IO IO

所有的Java IO流都是阻塞的,这意味着,当一条线程执行read()或者write()方法时,这条线程会一直阻塞知道读取到了一些数据或者要写出去的数据已经全部写出,在这期间这条线程不能做任何其他的事情

java NIO的非阻塞模式(Java NIO有阻塞模式和非阻塞模式,阻塞模式的NIO除了使用Buffer存储数据外和IO基本没有区别)允许一条线程从channel中读取数据,通过返回值来判断buffer中是否有数据,如果没有数据,NIO不会阻塞,因为不阻塞这条线程就可以去做其他的事情,过一段时间再回来判断一下有没有数据

NIO的写也是一样的,一条线程将buffer中的数据写入channel,它不会等待数据全部写完才会返回,而是调用完write()方法就会继续向下执行

scenes to be used

  • NIO allows you to use a single thread or several threads manage a number of channels (network or file), the cost of the program and a process more complex than IO
  • If you need to manage thousands of connections, but each connection to send small amounts of data, such as a chat server, it would be better achieved by NIO, like, if you need to keep a number of connections to other computers, such as P2P networks use a separate thread to manage all outlet connections is more appropriate
  • If you have only a small number of connection, but each connection occupies a very high bandwidth, and send a lot of data, traditional IO would be more suitable

I am here for you

Guess you like

Origin www.cnblogs.com/zyhbook/p/12154428.html