O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Java NIO

Book Description

Many serious Java programmers, especially enterprise Java programmers, consider the new I/O API--called NIO for New Input/Output--the most important feature in the 1.4 version of the Java 2 Standard Edition. The NIO package includes many things that have been missing from previous editions of Java that are critical to writing high-performance, large-scale applications: improvements in the areas of buffer management, scalable network and file I/O, character-set support, and regular expression matching. Most of all, it boosts performance and speed dramatically.Java NIO explores the new I/O capabilities of version 1.4 in detail and shows you how to put these features to work to greatly improve the efficiency of the Java code you write. This compact volume examines the typical challenges that Java programmers face with I/O and shows you how to take advantage of the capabilities of the new I/O features. You?ll learn how to put these tools to work using examples of common, real-world I/O problems and see how the new features have a direct impact on responsiveness, scalability, and reliability. The book includes:

  • A rundown of the new features in NIO
  • Basic and advanced I/O Concepts
  • Binary I/O and the new buffer classes
  • Memory mapped files and file locking
  • Character I/O: encoding, decoding and transforming character data
  • Regular Expressions and the new java.util.regex package
  • Muliplexing with java.nio
Because the NIO APIs supplement the I/O features of version 1.3, rather than replace them, you'll also learn when to use new APIs and when the older 1.3 I/O APIs are better suited to your particular application.Java NIO is for any Java programmer who is interested in learning how to boost I/O performance, but if you're developing applications where performance is critical, such as game computing or large-scale enterprise applications, you'll want to give this book a permanent spot on your bookshelf. With the NIO APIs, Java no longer takes a backseat to any language when it comes to performance. Java NIO will help you realize the benefits of these exciting new features.

