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
0
votes
2 answers

writing a file in Java without O_SYNC semantics

In C, when I call open() to open a file descriptor, I have to explicitly pass the O_SYNC flag to ensure that writes to this file will be persisted to disk by the time write() returns. If I want to, I can not supply O_SYNC to open(), and then my…
Dan
  • 7,155
  • 2
  • 29
  • 54
0
votes
0 answers

Receive message with NIO2

I'm currently developing an asynchronous server and client communication using nio2 asynchronous channels. How can I use it asynchronous. Do I need to open up a Thread for each connection? But this is not asynchronous?? public class JavaAsyncServer…
Steinliiippp
  • 373
  • 4
  • 20
0
votes
1 answer

ByteBuffer Performance

I have a socket which will be receiving more than 1000 TPS, so I need the reading to be as fast as possible, in this socket I read the info and I need to build and String and check if it matches a regex. I need to read char by char, so I can now…
zepol
  • 187
  • 5
  • 20
0
votes
1 answer

Reusing MemoryMappedByteBuffer

I read a file into a MappedByteBuffer: MappedByteBuffer buffer = FileChannel.open(file, StandardOpenOption.READ) .map(FileChannel.MapMode.READ_ONLY, 0, Files.size(file)) .load(); And write it to an…
Cheetah
  • 13,785
  • 31
  • 106
  • 190
0
votes
0 answers

How can I Asynchronously read from a file when new bytes are available in Java 8?

I am preparing a talk introducing NIO/NIO2 for our local Java Users Group and I am trying to work out how to accomplish a particular task. The basic concept is this... Open an AsynchronousFileChannel and somehow set up a callback so that whenever…
Deven Phillips
  • 1,129
  • 14
  • 39
0
votes
1 answer

Test if a file with a specific suffix exists using nio2

Given: a file named example.xml Aim: to test if the file example.xml.sha256 exists. What is the most elegant/efficient way to do this in Java 7+ (nio2, so using java.nio.files)? I have this, but it looks a little bit ugly for me: Path path =…
rmuller
  • 12,062
  • 4
  • 64
  • 92
0
votes
1 answer

Detect new content within a file, extract only the new things

it sounds like a stupid question and maybe I'm just blocked by whatever ... Using the new NIO2 of Java I can perfectly track the occurrance of new files, I can parse them and send the content so somewhere. But I have no glue how I can easily track…
Subcomandante
  • 403
  • 1
  • 6
  • 14
0
votes
2 answers

Cann't get file from classpath (using NIO2)

I want to create a String from the content of the file. According this answer I do it in this way: private static String buildStringFromTemplate(String stringTemplatePath) throws IOException { byte[] encoded =…
Sergey
  • 1,059
  • 3
  • 21
  • 35
0
votes
1 answer

Fastest and cleanest way to load a FileSystem entry into a memory-mapped file (NIO2)

I'd like to extract a zip/jar entry into memory so I can close the zip stream/FS and keep the file in the JVM without dealing with temporary copies. One option is to use Files.readAllLines(Path pathToZipFSentry), but it seems it used a buffered…
Whimusical
  • 6,401
  • 11
  • 62
  • 105
0
votes
2 answers

NIO's Filesystems and Paths inconsistent about default FileSystem

I am creating a FileSystem to browse the jar in case the access to my reosurces is frim within a jar. Then I noticed that when creating a new FileSystem, it actually registers as the default file system when using Paths NIO class. But…
Whimusical
  • 6,401
  • 11
  • 62
  • 105
0
votes
1 answer

AsynchronousFileChannel write failure

I am trying to write an image to a file using AsynchronousFileChannel.write: ... Path filePath = Paths.get(path + "/" + System.currentTimeMillis() + ".jpeg"); try (AsynchronousFileChannel channel =…
Bennyz
  • 623
  • 2
  • 18
  • 36
0
votes
1 answer

java nio SocketChannel.read does not return -1 to indicate end-of-stream

I am writing a code, which is using NIO/Selector to do web scraping. It works. I do get OP_CONNECT, then I send the GET request, and get the entire html page back. But, after that, I do not get a -1 to know it is finished. I do see , which means the…
Behzad Pirvali
  • 764
  • 3
  • 10
  • 28
0
votes
1 answer

Not getting OP_READ in my client

I am new to NIO and NIO2. I have been playing with an Echo server sample code, which works flawlessly. I started to write a similar client. My goal is to have multiple client socket connections running off the one main-thread. I do get the…
Behzad Pirvali
  • 764
  • 3
  • 10
  • 28
0
votes
0 answers

How to improve java nio2 net performance?

I read api of nio2, and find these ways to improve : AsynchronousSocketChannel.setOption(StandardSocketOptions.SO_RCVBUF, [size]); AsynchronousSocketChannel.setOption(StandardSocketOptions.SO_SNDBUF, [size]); [size] = RTT (from ping) * bandwidth…
skyrim
  • 21
  • 2
0
votes
1 answer

How get information about source of Windows Shortcut using NIO?

Please look at this code snippet: Path path = Paths.get("followLink.lnk"); System.out.println("path:" + path.toRealPath()); System.out.println("path:" + path.toRealPath(LinkOption.NOFOLLOW_LINKS)); file with name…
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710