Java NIO (1) Overview

Java NIO Overview

Java NIO consist of the following core components:

  • Channels
  • Buffers
  • Selectors

Java NIO has more classes and components than these, but the ChannelBuffer and Selector forms the core of the API, in my opinion. The rest of the components, like Pipe and FileLock are merely utility classes to be used in conjunction with the three core components. Therefore, I‘ll focus on these three components in this NIO overview. The other components are explained in their own texts elsewhere in this tutorial. See the menu at the top corner of this page.

Channels and Buffers

Typically, all IO in NIO starts with a Channel. A Channel is a bit like a stream. From the Channel data can be read into a Buffer. Data can also be written from a Buffer into a Channel. Here is an illustration of that:

Java NIO: Channels read data into Buffers, and Buffers write data into Channels

There are several Channel and Buffer types. Here is a list of the primary Channel implementations in Java NIO:

  • FileChannel
  • DatagramChannel
  • SocketChannel
  • ServerSocketChannel

As you can see, these channels cover UDP + TCP network IO, and file IO.

There are a few interesting interfaces accompanying these classes too, but I‘ll keep them out of this Java NIO overview for simplicity‘s sake. They‘ll be explained where relevant, in other texts of this Java NIO tutorial.

Here is a list of the core Buffer implementations in Java NIO:

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

These Buffer‘s cover the basic data types that you can send via IO: byte, short, int, long, float, double and characters.

Java NIO also has a MappedByteBuffer which is used in conjunction with memory mapped files. I‘ll leave this Buffer out of this overview though.

Selectors

Selector allows a single thread to handle multiple Channel‘s. This is handy if your application has many connections (Channels) open, but only has low traffic on each connection. For instance, in a chat server.

Here is an illustration of a thread using a Selector to handle 3 Channel‘s:

Java NIO: A Thread uses a Selector to handle 3 Channel‘s

To use a Selector you register the Channel‘s with it. Then you call it‘s select() method. This method will block until there is an event ready for one of the registered channels. Once the method returns, the thread can then process these events. Examples of events are incoming connection, data received etc.

时间: 2025-01-02 08:46:16

Java NIO (1) Overview的相关文章

Java NIO Overview

Java Nio 1 Java NIO Tutorial 2 Java NIO Overview 3 Java NIO Channel 4 Java NIO Buffer 5 Java NIO Scatter / Gather 6 Java NIO Channel to Channel Transfers 7 Java NIO Selector 8 Java NIO FileChannel 9 Java NIO SocketChannel 10 Java NIO ServerSocketChan

【JAVA】【NIO】2、Java NIO Overview

Java NIO主要有以下核心组件构成: 通道 缓存 选择器 Java NIO其实有比上面更多的类和组件,但是在我看来,Channel,Buffer,Selector是核心的API.其它组件,像管道和文件锁,仅仅只是在与三大核心组件结合使用时的通用工具类. 通道和缓存 一般来说,NIO中的所有IO都是从通道开始的.通道就像一个流,数据可以从通道中读到一个缓存里去,同样,数据可以从缓存写进通道里 上面代表读操作,下面代表写操作 在Java NIO中,主要有如下几种通道: ·FileChannel

Java NIO:NIO概述

在上一篇博文中讲述了几种IO模型,现在我们开始进入Java NIO编程主题.NIO是Java 4里面提供的新的API,目的是用来解决传统IO的问题.本文下面分别从Java NIO的几个基础概念介绍起. 以下是本文的目录大纲: 一.NIO中的几个基础概念 二.Channel 三.Buffer 四.Selector 若有不正之处,请多多谅解并欢迎批评指正. 一.NIO中的几个基础概念 在NIO中有几个比较关键的概念:Channel(通道),Buffer(缓冲区),Selector(选择器). 首先从

Java NIO系列教程(一) Java NIO 概述

Java NIO 由以下几个核心部分组成: Channels Buffers Selectors 虽然Java NIO 中除此之外还有很多类和组件,但在我看来,Channel,Buffer 和 Selector 构成了核心的API.其它组件,如Pipe和FileLock,只不过是与三个核心组件共同使用的工具类.因此,在概述中我将集中在这三个组件上.其它组件会在单独的章节中讲到. Channel 和 Buffer 基本上,所有的 IO 在NIO 中都从一个Channel 开始.Channel 有点

Java NIO 完全学习笔记(转)

本篇博客依照 Java NIO Tutorial翻译,算是学习 Java NIO 的一个读书笔记.建议大家可以去阅读原文,相信你肯定会受益良多. 1. Java NIO Tutorial Java NIO,被称为新 IO(New IO),是 Java 1.4 引入的,用来替代 IO API的. Java NIO:Channels and Buffers 标准的 Java IO API ,你操作的对象是字节流(byte stream)或者字符流(character stream),而 NIO,你操

Java NIO学习笔记(一)

文章目录: 1.什么是IO 2.什么是Java NIO 3.I/O常见概念 4.为什么使用NIO 5.IO VS NIO 一.什么是IO I/O 或者输入/输出 , 指的是计算机与外部世界或者一个程序与计算机的其余部分的之间的接口.它对于任何计算机系统都非常关键,因而所有 I/O 的主体实际上是内置在操作系统中的.单独的程序一般是让系统为它们完成大部分的工作.在 Java 编程中,直到最近一直使用 流 的方式完成 I/O.所有 I/O 都被视为单个的字节的移动,通过一个称为 Stream 的对象

java进阶 ------ Java NIO

Java NIO I/O简介 I/O或者输入输出指的是计算机与外部世界或者一个程序与计算机的其余部分之间的接口.它对于任何计算机系统都非常关键,因而所有I/O的主体实际上是内置在操作系统中的.单独的程序一般是让系统为它们完成大部分的工作. 在Java编程中,直到最近一直使用流的方式完成I/O.所有I/O都被视为单个的字节的移动,通过一个称为Stream的对象一次移动一个字节.流I/O用于与外部世界接触.它也在内部使用,用于将对象转换为字节,然后再转换回对象. NIO与原来的I/O有同样的作用和目

Java NIO DatagramChannel

Java Nio 1 Java NIO Tutorial 2 Java NIO Overview 3 Java NIO Channel 4 Java NIO Buffer 5 Java NIO Scatter / Gather 6 Java NIO Channel to Channel Transfers 7 Java NIO Selector 8 Java NIO FileChannel 9 Java NIO SocketChannel 10 Java NIO ServerSocketChan

Java NIO vs. IO

Java Nio 1 Java NIO Tutorial 2 Java NIO Overview 3 Java NIO Channel 4 Java NIO Buffer 5 Java NIO Scatter / Gather 6 Java NIO Channel to Channel Transfers 7 Java NIO Selector 8 Java NIO FileChannel 9 Java NIO SocketChannel 10 Java NIO ServerSocketChan