Questions tagged [nio]

NIO is Java 'New I/O' introduced in 1.4, providing non-blocking and multiplexed network I/O; 'direct' (native) buffers; file locks and mapped files; and character set codecs.

NIO stands for 'New I/O'. It was introduced in JDK 1.4 in the java.nio package. It comprises several elements:

  1. A family of buffers that encapsulate the current position and limit, and can contain primitive types and arrays of them, and whose data can be held at either the Java level or the native level, the latter via 'direct' buffers. Copying I/O between channels using direct buffers need not cross the JNI layer into Java at all, which has considerable speed benefits.
  2. A family of channels that can perform blocking or non-blocking I/O, or non-blocking multiplexed I/O via the Java equivalent of the Unix select() function, which avoids the necessity in java.net to create a thread per connection, and hence improves scalability. There is also a FileChannel with locking primitives and a capability to provide memory-mapped files. A Pipe class with a pair of selectable channels is also provided.
  3. A family of character set codecs.
  4. Starting in Java 1.7, NIO was extended to support non-blocking I/O for Filesystem reads and writes as well as NIO support for Multicast.

Java NIO Libraries

The NIO package is a fairly low level API but several third party libraries have emerged that simplify the development of NIO leveraging software:

  • Netty: Netty is an asynchronous event-driven network application framework for rapid development of maintainable high performance protocol servers & clients.
  • Apache MINA: Apache MINA is a network application framework which helps users develop high performance and high scalability network applications easily. It provides an abstract, event-driven,asynchronous API over various transports such as TCP/IP and UDP/IP via Java NIO.
  • Grizzly: The Grizzly NIO and Web framework has been designed to help developers to take advantage of the Java™ NIO API. Grizzly's goal is to help developers to build scalable and robust servers using NIO and we are also offering extended framework components: Web Framework (HTTP/S), Bayeux Protocol, Servlet, HttpService OSGi and Comet.
  • Vert.x: Effortless asynchronous application development for the modern web and enterprise, supporting components in JavaScript, Ruby, Groovy or Java and the ability to mix and match several programming languages in a single application.
  • xsocket: xSocket is an easy to use NIO-based network library to build high performance, highly scalable network applications. (Development discontinued)
  • NIO Framework: The NIO Framework is a library on top of NIO that hides most of the complexity of plain NIO. With the NIO Framework you can implement high-performance Java network applications without having to deal with all the nasty details of NIO.
3015 questions
1
vote
1 answer

ByteArrayOutputStream vs. CharBuffer with CharSet

I am just trying to avoid some large array copies(byte[] arrays). I have a String of size n I have a byte[] of size m I am using ISO-8859-1 for the String. I would very much like to write the String to byte[0] to byte[n-1] positions in this array,…
Dean Hiller
  • 19,235
  • 25
  • 129
  • 212
1
vote
1 answer

How to find the size of a ReadableByteChannel in Java?

I am converting a blob from a database into a PDF, using Java's Channel classes. When using a Channel's transferFrom( ) method, you are supposed to specify the maximum number of bytes to be transferred. How are you supposed to find this maximum…
jabe
  • 784
  • 2
  • 15
  • 33
1
vote
1 answer

ByteBuffer and partial write

If the ByteBuffer is written partially, the position is updated and the next _channel.write call will resume from last position, yep? compact() is not necessary? private AsynchronousSocketChannel _channel; private ByteBuffer _buffer; final…
Mr.Wang from Next Door
  • 13,670
  • 12
  • 64
  • 97
1
vote
0 answers

Files.getFileAttributeView() blocking?

I have a peculiar problem with the java.nio file system API. My application is running on Windows 8, Java version 1.8. I attach an alternate data stream to my files for my application scenarios. Below is the code: log.info("Path for which file id…
saurav
  • 5,388
  • 10
  • 56
  • 101
1
vote
0 answers

Android app connection exception to NIO server socket

I am trying to trace a request originating from android and send some data to serversocket at each hop. I am using below code to execute client and send data to ServerSocket at each hop (tomcats having servlet projects) and they are able to send…
teksan
  • 142
  • 13
1
vote
1 answer

Is FileChannel#force is equivalent to OutputStream#flush? Do I always have to call it?

I have a class works like FilterOutputStream. public class WritableFilterChannel implements WritableChannel { public WritableFilterChannel(final WritableByteChannel channel) { super(); this.channel = channel; } //…
Jin Kwon
  • 20,295
  • 14
  • 115
  • 184
1
vote
2 answers

Why FileChannel reading never ends?

When I execute following class import java.io.*; import java.nio.*; import java.nio.file.*; import java.nio.channels.*; public class FileChannelTest { …
Jin Kwon
  • 20,295
  • 14
  • 115
  • 184
1
vote
3 answers

How safe is it to use Java FileLock?

How safe is it to use java.nio.channels.FileLock for locking files among processes? It is stated that other processes can not access the file if we have an exclusive lock. However, the below answer on another SO question states other processes have…
Guillotine1789
  • 342
  • 4
  • 12
1
vote
0 answers

Java NIO TCP Server connection handling issue

I read an article and wrote a TCP server that handles approximately 10000 connections and I also wrote a program that opens 10000 client connections for testing the server program. When I communicate with them using loop back address on the same…
overlord
  • 489
  • 1
  • 7
  • 20
1
vote
1 answer

What exactly are the restrictions on accessing a buffer being used for asynchronous I/O?

According to the documentation for AsynchronousFileChannel, The ByteBuffers used when reading or writing are not safe for use by multiple concurrent I/O operations. Furthermore, after an I/O operation is initiated then care should be taken to…
user253751
  • 57,427
  • 7
  • 48
  • 90
1
vote
2 answers

Advenced Socket Programming - Sever to client extra data transfer problem

Here One server and one client is there. And communication has been maintained by selectable Channel. like-- Server --- SelectionKey selectKey = channel.register(this.selector, SelectionKey.OP_ACCEPT); while…
Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
1
vote
0 answers

Thread info in the Thread dump

I have an unique problem which happens in the user's system which is not reproducible in any other system. System does not crash. So this my thread model. I have a main SWT thread which triggers few daemon threads and of which a daemon thread…
saurav
  • 5,388
  • 10
  • 56
  • 101
1
vote
1 answer

Create a path with double forward slashes using static Paths.get method

I'm trying to pass string like that "hdfs://some_address/user/folder_with_files" to Paths.get method. Unfortunately as a result i always get "hdfs:/some_address/user/folder_with_files" instead of "hdfs://.....". Escape characters doesn't work…
UnderNotic
  • 345
  • 5
  • 16
1
vote
0 answers

COPY_ATTRIBUTES not working with Zip filesystem

The Zip filesystem doesn't copy file attributes when using the Java nio Files.copy method with StandardCopyOption.COPY_ATTRIBUTES. Is it supposed to? The following fully working example code demonstrates the issue. It copies two files into a zip…
Klitos Kyriacou
  • 10,634
  • 2
  • 38
  • 70
1
vote
1 answer

Netty channel traffic shaping issue : channelWritabilityChanged method not trigger

I want to control bandwidth for data transfer. According to Netty document, they suggest: In your handler, you should consider to use the channel.isWritable() and channelWritabilityChanged(ctx) to handle writability, or through…
The KNVB
  • 3,588
  • 3
  • 29
  • 54
1 2 3
99
100