Table of Contents

  1. Dedication
  2. Preface
    1. Organization
    2. Who Should Read This Book
    3. Software and Versions
    4. Conventions Used in This Book
      1. Font Conventions
    5. How to Contact Us
    6. Acknowledgments
  3. 1. Introduction
    1. 1.1. I/O Versus CPU Time
    2. 1.2. No Longer CPU Bound
    3. 1.3. Getting to the Good Stuff
    4. 1.4. I/O Concepts
      1. 1.4.1. Buffer Handling
        1. 1.4.1.1. Scatter/gather
      2. 1.4.2. Virtual Memory
      3. 1.4.3. Memory Paging
      4. 1.4.4. File I/O
        1. 1.4.4.1. Memory-mapped files
        2. 1.4.4.2. File locking
      5. 1.4.5. Stream I/O
    5. 1.5. Summary
  4. 2. Buffers
    1. 2.1. Buffer Basics
      1. 2.1.1. Attributes
      2. 2.1.2. Buffer API
      3. 2.1.3. Accessing
      4. 2.1.4. Filling
      5. 2.1.5. Flipping
      6. 2.1.6. Draining
      7. 2.1.7. Compacting
      8. 2.1.8. Marking
      9. 2.1.9. Comparing
      10. 2.1.10. Bulk Moves
    2. 2.2. Creating Buffers
    3. 2.3. Duplicating Buffers
    4. 2.4. Byte Buffers
      1. 2.4.1. Byte Ordering
      2. 2.4.2. Direct Buffers
      3. 2.4.3. View Buffers
      4. 2.4.4. Data Element Views
      5. 2.4.5. Accessing Unsigned Data
      6. 2.4.6. Memory-Mapped Buffers
    5. 2.5. Summary
  5. 3. Channels
    1. 3.1. Channel Basics
      1. 3.1.1. Opening Channels
      2. 3.1.2. Using Channels
      3. 3.1.3. Closing Channels
    2. 3.2. Scatter/Gather
    3. 3.3. File Channels
      1. 3.3.1. Accessing Files
      2. 3.3.2. File Locking
    4. 3.4. Memory-Mapped Files
      1. 3.4.1. Channel-to-Channel Transfers
    5. 3.5. Socket Channels
      1. 3.5.1. Nonblocking Mode
      2. 3.5.2. ServerSocketChannel
      3. 3.5.3. SocketChannel
      4. 3.5.4. DatagramChannel
    6. 3.6. Pipes
    7. 3.7. The Channels Utility Class
    8. 3.8. Summary
  6. 4. Selectors
    1. 4.1. Selector Basics
      1. 4.1.1. The Selector, SelectableChannel, and SelectionKey Classes
      2. 4.1.2. Setting Up Selectors
    2. 4.2. Using Selection Keys
    3. 4.3. Using Selectors
      1. 4.3.1. The Selection Process
      2. 4.3.2. Stopping the Selection Process
      3. 4.3.3. Managing Selection Keys
      4. 4.3.4. Concurrency
    4. 4.4. Asynchronous Closability
    5. 4.5. Selection Scaling
    6. 4.6. Summary
  7. 5. Regular Expressions
    1. 5.1. Regular Expression Basics
    2. 5.2. The Java Regular Expression API
      1. 5.2.1. The CharSequence Interface
      2. 5.2.2. The Pattern Class
        1. 5.2.2.1. Splitting strings with the Pattern class
      3. 5.2.3. The Matcher Class
    3. 5.3. Regular Expression Methods of the String Class
    4. 5.4. Java Regular Expression Syntax
    5. 5.5. An Object-Oriented File Grep
    6. 5.6. Summary
  8. 6. Character Sets
    1. 6.1. Character Set Basics
    2. 6.2. Charsets
      1. 6.2.1. The Charset Class
      2. 6.2.2. Comparing Charsets
      3. 6.2.3. Charset Encoders
        1. 6.2.3.1. The CoderResult class
      4. 6.2.4. Charset Decoders
    3. 6.3. The Charset Service Provider Interface
      1. 6.3.1. Creating Custom Charsets
      2. 6.3.2. Providing Your Custom Charsets
    4. 6.4. Summary
  9. A. NIO and the JNI
  10. B. Selectable Channels SPI
  11. C. NIO Quick Reference
    1. C.1. Package java.nio
      1. C.1.1. Buffer
      2. C.1.2. BufferOverflowException
      3. C.1.3. BufferUnderflowException
      4. C.1.4. ByteBuffer
      5. C.1.5. ByteOrder
      6. C.1.6. CharBuffer
      7. C.1.7. DoubleBuffer
      8. C.1.8. FloatBuffer
      9. C.1.9. IntBuffer
      10. C.1.10. InvalidMarkException
      11. C.1.11. LongBuffer
      12. C.1.12. MappedByteBuffer
      13. C.1.13. ReadOnlyBufferException
      14. C.1.14. ShortBuffer
    2. C.2. Package java.nio.channels
      1. C.2.1. AlreadyConnectedException
      2. C.2.2. AsynchronousCloseException
      3. C.2.3. ByteChannel
      4. C.2.4. CancelledKeyException
      5. C.2.5. Channel
      6. C.2.6. Channels
      7. C.2.7. ClosedByInterruptException
      8. C.2.8. ClosedChannelException
      9. C.2.9. ClosedSelectorException
      10. C.2.10. ConnectionPendingException
      11. C.2.11. DatagramChannel
      12. C.2.12. FileChannel
      13. C.2.13. FileLock
      14. C.2.14. FileLockInterruptionException
      15. C.2.15. GatheringByteChannel
      16. C.2.16. IllegalBlockingModeException
      17. C.2.17. IllegalSelectorException
      18. C.2.18. InterruptibleChannel
      19. C.2.19. NoConnectionPendingException
      20. C.2.20. NonReadableChannelException
      21. C.2.21. NonWritableChannelException
      22. C.2.22. NotYetBoundException
      23. C.2.23. NotYetConnectedException
      24. C.2.24. OverlappingFileLockException
      25. C.2.25. Pipe
      26. C.2.26. ReadableByteChannel
      27. C.2.27. ScatteringByteChannel
      28. C.2.28. SelectableChannel
      29. C.2.29. SelectionKey
      30. C.2.30. Selector
      31. C.2.31. ServerSocketChannel
      32. C.2.32. SocketChannel
      33. C.2.33. UnresolvedAddressException
      34. C.2.34. UnsupportedAddressTypeException
      35. C.2.35. WritableByteChannel
    3. C.3. Package java.nio.channels.spi
      1. C.3.1. AbstractInterruptibleChannel
      2. C.3.2. AbstractSelectableChannel
      3. C.3.3. AbstractSelectionKey
      4. C.3.4. AbstractSelector
      5. C.3.5. SelectorProvider
    4. C.4. Package java.nio.charset
      1. C.4.1. CharacterCodingException
      2. C.4.2. Charset
      3. C.4.3. CharsetDecoder
      4. C.4.4. CharsetEncoder
      5. C.4.5. CoderMalfunctionError
      6. C.4.6. CoderResult
      7. C.4.7. CodingErrorAction
      8. C.4.8. IllegalCharsetNameException
      9. C.4.9. MalformedInputException
      10. C.4.10. UnmappableCharacterException
      11. C.4.11. UnsupportedCharsetException
    5. C.5. Package java.nio.charset.spi
      1. C.5.1. CharsetProvider
    6. C.6. Package java.util.regex
      1. C.6.1. Matcher
      2. C.6.2. Pattern
      3. C.6.3. PatternSyntaxException
  12. Index
  13. Colophon
  14. Copyright