Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 254 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
254
Dung lượng
2,93 MB
Nội dung
[...]... so that 7 JavaNIO topics are presented in a sensible order and in easily absorbed chunks I've also tried to cross-reference heavily so it will be useful when accessed randomly Offers of lucrative consulting contracts, speaking engagments, and free stuff are appreciated Spurious flames and spam are cheerfully ignored You can contact me at ron@javanio.info or visit http://www.javanio.info/ O'Reilly and... updating process to make changes to the file without any reader processes seeing the file in an inconsistent state This is illustrated by Figures Figure 1-7 and Figure 1-8 19 JavaNIO Figure 1-7 Exclusive-lock request blocked by shared locks Figure 1-8 Shared-lock requests blocked by exclusive lock File locks are either advisory or mandatory Advisory locks provide information about current locks to those... this, use compact( ) Figure 2-6 shows a buffer from which we've drained some elements and that we now want to compact Figure 2-6 A partially drained buffer Doing this: buffer.compact( ) results in the buffer state shown in Figure 2-7 29 JavaNIO Figure 2-7 Buffer after compaction Several things have happened here You can see that data elements 2-5 were copied to locations 0-3 Locations 4 and 5 were unaffected... Figure 2-4 would look like Figure 2-5 26 JavaNIO Figure 2-5 Buffer after being flipped The rewind( ) method is similar to flip( ) but does not affect the limit It only sets the position back to 0 You can use rewind( ) to go back and reread the data in a buffer that has already been flipped What if you flip a buffer twice? It effectively becomes zero-sized Apply the same steps to the buffer of Figure 2-5 ;... operation supported by most operating systems that allows user processes to take maximum advantage of the page-oriented nature of system I/O and completely avoid buffer copies This is memory-mapped I/O, which is illustrated in Figure 1-6 Figure 1-6 User memory mapped to filesystem pages 18 JavaNIO Memory-mapped I/O uses the filesystem to establish a virtual memory mapping from user space directly to the applicable... abstractions of NIO Understanding these basic ideas should make it easy to recognize the I/O capabilities modeled by the new classes We're about to begin our Grand Tour of NIO The bus is warmed up and ready to roll Climb on board, settle in, get comfortable, and let's get this show on the road 21 JavaNIO Chapter 2 Buffers It's all relative —Big Al Einstein We begin our sightseeing tour of the java. nio packages... sightseeing tour of the java. nio packages with the Buffer classes These classes are the foundation upon which java. nio is built In this chapter, we'll take a close look at buffers, discover the various types, and learn how to use them We'll then see how the java. nio buffers relate to the channel classes of java. nio. channels A Buffer object is a container for a fixed amount of data It acts as a holding tank, or.. .Java NIO Font Conventions I use standard O'Reilly font conventions in this book This is not entirely by choice I composed the manuscript directly as XML using a pure Java GUI editor (XXE from http://www.xmlmind.com/), which enforced the DTD I used, O'Reilly' s subset of DocBook (http://www.oasis-open.org/) As such, I never specified fonts or type... any of the data elements of the buffer but simply sets the limit to the capacity and the position back to 0, as in Figure 2-2 This leaves the buffer ready to be filled again See Example 2-1 Example 2-1 Filling and draining buffers package com.ronsoft.books .nio. buffers; import java. nio. CharBuffer; /** * Buffer fill/drain example This code uses the simplest * means of filling and draining a buffer: one... applications in which high-performance I/O is essential The JDK 1.4 release contains many other significant improvements in addition to NIO As of 1.4, the Java platform has reached a high level of maturity, and there are few application areas remaining that Java cannot tackle A great guide to the full spectrum of JDK features in 1.4 is Java In A Nutshell, Fourth Edition by David Flanagan (O'Reilly) 1 There . Package java. nio. charset 242 C.5 Package java. nio. charset.spi 246 C.6 Package java. util.regex 246 Colophon 250 Java NIO 1 Dedication To my wife, Karen. What would I do without you? Java NIO. Appendix A. NIO and the JNI 215 Appendix B. Selectable Channels SPI 217 Appendix C. NIO Quick Reference 220 C.1 Package java. nio 220 C.2 Package java. nio. channels 227 C.3 Package java. nio. channels.spi. possible, the ugly-but-fast solution usually wins out over pretty-but-slow. Time is money, after all. Java NIO 3 JDK 1.4 is the first major Java release driven primarily by the Java Community