Basics of Non-Blocking IO — Buffers

Prasad Jayakumar
Javarevisited
Published in
3 min readDec 10, 2020
Photo by Patrick Lindenberg on Unsplash

Performing I/O usually takes orders of magnitude longer than performing in-memory processing tasks on the data. Many coders concentrate on what their objects are doing to the data and pay little attention to the environmental issues involved in acquiring and storing that data.

I/O is often the limiting factor in application performance, not processing speed
- By Ron Hitchens in book “Java NIO”

Java NIO (Non-blocking I/O) is a collection of APIs for scalable I/O. Key features includes

NIO Buffers is the basic building block for the overall implementation of the non-blocking I/O. In this blog, I will cover the behavior of Buffers. In subsequent blogs, I will cover how File and Network I/O are effectively handled.

  • A buffer is a linear, finite sequence of elements of a specific primitive type. Aside from its content, the essential properties of a buffer are its capacity, limit, and position.
  • A buffer’s capacity is the number of elements it contains. The capacity of a buffer is never negative and never changes.
  • A buffer’s limit is the index of the first element that should not be read or written. A buffer’s limit is never negative and is never greater than its capacity.
  • A buffer’s position is the index of the next element to be read or written. A buffer’s position is never negative and is never greater than its limit.
Buffer Class
ByteBuffer Basic Example
ByteBuffer Basic Example Output

There is one subclass of Buffer class for each non-boolean primitive type.

Buffer Class Hierarchy

ByteBuffer is straight-forward and most useful buffer among all the available primitive buffers. For better understanding, let’s consider CharBuffer and the conversion of CharBuffer to ByteBuffer.

We will use the following character sequences

  • ASCII characters. E.g., Hello
  • Unicode Characters. E.g., வணக்கம் (Vaṇakkam in Tamil language means Hello)

Points to note are

  1. Character sequence occupy 1 element per character in CharBuffer. For the given examples, these were 5 and 7 characters
  2. Content in CharBuffer can be converted to ByteArray only when we specify the character set for encoding. Default encoding depends on the locale and character set of the underlying operating system. We can explicitly set the character-set by using the JVM options flag -Dfile.encoding=UTF-8.
  3. A complete list of encoding set is provided here
  4. UTF-8 is defined to encode code points in one to four bytes
CharBuffer to ByteBuffer
https://en.wikipedia.org/wiki/UTF-8

For Tamil character வ, the details are

  • UTF-8 (binary) 11100000:10101110:10110101
  • Unicode 00001011 10110101 (all the bold binary digits from the above line) yields the Unicode character \u0BB5

For more details about Unicode, check here

CharBuffer to ByteBuffer
CharBuffer Basic Example

References:

  1. Stack Overflow
  2. “Java NIO” by Ron Hitchens

--

--