JDK 1.4 Tutorial jdk.book Page i Monday, March 11, 2002 3:38 PM www.it-ebooks.info jdk.book Page ii Monday, March 11, 2002 3:38 PM www.it-ebooks.info JDK 1.4 Tutorial GREG M. TRAVIS MANNING Greenwich (74° w. long.) jdk.book Page iii Monday, March 11, 2002 3:38 PM www.it-ebooks.info For electronic information and ordering of this and other Manning books, go to www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact: Special Sales Department Manning Publications Co. 209 Bruce Park Avenue Fax: (203) 661-9018 Greenwich, CT 06830 email: orders@manning.com ©2002 by Manning Publications Co. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books they publish printed on acid-free paper, and we exert our best efforts to that end. Library of Congress Cataloging-in-Publication Data Travis, Greg Book Title /Bill J. Author. p. cm. Includes bibliographical references and index. ISBN ?-??????-??-? (alk. paper) 1. Java (Computer program language). 2. Title. ????????????? 1998 ?????????—???? ??-????? CIP Manning Publications Co. Copyeditor: Andy Carroll 209 Bruce Park Avenue Typesetter: Tony Roberts Greenwich, CT 06830 Cover designer: Leslie Haimes ISBN 1-930110-66-9 Printed in the United States of America 12345678910– VHG – 05 04 03 02 jdk.book Page iv Monday, March 11, 2002 3:38 PM www.it-ebooks.info To Susan jdk.book Page v Monday, March 11, 2002 3:38 PM www.it-ebooks.info jdk.book Page vi Monday, March 11, 2002 3:38 PM www.it-ebooks.info vii preface xv acknowledgments xvii about this book xix author online xxvii about the cover illustration xxix 1 Basic NIO (New Input/Output) 1 1.1 Doing I/O with channels and buffers 2 Getting a channel from a stream 3 ■ Creating a buffer revision 4 ■ Reading from a channel 4 ■ Writing to a channel 5 ■ Reading and writing together 6 1.2 Understanding buffers 7 Creating buffers 7 ■ get() and put() 7 ■ Buffer state values 9 flip() and clear() 10 ■ slice() and subbuffers 12 ■ Buffers of other types 13 ■ Reading and writing other types from a ByteBuffer 14 ■ Direct buffers 16 ■ Example: TCP/IP forwarding 17 ■ Doing I/O with channels and buffers 27 1.3 The File Locking facility 28 Types of locks 28 ■ Using locks 29 ■ Acquiring locks 30 Portability issues 31 ■ Example: a simple database 32 1.4 Summary 36 contents jdk.book Page vii Monday, March 11, 2002 3:38 PM www.it-ebooks.info viii CONTENTS 2 Advanced NIO (New Input/Output) 37 2.1 Reading and writing with MappedByteBuffers 38 Advantages of MappedByteBuffers 38 ■ Disadvantages of MappedByteBuffers 40 ■ Using MappedByteBuffers 40 Example: checksumming 41 2.2 Nonblocking I/O 42 The multithreaded approach 43 ■ The really bad single- threaded approach 44 ■ Polling 44 ■ Example: a polling chat server 46 ■ Multiplexing with select() 49 2.3 Encoding and decoding with Charsets 58 Decoding and encoding 59 ■ Finding available Charsets 59 Using encoders and decoders 61 2.4 Network interfaces 63 When to use a network interface 64 ■ Getting a list of NetworkInterfaces 64 ■ Reporting on NetworkInterfaces 64 Getting a list of InetAddresses 66 ■ Getting a NetworkInterface by InetAddress 66 ■ Getting a NetworkInterface by name 67 ■ Listening on a particular address 67 2.5 Summary 73 3 Java2D 75 3.1 The Print Service API 76 Print Service packages 76 ■ Document flavors 77 ■ Printer discovery 77 ■ Printer attributes 79 ■ The SimpleDoc class 80 The DocPrintJob interface 81 ■ Example: printing an image 81 ■ Example: a custom print dialog box 83 3.2 Reading and writing images with the Image I/O API 88 The plug-in model 89 ■ Simple reading 89 ■ Simple writing 90 ■ The ImageIO class 90 ■ Discovering available formats 90 ■ Example: reading and displaying an image 92 Example: writing an image 92 ■ The ImageReader class 93 The ImageWriter class 95 ■ Customizing the reading process 97 ■ Listeners 99 ■ Example: generating a graph 102 jdk.book Page viii Monday, March 11, 2002 3:38 PM www.it-ebooks.info CONTENTS ix 3.3 Summary 105 4 Java Web Start (JAWS) 107 4.1 Understanding the JAWS execution model 108 Client, server, and application 109 ■ The sandbox 110 Consider the possibilities 110 4.2 Building and deploying a JAWS application 111 JAR files 111 ■ The JNLP file 111 Configuring the web server 113 4.3 Using the sandbox: services 113 Using the sandbox: resources 114 4.4 Bypassing the sandbox 115 4.5 Example: a simple drawing program 117 PicoDraw.java 118 ■ DrawCanvas.java 131 TransferableImage.java 135 4.6 Summary 136 5 Logging 137 5.1 Logging overview 138 Log message format 139 ■ Logging levels 139 ■ Logger names and the logger hierarchy 140 ■ Logging methods 141 The LogRecord class 141 ■ Handlers 142 ■ Filters 143 Formatters 143 ■ Logging efficiency 144 The philosophy of logging 144 5.2 Configuring the Logging system 145 Configuring handlers 145 ■ Configuration values for standard handlers 146 ■ Configuring loggers 148 Global handlers 149 5.3 Using logging in a program 149 5.4 Writing a custom handler 155 5.5 Writing a custom formatter 165 5.6 Summary 168 jdk.book Page ix Monday, March 11, 2002 3:38 PM www.it-ebooks.info x CONTENTS 6 Assertion facility 171 6.1 Assertion basics 172 Why use assertions? 172 ■ Assertions vs. other error code 173 Designing by contract 174 6.2 Working with assertions 174 Assertion syntax 175 ■ Compiling with assertions 177 Controlling assertions from the command line 178 Controlling assertions programmatically 181 ■ Removing assertions completely 182 ■ Determining if assertions are enabled 183 ■ Catching an assertion failure 184 Assertions and class initialization 185 6.3 Assertion examples 187 Avoiding inconsistent states 187 ■ Narrowing the range of states 189 ■ Ensuring consistency between container objects and contained objects 189 ■ More complicated consistency checks 192 6.4 Knowing when to use assertions 193 Rules of use 193 ■ What to check for 197 Miscellaneous rules 202 6.5 Summary 204 7 Exceptions 205 7.1 Chained exceptions 206 7.2 StackTraceElements 208 What is a stack trace? 208 ■ Using StackTraceElements 210 Writing a custom stack trace dumper 210 Synthesizing a stack trace 215 7.3 Summary 228 jdk.book Page x Monday, March 11, 2002 3:38 PM www.it-ebooks.info [...]... (parameterized types) feature was, in the end, not included with JDK 1.4 as originally promised This controversial addition to the language looks like it will be included in JDK 1.5 for sure, and you can download an early-access version of it from Sun However, since it requires a change to the compiler, it can’t really be said to be a part of JDK 1.4 and so is not discussed in this book Who should read this... discussions will be accessible from the publisher’s web site as long as the book is in print xxvii www.it-ebooks.info jdk. book Page xxviii Monday, March 11, 2002 3:38 PM www.it-ebooks.info jdk. book Page xxix Monday, March 11, 2002 3:38 PM about the cover illustration The figure on the cover of JDK 1.4 Tutorial is a woman of late eighteenth century Armenia, attired in an ornate and beautiful dress While the details... long to be conveniently entered by hand; the book’s web site, at http://www.manning.com/travis/, has all of the code available for download www.it-ebooks.info jdk. book Page xxvii Monday, March 11, 2002 3:38 PM author online When you purchase JDK 1.4 Tutorial, you also get free access to a private web forum run by Manning Publications where you can make comments about the book, ask technical questions,... programmer who needs to use the new features of JDK 1.4 I’ve tried to include enough introductory material that beginners will also find this book useful; however, this book will not teach you to program—it assumes you already know how This book is intended to guide the reader through the essentials of most of the new packages, libraries, and features in the JDK 1.4 release of the Java programming language... pictures from this collection www.it-ebooks.info jdk. book Page 1 Monday, March 11, 2002 3:38 PM 1 Basic NIO (New Input/Output) This chapter covers I The New I/O system I Doing I/O with channels and buffers I File locking 1 www.it-ebooks.info jdk. book Page 2 Monday, March 11, 2002 3:38 PM 2 CHAPTER 1 Basic NIO The New I/O (NIO) API introduced in JDK 1.4 provides a completely new model of low-level I/O... assertions are crucial to maintaining software throughout its release cycle Chapter 7—Exceptions While we’re on the subject of errors, there are a couple of nice surprises in JDK 1.4 in the area of Exceptions www.it-ebooks.info jdk. book Page xxiv Monday, March 11, 2002 3:38 PM xxiv ABOUT THIS BOOK The new StackTraceElement object allows a program to access each stack frame of an exception’s stack trace,... love and patience (and even a bit of proofreading) throughout the writing of this book, and more thanks to Hume Beal for his purring and enthusiasm www.it-ebooks.info jdk. book Page xix Monday, March 11, 2002 3:38 PM about this book The JDK 1.4 release of the Java programming language from Sun Microsystems represents a substantial step in Java’s progress Some of the new features are packages that have... 345 I I I 11.4 Example: a trivial secure web server 346 The authentication model 346 Generating the key 347 The configuration file 348 The code 349 I I 11.5 Example: a secure credit card authorization system 359 The authentication model 359 Generating the keys 360 The code 362 I 11.6 index Summary 370 371 www.it-ebooks.info jdk. book Page xiv Monday, March 11, 2002 3:38 PM www.it-ebooks.info jdk. book... projects demand it JDK 1.4 is another step on this path It includes a variety of new features, some of which have been available in prototype form, but all of which are now firmly part of the Java platform These features are not yet available in most browsers, but if you use Java for true application development, you’ll want to learn how to use them right away www.it-ebooks.info jdk. book Page xvii... read data from, and write data to, channels Direct buffers provide direct access to system input and output buffers and thus offer the potential for transferring data from one channel to another with a minimum of data copying NIO makes it possible to circumvent the potential inefficiencies of Java’s stream I/O architecture, with the possibility of great gains in speed The channel paradigm is not as . 11 1 ■ The JNLP file 11 1 Configuring the web server 11 3 4. 3 Using the sandbox: services 11 3 Using the sandbox: resources 11 4 4 .4 Bypassing the sandbox 11 5 4. 5. 14 2 ■ Filters 14 3 Formatters 14 3 ■ Logging efficiency 14 4 The philosophy of logging 14 4 5.2 Configuring the Logging system 14 5 Configuring handlers 14 5 ■ Configuration