Table of 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
- 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
- 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
- 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
- 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
- 8 Collections 229
- 8.1 Utilities 230
- Rotating list elements 230
- Replacing list elements 232
- Finding sublists within lists 232
- Swapping list elements 233
- Converting enumerations to lists 233
- 8.2 LinkedHashMap and LinkedHashSet 235
- Using LinkedHashMap 235
- Using LinkedHashSet 238
- Efficiency of LinkedHashMap and LinkedHashSet 240
- Example: searching a file path 241
- 8.3 IdentityHashMap 246
- Object equality 246
- Hashing and equality 247
- Example: using the IdentityHashMap 247
- 8.4 The RandomAccess interface 252
- 8.5 Summary 255
- 9 Regular Expressions 257
- 9.1 Overview of regular expressions 258
- Literals 259
- The . wildcard 259
- Quantifiers: * and + 259
- Grouping with () 260
- Character classes 260
- Predefined character classes 261
- Sequencing and alternation 263
- Boundary matchers 263
- Reluctant (non-greedy) matching 264
- Other features 265
- 9.2 Pattern and Matcher 265
- Capturing groups 267
- Find and replace 268
- Flags 269
- 9.3 Transitioning from Perl to Java 270
- Finding the longest word in a line 270
- Parsing a tab-delimited file 273
- A command-line processor 276
- Parsing and modifying names 280
- 9.4 Example: HTML templating system 285
- 9.5 Example: a lexical analyzer 288
- 9.6 Summary 296
- 10 The Preferences API 297
- 10.1 What the Preferences API is for 298
- Simple Preferences API example 298
- Appropriate applications of the Preferences API 299
- Design goals of the Preferences API 301
- 10.2 Knowing when to use the Preferences API 304
- Comparison with java.util.Properties 304
- Comparison with JNDI 305
- 10.3 Understanding the data hierarchy 305
- Tree-like structure 305
- Key/value pairs 305
- System vs. user 306
- Definition of a user 306
- Pathnames 307
- Per-package subtrees 308
- 10.4 Using the API 308
- Traversing the data hierarchy 308
- Reading and writing values 311
- Allowable types 311
- Allowable keys 312
- Allowable values 312
- Allowable node names 313
- Default values 313
- Removing values 314
- Iterating through the values in a node 314
- Distinguishing between user and system nodes 314
- Node names and paths 315
- Getting parent and child nodes 316
- Determining the presence of nodes 316
- Removing nodes 317
- Flushing 318
- Syncing 318
- Example: storing GUI configuration 319
- 10.5 Change listeners 324
- Preference change listeners 325
- Node change listeners 325
- Example: listening for a GUI change request 326
- Example: changing server ports on the fly 329
10.6 Stored defaults 335
- 10.7 Importing and exporting 335
- 10.8 Summary 337
- 11 The Java Secure Socket Extension (JSSE) 339
- 11.1 Cryptographic terminology 340
- 11.2 SSL?the Secure Sockets Layer 342
- Components of the default implementation 342
- SSL handshaking 343
- 11.3 Managing keys 343
- Creating keys with keytool 344
- Store keys in a KeyStore 344
- Creating a KeyManagerFactory 344
- Creating a TrustManagerFactory 345
- Creating an SSLContext 345
- 11.4 Example: a trivial secure web server 346
- The authentication model 346
- Generating the key 347
- The configuration file 348
- The code 349
- 11.5 Example: a secure credit card authorization system 359
- The authentication model 359
- Generating the keys 360
- The code 362
- 11.6 Summary 370
index 371