Questions tagged [nio2]

NIO2 is an umbrella name for new features added to the original Java NIO package in the Java 1.7 release. Also referred to as "New I/O".

NIO.2 is an umbrella name for new features added to the original Java package in the Java 1.7 release. Also referred to as "New I/O".

  • Asynchronous Channels and Futures: A Channel that supports asynchronous I/O operations with implementations for sockets, server sockets and files.
  • Completion Handlers: The ability to register handlers that will be called on the completion of an asynchronous I/O event.
  • Asynchronous Channel Groups: A grouping of asynchronous channels for the purpose of resource sharing.
  • Asynchronous Multicast: Support for implementing asynchronous NIO I/O with callbacks for UDP multicast.
  • A New File System API for Java: Defines new interfaces and classes for the Java virtual machine to access files, file attributes, and file systems.

Articles and Tutorials

Note: This tag and wiki should be rolled into the nio tag.

173 questions
3
votes
3 answers

Java 7 walkFileTree calling visitFile on directories

Say I have the following directory structure /root/dir /root/dir/file1.txt /root/dir/subdir /root/dir/subdir/file2.txt And let's say I'll be using the following visitor: class MyFileVisitor extends SimpleFileVisitor { @Override public…
Matthew Madson
  • 1,643
  • 13
  • 24
3
votes
1 answer

How to copy a directory with its attributes/permissions from one location to another?

I see a lot of examples that use Files.walkFileTree() to copy a directory and its contents from one location to another, but they fail to take the directory's file attributes and permissions into consideration. Meaning, they just invoke…
Gili
  • 86,244
  • 97
  • 390
  • 689
3
votes
1 answer

How can I prevent AccessDeniedException during Files.walkFileTree?

I'm trying to use Java 1.7 nio. When I call Files.walkFileTree(source[i], tc); where source[i] is any folder on my Windows file system and tc is a SimpleFileVisitor, I get an java.nio.file.AccessDeniedException. I've checked the folder and the…
James
  • 2,876
  • 18
  • 72
  • 116
3
votes
2 answers

Is Java's NIO2 API a replacement for writing a single-threaded multiplexed server with NIO?

I'm learning how to use NIO to write a mud (I have not ruled out higher-level libraries, but I would like to learn the low-level operations as well). In my reading I've learned about NIO2. Since I'm using Java 7 for this project already I'm…
georgek
  • 877
  • 6
  • 11
3
votes
4 answers

How to use AsynchronousSocketChannel#read in a loop or recursively?

I found a related question, but it wasn't particularly helpful as it didn't provide a full example. The problem: how to use AsynchronousSocketChannel for reading data of unknown length using a buffer of a fixed size First attempt (reads once): final…
Andrey
  • 8,882
  • 10
  • 58
  • 82
3
votes
0 answers

Counterintuinive AsynchronousSocketChannel behaviour

Look at the following test: public class AsynchronousSocketTest { static PrintStream out=System.out; public static void main(String[] args) throws IOException, InterruptedException, ExecutionException { int port=9008; InetSocketAddress…
Alexei Kaigorodov
  • 13,189
  • 1
  • 21
  • 38
3
votes
1 answer

How to know when the last file is being visited in FileVisitor?

I need to do something with the file visited last in a directory. How can I know if the current call to my visitFile() is the last one? (I only want to list all the files and directories in a given directory. To do so, I've introduced a depth…
Bloke
  • 2,229
  • 3
  • 22
  • 27
2
votes
1 answer

What is the actual difference between java Files.lines and Files.newBufferedReader().lines in Java?

Both two method will return stream of data, Is there any different between these two methods? If it's Which way is more suitable to read the large files?
2
votes
2 answers

Why is StandardOpenOption.DELETE_ON_CLOSE not deleting the source file of the FileChannel?

We have underneath method in Java which should delete the source file when its close method is called. private void appendFile(Path destination, Path source) {     try (FileChannel sourceChannel…
2
votes
0 answers

Unexpected behavior difference between Nio and Nio2 connectors in Tomcat 8.5

I've run across some peculiar behavior with the NIO2 connector in Tomcat 8.5.23 Here is how I am using it:
2
votes
1 answer

java async nio ReadPendingException

I have written an NIO.2 http client. When reading the response, it is possible that the full response hasn't been received so as soon as I get the content-length header on the byte buffer I know how much is left so I use that or read until the…
paulturnip
  • 106
  • 5
2
votes
3 answers

In Java on WIndows how do I detect if file has 'Read Only' attribute

In Windows a file may not be writable because the user simply doesn't have permission to modify the file due to Access Control Lists permissions, or just because the read only attribute is set for the file. My application is written in Java and…
Paul Taylor
  • 13,411
  • 42
  • 184
  • 351
2
votes
1 answer

Path.relativize behaviour when "dot directory" is included

About Path.relativize method you can read [...] This method attempts to construct a relative path that when resolved against this path, yields a path that locates the same file as the given path. For example, on UNIX, if this path is "/a/b"…
Luigi Cortese
  • 10,841
  • 6
  • 37
  • 48
2
votes
1 answer

creating a java.nio.file.FileSystem from a .zip file if it's encrypted?

There's a relatively new zip filesystem provider that's a supported part of the NIO2 library in JDK7 and above. I specifically need it to support the java.nio.file.FileSystem class. Is it compatible with encrypted .zip files and if so, how do you…
Jason S
  • 184,598
  • 164
  • 608
  • 970
2
votes
1 answer

AsynchronousServerSocketChannel with graceful shutdown

In my previous Question i asked how to implement a correct Multithreaded server. I got the response to program a "graceful shutdown", and i tried todo so. However, it didn't work. I still have open sockets in TIME_WAIT state on the client…
lhlmgr
  • 2,087
  • 1
  • 22
  • 38