وبلاگ بلیان

Java NIO

معرفی کتاب «Java NIO» نوشتهٔ Ron Hitchens، منتشرشده توسط نشر O'Reilly Media در سال 2002. این کتاب در 282 صفحه، فرمت pdf، زبان انگلیسی ارائه شده است. «Java NIO» در دستهٔ بدون دسته‌بندی قرار دارد.

Actually the small subtitle on the top of the front page "Regular Expressions and High-Performance I/O" would be the better title. "Java NIO" is incorrect. That suggest already that the mere 282 pages are not completely devoted to explaining usage of NIO package. The obvious conclusion is that this book is to terse to grant it the status of the sole source of information on NIO. But wait, there is more: The author feels the necessity to quote the Java package specifications, which we all have anyways on the net. This nonsensical waste of paper starts on the page 238, preceded by several pages of a similarly useless appendix B which quotes java.nio.channels.spi specifications. Thus the book has de facto approx. 230 pages. But wait, there is more! Already on the page 151 author abandons NIO and devotes himself to a talk about regular expression package, and... in his code examples uses boldly standard java.io. His examples are of a stunning banality and simplicity, to terse to be of any use beyond the regular Java instructional trail on the net. Regarding NIO, the 150 pages remaining, reader should rather take a look at "The JDK 1.4 Tutorial", 408 pages: The JDK 1.4 Tutorial. Travis'es chapter 1 and 2 clearly top this text in every aspect. Hitchens code examples seem very arbitrary, and often to verbose, dealing with, or constructing something not pertinent to IO itself. For example, pages 63/65, where author suggest to "wait" in a spinning while loop for write, without any sleep (hm..., is that really necessary?) and later on wastes an entire page on assembling some arbitrary text messages or sentences of a kind. Every reader will make many more such observations. Not a good call on O'Reilly's part... NIO is very complex indeed, and every practitioner has his/her own set of papers and snippets collected over the time, still waiting for "the ultimate" summary by someone. Last but not least, what annoys me in this book are the nonsensical and banal quotes of someone or something in the beginning of the chapters. This is a very nasty habit, copied over and over again by many authors. Only a few writers have the knowledge and ability to make such quotes, see Donald Knuth to contrast what you will find in this book. For example, "here, put this fish in you ear" by Ford Prefect. I have no clue what that means, and who or what Ford Prefect was. The said appendix with cut-and-paste of Java package specifications starts with a sentence by "U2", what ever that means. The equally useless appendix B starts with "If you build it, he will come" by... "An Iowa cornfield"! I have really no words to comment on this nonsense. Now let me pack the "NIO users manual" and ship it back to Amazon. Give me my money back! Table of Content......Page 2 Dedication......Page 3 Preface......Page 4 Organization......Page 5 Who Should Read This Book......Page 7 Conventions Used in This Book......Page 8 Font Conventions......Page 9 How to Contact Us......Page 10 Acknowledgments......Page 11 Table?1-1. Throughput rate, processing versus I/O time......Page 13 1.2 No Longer CPU Bound......Page 14 1.3 Getting to the Good Stuff......Page 15 1.4.1 Buffer Handling......Page 16 Figure 1-1. Simplified I/O buffer handling......Page 17 Figure 1-3. Multiply mapped memory space......Page 18 Figure 1-5. Physical memory as a paging-area cache......Page 19 1.4.4 File I/O......Page 20 Figure 1-6. User memory mapped to filesystem pages......Page 22 Figure 1-7. Exclusive-lock request blocked by shared locks......Page 23 1.4.5 Stream I/O......Page 24 1.5 Summary......Page 25 Figure 2-1. The Buffer family tree......Page 26 2.1.1 Attributes......Page 27 2.1.2 Buffer API......Page 28 2.1.3 Accessing......Page 29 Figure 2-3. Buffer after five put()s......Page 30 Figure 2-5. Buffer after being flipped......Page 31 2.1.6 Draining......Page 32 Example 2-1. Filling and draining buffers......Page 33 Figure 2-7. Buffer after compaction......Page 34 Figure 2-8. A buffer with a mark set......Page 35 2.1.9 Comparing......Page 36 Figure 2-11. Two buffers considered to be unequal......Page 37 2.1.10 Bulk Moves......Page 38 2.2 Creating Buffers......Page 41 2.3 Duplicating Buffers......Page 44 Figure 2-13. Creating a slice buffer......Page 45 2.4 Byte Buffers......Page 46 Figure 2-14. Big-endian byte order......Page 48 Figure 2-15. Little-endian byte order......Page 49 2.4.2 Direct Buffers......Page 51 2.4.3 View Buffers......Page 52 Figure 2-16. A CharBuffer view of a ByteBuffer......Page 53 Example 2-2. Creating a char view of a ByteBuffer......Page 54 2.4.4 Data Element Views......Page 55 Figure 2-17. A ByteBuffer containing some data......Page 56 Example 2-3. Utility routines for getting/putting unsigned values......Page 57 2.5 Summary......Page 59 Figure 3-1. Channels act as conduits to I/O services......Page 61 3.1 Channel Basics......Page 62 3.1.2 Using Channels......Page 64 Figure 3-3. The ByteChannel interfaces......Page 65 Example 3-1. Copying data between channels......Page 66 3.1.3 Closing Channels......Page 68 3.2 Scatter/Gather......Page 70 Figure 3-4. Scatter/gather interfaces......Page 71 Figure 3-6. A scattering read using four buffers......Page 72 Example 3-2. Collecting many buffers in a gathering write......Page 73 3.3 File Channels......Page 75 Figure 3-7. FileChannel family tree......Page 76 Table?3-1. File I/O API comparison chart......Page 78 Figure 3-8. A disk file with two holes......Page 82 3.3.2 File Locking......Page 83 Example 3-3. Shared- and exclusive-lock interaction......Page 86 3.4 Memory-Mapped Files......Page 89 Example 3-4. Composing HTTP replies with mapped files and gathering writes......Page 94 Example 3-5. Three types of memory-mapped buffers......Page 96 3.4.1 Channel-to-Channel Transfers......Page 99 Example 3-6. File concatenation using channel transfer......Page 100 3.5 Socket Channels......Page 101 Figure 3-9. The socket channel family tree......Page 102 3.5.1 Nonblocking Mode......Page 103 3.5.2 ServerSocketChannel......Page 105 Example 3-7. A nonblocking accept() with ServerSocketChannel......Page 106 3.5.3 SocketChannel......Page 107 Example 3-8. Concurrent-connection establishment......Page 109 3.5.4 DatagramChannel......Page 111 Example 3-9. Time-service client using DatagramChannel......Page 115 Example 3-10. DatagramChannel time server......Page 119 Figure 3-10. The Pipe family tree......Page 121 Figure 3-11. A pipe is a pair of looped channels......Page 123 Example 3-11. Worker thread writing to a pipe......Page 124 Table?3-2. Summary of java.nio.channels.Channels utility methods......Page 126 3.8 Summary......Page 127 4.1 Selector Basics......Page 129 4.1.1 The Selector, SelectableChannel, and SelectionKey Classes......Page 131 Figure 4-1. Selection class family tree......Page 132 Figure 4-2. Relationships of the selection classes......Page 134 4.1.2 Setting Up Selectors......Page 136 4.2 Using Selection Keys......Page 138 4.3.1 The Selection Process......Page 142 4.3.2 Stopping the Selection Process......Page 145 4.3.3 Managing Selection Keys......Page 146 Example 4-1. Using select() to service multiple channels......Page 147 4.3.4 Concurrency......Page 151 4.5 Selection Scaling......Page 152 Example 4-2. Servicing channels with a thread pool......Page 153 4.6 Summary......Page 158 5.1 Regular Expression Basics......Page 160 5.2.1 The CharSequence Interface......Page 163 Example 5-1. CharSequence interface examples......Page 164 5.2.2 The Pattern Class......Page 165 Table?5-1. Flag values affecting regular expression compilation......Page 167 Table?5-2. Matrix of split() behavior......Page 169 Example 5-2. Splitting strings with Pattern......Page 170 Example 5-3. Split matrix styelsheet......Page 172 5.2.3 The Matcher Class......Page 173 Example 5-4. Simple file grep......Page 174 Example 5-5. Extracting matched expressions......Page 175 Figure 5-2. start(), end(), and group() values......Page 177 Table?5-3. Regular expression capture groups of A((B)(C(D)))......Page 178 Example 5-6. Regular expression replacement......Page 180 Example 5-7. Backslashes in regular expressions......Page 182 Example 5-8. Regular expression append/replace......Page 184 5.3 Regular Expression Methods of the String Class......Page 185 5.4 Java Regular Expression Syntax......Page 186 Table?5-7. Java regular expression syntax quick reference......Page 187 Example 5-9. Object-oriented grep......Page 190 5.6 Summary......Page 196 6.1 Character Set Basics......Page 198 Figure 6-1. Characters encoded as UTF-8......Page 199 Table?6-1. Required charsets......Page 200 Example 6-1. Encoding with the standard charsets......Page 201 Figure 6-2. The charset classes......Page 205 6.2.2 Comparing Charsets......Page 208 6.2.3 Charset Encoders......Page 209 6.2.3.1 The CoderResult class......Page 215 6.2.4 Charset Decoders......Page 217 Example 6-2. Charset decoding......Page 219 Table?6-4. Legal characters for charset names......Page 222 6.3.1 Creating Custom Charsets......Page 223 6.3.2 Providing Your Custom Charsets......Page 226 Example 6-3. The custom Rot13 charset......Page 229 Example 6-4. Custom charset provider......Page 233 6.4 Summary......Page 235 Appendix A. NIO and the JNI......Page 237 Appendix B. Selectable Channels SPI......Page 240 C.1.1 Buffer......Page 243 C.1.4 ByteBuffer......Page 244 C.1.5 ByteOrder......Page 245 C.1.6 CharBuffer......Page 246 C.1.8 FloatBuffer......Page 247 C.1.9 IntBuffer......Page 248 C.1.11 LongBuffer......Page 249 C.1.14 ShortBuffer......Page 250 C.2.2 AsynchronousCloseException......Page 251 C.2.6 Channels......Page 252 C.2.8 ClosedChannelException......Page 253 C.2.11 DatagramChannel......Page 254 C.2.12 FileChannel......Page 255 C.2.14 FileLockInterruptionException......Page 256 C.2.17 IllegalSelectorException......Page 257 C.2.21 NonWritableChannelException......Page 258 C.2.24 OverlappingFileLockException......Page 259 C.2.27 ScatteringByteChannel......Page 260 C.2.29 SelectionKey......Page 261 C.2.31 ServerSocketChannel......Page 262 C.2.33 UnresolvedAddressException......Page 263 C.3.1 AbstractInterruptibleChannel......Page 264 C.3.4 AbstractSelector......Page 265 C.4.1 CharacterCodingException......Page 266 C.4.3 CharsetDecoder......Page 267 C.4.4 CharsetEncoder......Page 268 C.4.7 CodingErrorAction......Page 269 C.4.10 UnmappableCharacterException......Page 270 C.6.1 Matcher......Page 271 C.6.2 Pattern......Page 272 Colophon......Page 273 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 NIOBasic and advanced I/O ConceptsBinary I/O and the new buffer classesMemory mapped files and file lockingCharacter I/O: encoding, decoding and transforming character dataRegular Expressions and the new java.util.regex packageMuliplexing with java.nioBecause 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. The Java New I/O (NIO) packages in J2SE 1.4 introduce many new, indispensable features previously unavailable to Java programmers. These include APIs for high-performance I/O operations, regular expression processing, and character set coding. These new libraries are a treasure trove for Java developers. The NIO APIs are especially valuable where high-performance I/O is a requirement, but they can also be useful in a wide range of scenarios. The new APIs let you work directly with I/O buffers, multiplex nonblocking streams, do scattering reads and gathering writes, do channel-to-channel transfers, work with memory-mapped files, manage file locks, and much more. The new high-performance Regular Expression Library provides sophisticated, Perl-like regex-processing features such as pattern matching, search and replace, capture groups, look-ahead assertions, and many others. The Charset API gives you complete control over character set encoding and decoding, which are vital for properly managing the exchange of documents on the Web, for localization, or for other purposes. You can also create and install your own custom character sets.Staying current with the latest Java technology is never easy. NIO, new in Java 1.4, is quite possibly the most important new Java feature since Swing. Understanding it thoroughly is essential for any serious Java developer. NIO closes the gap between Java and natively compiled languages and enables Java applications to achieve maximum I/O performance by effectively leveraging operating-system services in a portable way. Java NIO is a comprehensive guide to the Java New I/O facilities. It lets you take full advantage of NIO features and shows you how they work, what they can do for you, and when you should use them. This book brings you up to speed on NIO and shows you how to bring your I/O-bound Java applications up to speed as well. Java NIO is an essential part of any Java professional's library. 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 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 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.
دانلود کتاب Java NIO