java.io
 | What's in the java.io package?
 | I/O Stream classes to read and write Strings, byte/char arrays,
and other Java primitive data types |
 | Platform independent support of files, directories, and handles
to either open file or socket |
 | Serialization/Deserialization of Objects |
 | I/O related Exceptions |
|
 | Streams
|
 | InputStreams, OutputStreams, Readers and Writers
 | What's the difference between the Input/Output Streams and
Readers/Writers?
 | Input/Output Streams use bytes (8bits) as data (and its
limitations) |
 | Readers/Writers use chars (16bits) as data (which
supports Unicode) |
 | Thus, Readers and Writers are foreign-language-friendly
and is an important internationalization feature in Java
1.1 and later |
|
 | Use InputStreamReader and OutputStreamWriter to convert from
Streams to Reader/Writer
 | This is the preferred way for conversions - these two
classes handle the translation from bytes into Unicode
characters according to the particular platform and
locale dependent character encoding |
|
 | Common I/O classes
 | BufferedReader/Writer and Buffered[Input, Output]Stream
- buffer data to improve efficiency |
 | FileReader/Writer and File[Input, Output]Stream -
read/write to files |
 | Object[Input, Output]Stream - (de)serialize objects
from/to streams |
|
|
 | Files and FileDescriptors
 | FileDescriptors are representations of low-level file-handles
and sockets. You cannot "create" your own
FileDescriptor and have it make sense. Useful for socket
and low-level IO programming via JNI to communicate with C code. |
 | Files enable you to do a whole host of actions on the local file
system (those that security permit you to), including:
 | Creating, deleting, renaming files and creating
temporary (uniquely named) files |
 | Testing for file existence and reading its length |
 | Reading and setting file/directory attributes such as
readable, writable, hidden and modification time |
 | Getting the absolute, relative, and canonical (system
dependent) file path/name |
 | Listing and creating directories |
|
|
 | Serialization
 | An "automatic" way to save and restore Java Objects
and primitive data types |
 | Serialization saves fields that are non-transient, non-static,
and non-private
 | Objects are read with the readobject method and written
with the writeObject method |
 | Primitives are read with the methods of DataInput and
written with the methods of DataOutput |
//
// Serialize today's date to a file.
//
FileOutputStream fos = new FileOutputStream("C:\\DATA\\TEMP\\TODAY.OBJ");
ObjectOutput oo = new ObjectOutputStream(fos);
oo.writeObject("Today");
oo.writeObject(new Date());
oo.flush();
//
// Deserialize a string and date from a file.
//
FileInputStream fis = new FileInputStream("C:\\DATA\\TEMP\\TODAY.OBJ");
ObjectInputStream ois = new ObjectInputStream(fis);
String today = (String)ois.readObject();
Date date = (Date)ois.readObject();
|
 | Caveat: Only Objects that implement the Serializable and
Externalizable interfaces can be (de)serialized. |
 | Additional caveats include: Can't serialize inner classes |
 | Cool things Serialization enable: sending Java Objects perfectly
to remote machines via the network |
 | Full details at http://web2.java.sun.com/products/jdk/1.3/docs/guide/serialization/spec/serialTOC.doc.html |
|
java.net
java.util.jar / java.util.zip
 | Why use JAR files?
 | Security: digitally sign the contents of a JAR file. Users who
recognize your signature can then optionally grant your software
security privileges it wouldn't otherwise have. |
 | Decreased download time: If your applet is bundled in a JAR
file, the applet's class files and associated resources can be
downloaded to a browser in a single HTTP transaction without the
need for opening a new connection for each file. |
 | Compression: The JAR format allows you to compress your files
for efficient storage. |
 | Package Sealing: Packages stored in JAR files can be optionally
sealed so that the package can enforce version consistency.
Sealing a package within a JAR file means that all classes
defined in that package must be found in the same JAR file. |
 | Package Versioning: A JAR file can hold data about the files it
contains, such as vendor and version information. |
 | Portability: The mechanism for handling JAR files is a standard
part of the Java platform's core API. |
|
 | Common JAR Usage
Operation |
Command |
To create a JAR file |
jar cf jar-file input-file(s) |
To view the contents of a JAR file |
jar tf jar-file |
To extract the contents of a JAR file |
jar xf jar-file |
To extract specific files from a JAR file |
jar xf jar-file archived-file(s) |
To run an application packaged as a JAR file
(version 1.1) |
jre -cp app.jar MainClass
|
To run an application packaged as a JAR file
(version 1.2 -- requires Main-Class
manifest header) |
java -jar app.jar
|
To invoke an applet packaged as a JAR file |
<applet code=AppletClassName.class
archive="JarFileName.jar"
width=width height=height>
</applet>
|
|
 | Manifests
 | In many ways, the jar package is a specialized use of the zip
package. Thus, it only thinly wraps the zip package
 | A JAR file is basically a ZIP file with a manifest,
which gives JAR files a wide range of functionality |
|
 | Manifests allow textual descriptions and attributes to be
externally associated with class files |
 | Manifests can be used to allow
 | Electronic signing and version control |
 | Package sealing
Name: myCompany/myPackage/
Sealed: true
|
 | Download Extensions
Class-Path: servlet.jar infobus.jar acme/beans.jar
|
 | Applications bundled as JAR files
Main-Class: classname
|
|
|
java.security
 | Evolution of the Java Security Model
|
 | Java 1.2 Security Security Model in depth
 | New Policy/Permissions/Domain concept for security |
 | Cryptographic API
 | Cryptographic services |
 | Certificate management |
 | Key management |
|
 | Security related tools
 | keytool - create public/private key pairs,
import/display/export certificates, and generate X.509
v1 self-signed certificates and certificate requests
that can be sent to a certification authority |
 | jarsigner - signs and verifies the authenticity of
signatures on JAR files |
 | policytool - creates and modifies the policy
configuration files that define your installation's
security policy |
|
|
 | How to control Applets and Applications
 | Applets - by default, still restricted system access
 | SecurityManager runs by default |
 | Use policytool to edit the policy file to grant more
access |
|
 | Applications - by default, still unrestricted system access
 | Must run with SecurityManager in place
java -Djava.security.manager ApplicationClassName
|
 | Use policytool to edit the local policy file to control
application access |
|
|
Java VM and Bytecode
 | What is the Java VM?
 | The Java virtual machine is an abstract computing machine. Like
a real computing machine, it has an instruction set and
manipulates various memory areas at run-time |
 | The Java virtual machine knows nothing of the Java programming
language, only of a particular binary format, the class
file format. A class file contains Java virtual
machine instructions (or bytecodes) and a symbol table,
as well as other ancillary information |
 | For the sake of security, the Java virtual machine imposes
strong format and structural constraints on the code in a class
file. However, any language with functionality that can be
expressed in terms of a valid class file can be
hosted by the Java virtual machine |
|
 | Java VM Structure
 | To implement the Java virtual machine correctly, you need only
be able to read the class file format and correctly
perform the operations specified therein. Implementation details
that are not part of the Java virtual machine's specification
would unnecessarily constrain the creativity of implementors.
For example, the memory layout of run-time data areas, the
garbage-collection algorithm used, and any internal optimization
of the Java virtual machine instructions (for example,
translating them into machine code) are left to the discretion
of the implementor |
 | Two data types - primitives and references |
 | "Stack" programming language with random-access memory |
|
 | Java VM Instruction Set
 | Opcodes and mnemonics |
|
|