Misplaced Pages

Non-blocking I/O (Java)

Article snapshot taken from Wikipedia with creative commons attribution-sharealike license. Give it a read and then ask your questions in the chat. We can research this topic together.

java.nio (NIO stands for New Input/Output) is a collection of Java programming language APIs that offer features for intensive I/O operations. It was introduced with the J2SE 1.4 release of Java by Sun Microsystems to complement an existing standard I/O. NIO was developed under the Java Community Process as JSR 51. An extension to NIO that offers a new file system API, called NIO.2, was released with Java SE 7 ("Dolphin").

#549450

24-498: The APIs of NIO were designed to provide access to the low-level I/O operations of modern operating systems. Although the APIs are themselves relatively high-level, the intent is to facilitate an implementation that can directly use the most efficient operations of the underlying platform. The Java NIO APIs are provided in the java.nio package and its subpackages. The documentation by Oracle identifies these features. NIO data transfer

48-481: A java.nio.file package which, with the Path class (also new to JDK 7), among other features, provides extended capabilities for filesystem tasks, e.g. can work with symbolic / hard links and dump big directory listings into buffers more quickly than the old File class does. The java.nio.file package and its related package, java.nio.file.attribute , provide comprehensive support for file I/O and for accessing

72-450: A character set is a mapping between Unicode characters (or a subset of them) and bytes. The java.nio.charset package of NIO provides facilities for identifying character sets and providing encoding and decoding algorithms for new mappings. It is unexpected that a Channel associated with a Java IO RandomAccess file closes the file descriptor on an interrupt, whereas RandomAccessFiles own read method does not do this. JDK 7 includes

96-794: A POSIX standard when formally approved by the ISO. POSIX.1-2001 (or IEEE Std 1003.1-2001) equates to the Single UNIX Specification, version 3 minus X/Open Curses . This standard consisted of: IEEE Std 1003.1-2004 involved a minor update of POSIX.1-2001. It incorporated two minor updates or errata referred to as Technical Corrigenda (TCs). Its contents are available on the web. Base Specifications, Issue 7 (or IEEE Std 1003.1-2008 , 2016 Edition). This standard consists of: IEEE Std 1003.1-2017 (Revision of IEEE Std 1003.1-2008) - IEEE Standard for Information Technology—Portable Operating System Interface (POSIX(R)) Base Specifications, Issue 7

120-414: A buffer directly mapped to file contents using memory-mapped file . They can also interact with file system locks. Similarly, socket channels ( java.nio.channels.SocketChannel and java.nio.channels.ServerSocketChannel ) allow for data transfer between sockets and NIO buffers. FileChannel can be used to do a file copy, which is potentially far more efficient than using old read/write with

144-482: A byte array. The typical code for this is: A selector ( java.nio.channels.Selector and subclasses) provides a mechanism for waiting on channels and recognizing when one or more become available for data transfer. When a number of channels are registered with the selector, it enables blocking of the program flow until at least one channel is ready for use, or until an interruption condition occurs. Although this multiplexing behavior could be implemented with threads,

168-578: Is a family of standards specified by the IEEE Computer Society for maintaining compatibility between operating systems . POSIX defines application programming interfaces (APIs), along with command line shells and utility interfaces, for software compatibility (portability) with variants of Unix and other operating systems. POSIX is also a trademark of the IEEE. POSIX is intended to be used by both application and system developers. Originally,

192-441: Is available from either The Open Group or IEEE. It is technically identical to POSIX.1-2008 with Technical Corrigenda 1 and 2 applied. Its contents are available on the web. IEEE Std 1003.1-2024 - IEEE Standard for Information Technology—Portable Operating System Interface (POSIX(R)) Base Specifications, Issue 8 was published on 14 June 2024. Its contents are available on the web. POSIX mandates 512-byte default block sizes for

216-454: Is based on buffers ( java.nio.Buffer and related classes). These classes represent a contiguous extent of memory, together with a small number of data transfer operations. Although theoretically these are general-purpose data structures, the implementation may select memory for alignment or paging characteristics, which are not otherwise accessible in Java. Typically, this would be used to allow

240-520: Is classified as object-based . If the language also provides polymorphism and inheritance it is classified as object-oriented . A language that supports creating an object from a class is classified as class-based . A language that supports object creation via a template object is classified as prototype-based . The concept of object is used in many different software contexts, including: POSIX The Portable Operating System Interface ( POSIX ; IPA : / ˈ p ɒ z . ɪ k s / )

264-558: Is divided into two parts: The development of the POSIX standard takes place in the Austin Group (a joint working group among the IEEE, The Open Group , and the ISO/IEC JTC 1/SC 22 /WG 15). Before 1997, POSIX comprised several standards: After 1997, the Austin Group developed the POSIX revisions. The specifications are known under the name Single UNIX Specification , before they become

