In addition, some abstract classes such as FilterInputStream are also omitted so that derived classes in CLDC are reparented directly to InputStream or OutputStream.. The java.io hierarc
Trang 1Type Of
This section lists all of the fields and constants that are of this type, which can
help you figure out how to obtain an object of this type
II.3.6 A Note About Class Names
Throughout the quick reference, you'll notice that classes are sometimes referred to by class name alone and at other times referred to by class name
and package name If package names were always used, the class synopses would become long and hard to read On the other hand, if package names were never used, it would sometimes be difficult to know what class was being
referred to The rules for including or omitting the package name are complex
They can be summarized approximately as follows, however:
• If the class name alone is ambiguous, the package name is always used
• If the class is part of the java.lang package or is a very commonly
used class, the package name is omitted
• If the class being referred to is part of the current package (and has a
quick-reference entry in the current chapter), the package name is omitted
Trang 2Chapter 10 J2ME Packages and Classes
There are two types of packages and classes in the J2ME platform:
• Those that are inherited from the J2SE platform
• Those that are specific to J2ME and have no counterpart in J2SE
Most of J2ME falls into the first category Depending on the configuration and profile you are using, you can use a different subset of J2SE packages and a subset of those packages that are part of J2ME itself, as shown in Table 10-1 Some J2SE packages, particularly those associated with the user interface, are not currently part of any J2ME profile These classes are listed in Table 10-2
Even when a package is available, it is not necessarily the case that all of its classes are included in the J2ME implementation The remaining sections of this chapter list all classes in the J2SE packages included in at least one J2ME profile and show, for each configuration and profile, the classes applicable to it.1 In some cases, you'll see that a J2SE class is not available
at all in J2ME Classes are usually omitted because of resource limitations, but there has also been an effort to remove classes that are deprecated in Java 2 Version 1.3; where this is the case, it is indicated in the tables
Table 10-1 J2ME Package List
Package CLDC 1.0 MID Profile 1.0 CDC 1.0 Foundation Profile 1.0 RMI Profile 1.0
Trang 310.2 J2SE Packages Not Present in J2ME
The J2SE packages listed in the following table are not included in any J2ME configuration or profile Since all J2ME configurations and profiles are based on Java 2 Version 1.3, this list does not include classes available in later J2SE platform releases
Table 10-2 J2SE Packages Not Included in J2ME
Trang 410.3 J2ME Package Contents
This section contains tables that indicate which classes are available in each of the J2ME packages, itemized by configuration or profile The presence of a symbol in a cell indicates that the class corresponding to its row is available in the configuration or profile corresponding to its column
10.3.1 The java.io Package
Table 10-3 Classes in the J2ME java.io Package
Class CLDC 1.0 MID Profile 1.0 CDC 1.0 Foundation Profile 1.0 RMI Profile 1.0
Trang 510.3.2 The java.lang Package
Table 10-4 Classes in the J2ME java.lang Package
Class CLDC 1.0 MID Profile 1.0 CDC 1.0 Foundation Profile 1.0 RMI Profile 1.0
AbstractMethodError
ArithmeticException
Trang 710.3.3 The java.lang.ref Package
Table 10-5 Classes in the J2ME java.lang.ref Package
Class CLDC 1.0 MID Profile 1.0 CDC 1.0 Foundation Profile 1.0 RMI Profile 1.0
10.3.4 The java.lang.reflect Package
Table 10-6 Classes in the J2ME java.lang.reflect Package
Class CLDC 1.0 MID Profile 1.0 CDC 1.0 Foundation Profile 1.0 RMI Profile 1.0
AccessibleObject
Array
Constructor
Trang 810.3.5 The java.math Package
Table 10-7 Classes in the J2ME java.math Package
Class CLDC 1.0 MID Profile 1.0 CDC 1.0 Foundation Profile 1.0 RMI Profile 1.0
BigDecimal
BigInteger
10.3.6 The java.net Package
Table 10-8 Classes in the J2ME java.net Package
Class CLDC 1.0 MID Profile 1.0 CDC 1.0 Foundation Profile 1.0 RMI Profile 1.0
Trang 910.3.7 The java.rmi Package
Table 10-9 Classes in the J2ME java.rmi Package
Class CLDC 1.0 MID Profile 1.0 CDC 1.0 Foundation Profile 1.0 RMI Profile 1.0
10.3.8 The java.rmi.activation Package
Table 10-10 Classes in the J2ME java.rmi.activation Package
Class CLDC 1.0 MID Profile 1.0 CDC 1.0 Foundation Profile 1.0 RMI Profile 1.0
Trang 10The CDC RMI profile contains only a small subset of the J2SE java.rmi.activation
package, because it provides only the client-side functionality The server side is assumed to
be hosted in a J2SE or J2EE environment
10.3.9 The java.rmi.dgc Package
Table 10-11 Classes in the J2ME java.rmi.dgc Package
Class CLDC 1.0 MID Profile 1.0 CDC 1.0 Foundation Profile 1.0 RMI Profile 1.0
DGC
Lease
VMID
10.3.10 The java.rmi.registry Package
Table 10-12 Classes in the J2ME java.rmi.registry Package
Class CLDC 1.0 MID Profile 1.0 CDC 1.0 Foundation Profile 1.0 RMI Profile 1.0
LocateRegistry
Registry
RegistryHandler Deprecated
10.3.11 The java.rmi.server Package
Table 10-13 Classes in the java.rmi.server Package
Class CLDC 1.0 MID Profile 1.0 CDC 1.0 Foundation Profile 1.0 RMI Profile 1.0
Trang 1110.3.12 The java.security Package
Table 10-14 Classes in the J2ME java.security Package
Class CLDC 1.0 MID Profile 1.0 CDC 1.0 Foundation Profile 1.0 RMI Profile 1.0
Trang 1210.3.13 The java.security.acl Package
Table 10-15 Classes in the J2ME java.security.acl Package
Class CLDC 1.0 MID Profile 1.0 CDC 1.0 Foundation Profile 1.0 RMI Profile 1.0
Trang 1310.3.14 The java.security.cert Package
Table 10-16 Classes in the J2ME java.security.cert Package
Class CLDC 1.0 MID Profile 1.0 CDC 1.0 Foundation Profile 1.0 RMI Profile 1.0
10.3.15 The java.security.interfaces Package
Table 10-17 Classes in tn the J2ME java.security.interfaces Package
Class CLDC 1.0 MID Profile 1.0 CDC 1.0 Foundation Profile 1.0 RMI Profile 1.0
10.3.16 The java.security.spec Package
Table 10-18 Classes in the J2ME java.security.spec Package
Class CLDC 1.0 MID Profile 1.0 CDC 1.0 Foundation Profile 1.0 RMI Profile 1.0
Trang 1410.3.17 The java.text Package
Table 10-19 Classes in the J2ME java.text Package
MID Profile 1.0
CDC 1.0 Foundation Profile 1.0
RMI Profile 1.0
10.3.18 The java.util Package
Table 10-20 Classes in the J2ME java.util Package
Class CLDC 1.0 MID Profile 1.0 CDC 1.0 Foundation Profile 1.0 RMI Profile 1.0
Trang 1610.3.19 The java.util.jar Package
Table 10-21 Classes in the J2ME java.util.jar Package
Class CLDC 1.0 MID Profile 1.0 CDC 1.0 Foundation Profile 1.0 RMI Profile 1.0
10.3.20 The java.util.zip Package
Table 10-22 Classes in the J2ME java.util.zip Package
Class CLDC 1.0 MID Profile 1.0 CDC 1.0 Foundation Profile 1.0 RMI Profile 1.0
10.3.21 The javax.microedition.io Package
Table 10-23 Classes in the J2ME javax.microedition.io Package
Class CLDC 1.0 MID Profile 1.0 CDC 1.0 Foundation Profile 1.0 RMI Profile 1.0
Trang 17OutputConnection
StreamConnection
StreamConnectionNotifier
10.3.22 The javax.microedition.lcdui Package
Table 10-24 Classes in the J2ME javax.microedition.lcdui Package
Class CLDC 1.0 MID Profile 1.0 CDC 1.0 Foundation Profile 1.0 RMI Profile 1.0
10.3.23 The javax.microedition.midlet Package
Table 10-25 Classes in the J2ME javax.microedition.midlet Package
Class CLDC 1.0 MID Profile 1.0 CDC 1.0 Foundation Profile 1.0 RMI Profile 1.0
MIDlet
MIDletStateChangeException
10.3.24 The javax.microedition.rms Package
Table 10-26 Classes in the javax.microedition.rms Package
Class CLDC 1.0 MID Profile 1.0 CDC 1.0 Foundation Profile 1.0 RMI Profile 1.0
InvalidRecordIDException
RecordComparator
Trang 19Chapter 11 java.io
Package java.io CLDC 1.0, MIDP 1.0
This package, whose class hierarchy is shown in Figure 11-1, contains interfaces and classes used to access input and output sources and treat them either as a sequence of 8-bit bytes or 16-bit characters
The CLDC version of this package contains only the basic classes and interfaces from its J2SE counterpart that provide 8-bit input and output, together with support for Unicode conversion using the InputStreamReader and OutputStreamWriter classes Most of the concrete input and output stream and reader/writer classes available in J2SE are not provided
In addition, some abstract classes (such as FilterInputStream) are also omitted so that derived classes in CLDC are reparented directly to InputStream or OutputStream
ByteArrayInputStream CLDC 1.0, MIDP 1.0
java.io
This is a subclass of InputStream that returns data from an array of bytes passed into one of its constructors It is useful when you have data stored in a byte array and wish to read it as if
it were coming from a file, pipe, or socket A good example of this is provided by MIDlet
RecordStores, which hold their content in byte arrays MIDlets typically read these records
by creating a ByteArrayInputStream from the content of a record and then wrap it with a
DataInputStream to recover strings and Java data types without assuming the format used to store them
This class supports the mark() and reset() operations of InputStream Once constructed, the byte array from which this stream gets its data cannot be switched However, because the data is not copied by the stream, the contents of the array can be modified and then re-read following a reset() Note that such an approach is only practical if the original byte array is large enough to hold any desired modifications
Trang 20Figure 11-1 The java.io hierarchy
public class ByteArrayInputStream extends InputStream {
// Public Constructors
public ByteArrayInputStream( byte[] buf);
public ByteArrayInputStream(byte[] buf, int offset,
int length);
// Public Methods Overriding InputStream
public int available(); // synchronized
public void close() throws IOException; // synchronized empty public void mark( int readAheadLimit);
public boolean markSupported(); // constant
public int read(); // synchronized
public int read( byte[] b, int off, int len); // synchronized
public void reset(); // synchronized
public long skip( long n); // synchronized
// Protected Instance Fields
protected byte[] buf;
protected int count;
protected int mark;
protected int pos;
}
Trang 21ByteArrayOutputStream CLDC 1.0, MIDP 1.0
java.io
This subclass of OutputStream stores data written to it in a buffer which can later be retrieved using its toByteArray() method When the stream is created, the buffer is empty It
is then expanded as data is written to the stream
ByteArrayOutputStream is often used in J2ME in conjunction with a DataOutputStream to allow strings and Java primitives types to be stored in a byte array This byte array is written
to persistent storage on a device using the RecordStore APIs in the
javax.microedition.rms package
public class ByteArrayOutputStream extends OutputStream {
// Public Constructors
public ByteArrayOutputStream();
public ByteArrayOutputStream( int size);
// Public Instance Methods
public void reset(); // synchronized
public int size();
public byte[] toByteArray(); // synchronized
// Public Methods Overriding OutputStream
public void close() throws IOException; // synchronized public void write( int b); // synchronized public void write( byte[] b, int off, int len); // synchronized
// Public Methods Overriding Object
public String toString();
// Protected Instance Fields
protected byte[] buf;
protected int count;
The class provides methods for reading primitives of type boolean, byte, char, int, long
and short There is also provision for reading an unsigned short, which is a 16-bit value that
is returned in a Java int without sign extension
Strings are held in a slightly modified form of UTF-8, which results in Unicode characters with values in the range 0-127 inclusive being encoded as a single byte This is an
Trang 22optimization for the most common characters in Western locales The readUTF() returns a
String from a sequence of bytes held in this form
The readFully() methods read a stream of bytes into a buffer, blocking until either a specified number of bytes has been read or an end-of-file is reached This is a convenience method that repeatedly reads from the underlying input stream until the end condition is met;
it removes the need for application code to include this loop
public interface DataInput {
// Public Instance Methods
public abstract boolean readBoolean() throws IOException;
public abstract byte readByte() throws IOException;
public abstract char readChar() throws IOException;
public abstract void readFully(
byte[] b) throws IOException;
public abstract void readFully(byte[] b, int off,
int len) throws IOException;
public abstract int readInt() throws IOException;
public abstract long readLong() throws IOException;
public abstract short readShort() throws IOException;
public abstract int readUnsignedByte() throws IOException;
public abstract int readUnsignedShort(
) throws IOException;
public abstract String readUTF() throws IOException;
public abstract int skipBytes( int
openDataInputStream method of javax.microedition.io.Connector In this case, the
openDataInputStream method returns a stream to read data from a network connection or some other type of data source accessed using the CLDC Generic Connection Framework Note that the CLDC version of this class is derived from InputStream and not
FilterInputStream as in J2SE This is because CLDC does not have a FilterInputStream
class
Trang 23public class DataInputStream extends InputStream implements DataInput {
// Public Constructors
public DataInputStream( InputStream in);
// Public Class Methods
public static final String readUTF(
DataInput in) throws IOException;
// Methods Implementing DataInput
public final boolean readBoolean() throws IOException;
public final byte readByte() throws IOException;
public final char readChar() throws IOException;
public final void readFully(
byte[] b) throws IOException;
public final void readFully(byte[] b, int off,
int len) throws IOException;
public final int readInt() throws IOException;
public final long readLong() throws IOException;
public final short readShort() throws IOException;
public final int readUnsignedByte() throws IOException;
public final int readUnsignedShort() throws IOException;
public final String readUTF() throws IOException;
public final int skipBytes( int n) throws IOException;
// Public Methods Overriding InputStream
public int available() throws IOException;
public void close() throws IOException;
public void mark( int readlimit); //
synchronized
public boolean markSupported();
public int read() throws IOException;
public final int read(
byte[] b) throws IOException;
public final int read(byte[] b, int off,
int len) throws IOException;
public void reset() throws IOException; //
synchronized
public long skip( long n) throws IOException;
// Protected Instance Fields
protected InputStream in;
This interface is implemented by classes that write strings and Java primitive types to
a platform-independent binary encoding that can be read by a class implementing the DataInput interface
Trang 24Methods are provided for writing a single byte or an array of bytes as well as primitives of type boolean, char, int, long and short
The writeChars() method writes the content of a String as an array of 16-bit characters In many cases, it is more efficient to output a String by using the writeUtf() method This method encodes the characters in the String using a slightly modified form of UTF-8, which results in Unicode characters with values in the range 0-127 inclusive being encoded as a single byte This is an optimization for the most common characters in Western locales
public interface DataOutput {
// Public Instance Methods
public abstract void write(
byte[] b) throws IOException;
public abstract void write( int b) throws IOException;
public abstract void write(byte[] b, int off,
int len) throws IOException;
public abstract void writeBoolean(
boolean v) throws IOException;
public abstract void writeByte( int v) throws IOException;
public abstract void writeChar( int v) throws IOException;
public abstract void writeChars(
String s) throws IOException;
public abstract void writeInt( int v) throws IOException;
public abstract void writeLong(long v) throws IOException;
public abstract void writeShort(int v) throws IOException;
public abstract void writeUTF(
String str) throws IOException;
This is a subclass of OutputStream that implements the methods of the DataOutput
interface, writing encoded data as a sequence of bytes to another OutputStream Instances of this class can be created directly In addition, they can also be obtained from various other sources, such as the openDataOutputStream method of the javax.microedition.io.Connector This example returns a stream to write data to
a network connection or some other type of data source accessed using the CLDC Generic Connection Framework
Note that the CLDC version of this class is derived from OutputStream and not
FilterOutputStream, as it is in J2SE This is because the CLDC does not have
a FilterOutputStream class
Trang 25public class DataOutputStream extends OutputStream implements DataOutput {
// Public Constructors
public DataOutputStream( OutputStream out);
// Methods Implementing DataOutput
public void write( int b) throws IOException;
public void write(byte[] b, int off,
int len) throws IOException;
public final void writeBoolean(
boolean v) throws IOException;
public final void writeByte( int v) throws IOException;
public final void writeChar( int v) throws IOException;
public final void writeChars(String s) throws IOException;
public final void writeInt( int v) throws IOException;
public final void writeLong( long v) throws IOException;
public final void writeShort( int v) throws IOException;
public final void writeUTF(String str) throws IOException;
// Public Methods Overriding OutputStream
public void close() throws IOException;
public void flush() throws IOException;
// Protected Instance Fields
protected OutputStream out;
public class EOFException extends IOException {
// Public Constructors
public EOFException();
public EOFException( String s);
}
Trang 26InputStream CLDC 1.0, MIDP 1.0
java.io
This is an abstract class that defines the methods used to read data from an input source in the form of a stream of bytes
The no-argument read() method returns a single byte from the stream in the low-order 8 bits
of an int, blocking until a byte is available to be read If no more data is available from the stream, -1 is returned There are also two other variants of read() that read a sequence of bytes into a buffer and return the number of bytes actually read, or -1 if end-of-file has been reached These methods also block until data is available; however, they only guarantee to return a single byte they do not block until the buffer is full If you need this functionality, wrap the InputStream with a DataInputStream and use the readFully() method
The available() method can be used to determine how much data is available to be read without blocking; this method is useful when the input source is a network connection which typically does not have all of its data immediately available The skip() skips over a given number of bytes in the input stream; this method returns the number of bytes that it actually skipped, which may be fewer than the number requested if end-of-file was reached before or during the operation
The mark() and reset() methods provide the capability for application code to mark a position in the input stream, which may be returned to later The argument passed to the
mark() method specifies the maximum number of bytes that application code will read beyond the mark before invoking the reset() and therefore represents the maximum buffering that the stream will need to use to save the data if it cannot be re-read on demand from the underlying data source Not all InputStream subclasses support this functionality;
markSupported() can be used to discover whether this facility is available
The close() method closes the InputStream and releases any resources associated with it
An IOException may be thrown if an attempt is made to use a closed input stream (although not all InputStream subclasses do this)
It is important to note that an InputStream is byte-oriented and not character-oriented and
therefore cannot safely be used on its own to read character data by widening a sequence of bytes into a sequence of chars Instead, character data should be handled using the
readUTF() or readChar() methods of DataInputStream or by using an
InputStreamReader with the appropriate encoding for the data in the input stream
public abstract class InputStream {
// Public Constructors
public InputStream();
// Public Instance Methods
public int available() throws IOException; // constant
public void close() throws IOException; // empty
public void mark( int readlimit); // synchronized empty public boolean markSupported(); // constant