Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Non-blocking I/O (Java)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Channels=== Channels ([[Object (computer science)|classes]] implementing the interface {{Javadoc:SE|package=java.nio.channels|java/nio/channels|Channel}}) are designed to provide for bulk data transfers to and from NIO buffers. This is a low-level data transfer mechanism that exists in parallel with the classes of the higher-level I/O library (packages {{Javadoc:SE|package=java.io|java/io}} and {{Javadoc:SE|package=java.net|java/net}}). A channel implementation can be obtained from a high-level data transfer class such as {{Javadoc:SE|package=java.io|java/io|File}}, {{Javadoc:SE|package=java.net|java/net|ServerSocket}}, or {{Javadoc:SE|package=java.net|java/net|Socket}}, and vice versa. Channels are analogous to "[[file descriptor]]s" found in Unix-like operating systems. [[Computer file|File]] channels ({{Javadoc:SE|package=java.nio.channels|java/nio/channels|FileChannel}}) can use arbitrary buffers but can also establish a buffer directly mapped to file contents using [[memory-mapped file]]. They can also interact with [[file system]] locks. Similarly, [[Internet socket|socket]] channels ({{Javadoc:SE|package=java.nio.channels|java/nio/channels|SocketChannel}} and {{Javadoc:SE|package=java.nio.channels|java/nio/channels|ServerSocketChannel}}) allow for data transfer between sockets and NIO buffers. <code>FileChannel</code> can be used to do a file copy, which is potentially far more efficient than using old read/write with a byte array. The typical code for this is: <syntaxhighlight lang="java"> // Getting file channels try(FileChannel in = FileChannel.open(source, StandardOpenOption.READ); FileChannel out = FileChannel.open(target, StandardOpenOption.WRITE)) { // JavaVM does its best to do this as native I/O operations. in.transferTo(0, in.size(), out); } </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)