SECTION 10

#1732870061550

288-485: The OpenOption and CopyOption interfaces, which emulates the effects of an extensible Enum type. A small down-side to this approach is that implementations cannot be inherited between various Enum types. Object (computer science) In software development , an object is an entity that has state , behavior , and identity . An object can model some part of reality or can be an invention of

312-553: The UNIX System V shell. Many user-level programs, services, and utilities (including awk , echo , ed ) were also standardized, along with required program-level services (including basic I/O : file , terminal , and network ). POSIX also defines a standard threading library API which is supported by most modern operating systems. In 2008, most parts of POSIX were combined into a single standard (IEEE Std 1003.1-2008 , also known as POSIX.1-2008). As of 2014 , POSIX documentation

336-459: The design process whose collaborations with other such objects serve as the mechanisms that provide some higher-level behavior. Put another way, an object represents an individual, identifiable item, unit, or entity, either real or abstract, with a well-defined role in the problem domain. A programming language can be classified based on its support for objects. A language that provides an encapsulation construct for state, behavior, and identity

360-463: The df and du utilities, reflecting the typical size of blocks on disks. When Richard Stallman and the GNU team were implementing POSIX for the GNU operating system , they objected to this on the grounds that most people think in terms of 1024 byte (or 1 KiB ) blocks. The environment variable POSIX_ME_HARDER was introduced to allow the user to force the standards-compliant behaviour. The variable name

384-696: The automated conformance tests and their certification has not expired and the operating system has not been discontinued. Some versions of the following operating systems had been certified to conform to one or more of the various POSIX standards. This means that they passed the automated conformance tests. The certification has expired and some of the operating systems have been discontinued. The following are not certified as POSIX compliant yet comply in large part: Mostly POSIX compliant environments for OS/2 : Partially POSIX compliant environments for DOS include: The following are not officially certified as POSIX compatible, but they conform in large part to

408-667: The buffer contents to occupy the same physical memory used by the underlying operating system for its native I/O operations, thus allowing the most direct transfer mechanism, and eliminating the need for any additional copying. In most operating systems, provided the particular area of memory has the right properties, transfer can take place without using the CPU at all. The NIO buffer is intentionally limited in features in order to support these goals. There are buffer classes for all of Java's primitive types except boolean , which can share memory with byte buffers and allow arbitrary interpretation of

432-509: The classes of the higher-level I/O library (packages java.io and java.net ). A channel implementation can be obtained from a high-level data transfer class such as java.io.File , java.net.ServerSocket , or java.net.Socket , and vice versa. Channels are analogous to " file descriptors " found in Unix-like operating systems. File channels ( java.nio.channels.FileChannel ) can use arbitrary buffers but can also establish

456-458: The file system. A zip file system provider is also available in JDK 7. The java.nio.file.LinkOption is an example of emulating extensible enums with interfaces. In Java, it is not possible to have one Enum extend another Enum . However, it is possible to emulate an extensible Enum type by having an Enum implement one or more interfaces. LinkOption is an enum type that implements both

480-558: The name "POSIX" referred to IEEE Std 1003.1-1988, released in 1988. The family of POSIX standards is formally designated as IEEE 1003 and the ISO/IEC standard number is ISO / IEC 9945. The standards emerged from a project that began in 1984 building on work from related activity in the /usr/group association. Richard Stallman suggested the name POSIX to the IEEE instead of the former IEEE-IX . The committee found it more easily pronounceable and memorable, and thus adopted it. Unix

504-410: The selector can provide a significantly more efficient implementation using lower-level operating system constructs. A POSIX -compliant operating system , for example, would have direct representations of these concepts, select() . A notable application of this design would be the common paradigm in server software which involves simultaneously waiting for responses on a number of sessions. In Java,

SECTION 20

#1732870061550

528-434: The underlying bytes. NIO buffers maintain several pointers that dictate the function of their accessor methods. The NIO buffer implementation contains a rich set of methods for modifying these pointers: Channels ( classes implementing the interface 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

552-407: Was later changed to POSIXLY_CORRECT . This variable is now also used for a number of other behaviour quirks. Depending upon the degree of compliance with the standards, one can classify operating systems as fully or partly POSIX compatible. Current versions of the following operating systems have been certified to conform to one or more of the various POSIX standards. This means that they passed

576-499: Was selected as the basis for a standard system interface partly because it was "manufacturer-neutral". However, several major versions of Unix existed—so there was a need to develop a common-denominator system. The POSIX specifications for Unix-like operating systems originally consisted of a single document for the core programming interface , but eventually grew to 19 separate documents (POSIX.1, POSIX.2, etc.). The standardized user command line and scripting interface were based on

#549450