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
3 answers

Java ByteBuffer clear data

I know that Java's ByteBuffer.clear() is not really to clean all data in ByteBuffer, so when I StringBuilder.append() string every time, the final result has always appended all remaining chars in ByteBuffer, which are the old data from last write,…
Andy Chan
  • 277
  • 5
  • 16
1
vote
1 answer

Why doesn't Files.isHidden() working correctly?

I'm messing around with Java NIO and for some reason I can't get Files.isHidden() to return the correct boolean value. The program just checks to see if the directory is hidden then if it is hidden will make it visible and if it is not hidden it…
MeesterMarcus
  • 700
  • 1
  • 9
  • 25
1
vote
1 answer

How do you change file permissions in Java using NIO library on Windows 7

I'm trying to write code that starts with a Path object that is for a specific file and makes it so the owner of the file no longer has permissions to move it, delete it, or modify it, but they can still read it. Also need to make sure that this can…
JereTheJuggler
  • 127
  • 1
  • 2
  • 9
1
vote
1 answer

ReplayingDecoder throws exception on decode

I have created an decoder to process bytes that client sends. Here is it import java.util.List; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.ReplayingDecoder; public class…
user2686299
  • 427
  • 8
  • 25
1
vote
2 answers

Reading user file system returns AccessDeniedException

I have this method which is supposed to return an OberservableList, to be used in a JavaFX ListView, containing the names of the files in the currentUserPath (initialised as System.getProperty("user.home") by default). public ObservableList
Adam Lazarus
  • 134
  • 1
  • 8
1
vote
1 answer

Java MappedByteBuffer performance getting worse and worse when you change the map size continuously

Recently I had some tests about Java MappedByteBuffer. I found that if I continuously map the same file and read it, the time spend in reading getting longer and longer. But if I didn't change the map size, It would be faster than the I use the same…
Alexis
  • 1,080
  • 3
  • 21
  • 44
1
vote
1 answer

Create ZipFileSystem from a docx file obtained as a byte array without writing to a File

I want to modify a docx document - obtained as a byte array - by changing the word/document.xml using a java nio ZipFileSystem. I have the following code (based on SO discussion How to edit MS Word documents using Java?): public static void…
Caroh
  • 113
  • 8
1
vote
0 answers

How to resolve conflicts between PyNIO and SciPy

I did the following to install PyNIO in my Linux system: $ conda install libnetcdf netcdf4 hdf5 jpeg libgfortran gcc libgcc xray --name=PyNIO $ hash gcc $ which gcc ~/.conda/envs/PyNIO/bin/gcc $ hash gfortran $ which…
takachanbo
  • 633
  • 2
  • 6
  • 15
1
vote
1 answer

How can I send multiple http requests asynchronous with Netty?

I am trying to send asynchronously a large amount a http posts requests to one server. My goals is to compare each response to its orginal request. To do so I am following the Netty Snoop example. However, this example (and the other http examples)…
Mart
  • 56
  • 1
  • 5
1
vote
1 answer

How to read and write serialized object through SocketChannel in non-blocking mode?

I am currently attempting to read and write serialized Objects through a NIO SocketChannel. This SocketChannel is in non-blocking mode. I can't seem to find the right way to do it without corrupting the stream, corruping the stream header,…
1
vote
1 answer

What is the correct way of reading files in from the socket?

What is really the correct way of reading files from the socket? Because the loop on reading the file doesn't end even though on the client side writing the files has been finished. I even tried printing the buffer position and length if I still…
jnapor
  • 63
  • 1
  • 1
  • 9
1
vote
0 answers

How to rename a directory in a zip file?

I'm trying to rename a directory in a ZIP file using Java 7 NIO. The zip file is very simple: it contains a "TestFolder" directory, which contains a "test.txt" file. I want to rename TestFolder to NewFolder. Here is my code: public class Test { …
Ben
  • 6,321
  • 9
  • 40
  • 76
1
vote
1 answer

Are bytes on a TCP socket connection lost if the connection is severed before the bytes are read?

If I have a Selector that's accepting sockets and pushing them to a queue for other threads to handle, and no threads takes a socket for, say two minutes, and the client pushes data but then times out and disconnects. When a thread then takes one of…
mal
  • 3,022
  • 5
  • 32
  • 62
1
vote
1 answer

Java - Reading a txt file line by line

So I have this fairly simple code where I'm trying to read a file line by line: if(new File(filesDir).listFiles() != null){ for(File file : new File(filesDir).listFiles()){ try { Stream stream =…
Artemio Ramirez
  • 1,116
  • 1
  • 10
  • 23
1
vote
1 answer

File separator issues on windows/linux box

Below enum helps me avoid hard-coded directory values at different places in the JUNIT test cases. This seems to work in a development environment inside the IDE on a WINDOW's box. As soon as I put this code on a LINUX box, the JUNIT classes cannot…
TheMonkWhoSoldHisCode
  • 2,182
  • 3
  • 26
  • 40