Table Of Contentjdk.book Page i Monday, March 11, 2002 3:38 PM
JDK 1.4 Tutorial
jdk.book Page ii Monday, March 11, 2002 3:38 PM
jdk.book Page iii Monday, March 11, 2002 3:38 PM
JDK 1.4 Tutorial
GREG M. TRAVIS
MANNING
Greenwich
(74° w. long.)
jdk.book Page iv Monday, March 11, 2002 3:38 PM
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: [email protected]
©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
1 2 3 4 5 6 7 8 9 10 – VHG – 05 04 03 02
jdk.book Page v Monday, March 11, 2002 3:38 PM
To Susan
jdk.book Page vi Monday, March 11, 2002 3:38 PM
jdk.book Page vii Monday, March 11, 2002 3:38 PM
contents
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 (cid:1) Creating a buffer
revision 4 (cid:1) Reading from a channel 4 (cid:1) Writing to a
channel 5 (cid:1) Reading and writing together 6
1.2 Understanding buffers 7
Creating buffers 7 (cid:1) get() and put() 7 (cid:1) Buffer state values 9
flip() and clear() 10 (cid:1) slice() and subbuffers 12 (cid:1) Buffers of
other types 13 (cid:1) Reading and writing other types from a
ByteBuffer 14 (cid:1) Direct buffers 16 (cid:1) Example: TCP/IP
forwarding 17 (cid:1) Doing I/O with channels and buffers 27
1.3 The File Locking facility 28
Types of locks 28 (cid:1) Using locks 29 (cid:1) Acquiring locks 30
Portability issues 31 (cid:1) Example: a simple database 32
1.4 Summary 36
vii
jdk.book Page viii Monday, March 11, 2002 3:38 PM
viii CONTENTS
2 Advanced NIO (New Input/Output) 37
2.1 Reading and writing with MappedByteBuffers 38
Advantages of MappedByteBuffers 38 (cid:1) Disadvantages of
MappedByteBuffers 40 (cid:1) Using MappedByteBuffers 40
Example: checksumming 41
2.2 Nonblocking I/O 42
The multithreaded approach 43 (cid:1) The really bad single-
threaded approach 44 (cid:1) Polling 44 (cid:1) Example: a polling chat
server 46 (cid:1) Multiplexing with select() 49
2.3 Encoding and decoding with Charsets 58
Decoding and encoding 59 (cid:1) Finding available Charsets 59
Using encoders and decoders 61
2.4 Network interfaces 63
When to use a network interface 64 (cid:1) Getting a list of
NetworkInterfaces 64 (cid:1) Reporting on NetworkInterfaces 64
Getting a list of InetAddresses 66 (cid:1) Getting a
NetworkInterface by InetAddress 66 (cid:1) Getting a
NetworkInterface by name 67 (cid:1) Listening on a
particular address 67
2.5 Summary 73
3 Java2D 75
3.1 The Print Service API 76
Print Service packages 76 (cid:1) Document flavors 77 (cid:1) Printer
discovery 77 (cid:1) Printer attributes 79 (cid:1) The SimpleDoc class 80
The DocPrintJob interface 81 (cid:1) Example: printing an
image 81 (cid:1) 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 (cid:1) Simple reading 89 (cid:1) Simple
writing 90 (cid:1) The ImageIO class 90 (cid:1) Discovering available
formats 90 (cid:1) Example: reading and displaying an image 92
Example: writing an image 92 (cid:1) The ImageReader class 93
The ImageWriter class 95 (cid:1) Customizing the reading
process 97 (cid:1) Listeners 99 (cid:1) Example: generating a graph 102
jdk.book Page ix Monday, March 11, 2002 3:38 PM
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 (cid:1) The sandbox 110
Consider the possibilities 110
4.2 Building and deploying a JAWS application 111
JAR files 111 (cid:1) 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 (cid:1) DrawCanvas.java 131
TransferableImage.java 135
4.6 Summary 136
5 Logging 137
5.1 Logging overview 138
Log message format 139 (cid:1) Logging levels 139 (cid:1) Logger
names and the logger hierarchy 140 (cid:1) Logging methods 141
The LogRecord class 141 (cid:1) Handlers 142 (cid:1) Filters 143
Formatters 143 (cid:1) Logging efficiency 144
The philosophy of logging 144
5.2 Configuring the Logging system 145
Configuring handlers 145 (cid:1) Configuration values for
standard handlers 146 (cid:1) 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 x Monday, March 11, 2002 3:38 PM
x CONTENTS
6 Assertion facility 171
6.1 Assertion basics 172
Why use assertions? 172 (cid:1) Assertions vs. other error code 173
Designing by contract 174
6.2 Working with assertions 174
Assertion syntax 175 (cid:1) Compiling with assertions 177
Controlling assertions from the command line 178
Controlling assertions programmatically 181 (cid:1) Removing
assertions completely 182 (cid:1) Determining if assertions are
enabled 183 (cid:1) Catching an assertion failure 184
Assertions and class initialization 185
6.3 Assertion examples 187
Avoiding inconsistent states 187 (cid:1) Narrowing the range
of states 189 (cid:1) Ensuring consistency between container
objects and contained objects 189 (cid:1) More complicated
consistency checks 192
6.4 Knowing when to use assertions 193
Rules of use 193 (cid:1) 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 (cid:1) Using StackTraceElements 210
Writing a custom stack trace dumper 210
Synthesizing a stack trace 215
7.3 Summary 228