FileDocCategorySizeDatePackage
PrintStream.javaAPI DocphoneME MR2 API (J2ME)14466Wed May 02 17:59:54 BST 2007java.io

PrintStream

public class PrintStream extends OutputStream
A PrintStream adds functionality to another output stream, namely the ability to print representations of various data values conveniently. Two other features are provided as well. Unlike other output streams, a PrintStream never throws an IOException; instead, exceptional situations merely set an internal flag that can be tested via the checkError method. Optionally, a PrintStream can be created so as to flush automatically; this means that the flush method is automatically invoked after a byte array is written, one of the println methods is invoked, or a newline character or byte ('\n') is written.

All characters printed by a PrintStream are converted into bytes using the platform's default character encoding.

version
1.20, 99/12/04 (CLDC 1.0, Spring 2000)
since
JDK1.0

Fields Summary
private boolean
trouble
private OutputStreamWriter
charOut
Track both the text- and character-output streams, so that their buffers can be flushed without flushing the entire stream.
private OutputStream
byteOut
private boolean
closing
Constructors Summary
public PrintStream(OutputStream out)
Create a new print stream. This stream will not flush automatically.

param
out The output stream to which values and objects will be printed


                                                             
       
        if (out == null) {
            throw new NullPointerException(
/* #ifdef VERBOSE_EXCEPTIONS */
/// skipped                       "Null output stream"
/* #endif */
            );
        }
        byteOut = out;
        this.charOut = new OutputStreamWriter(out);
    
Methods Summary
public booleancheckError()
Flush the stream and check its error state. The internal error state is set to true when the underlying output stream throws an IOException, and when the setError method is invoked.

return
True if and only if this stream has encountered an IOException, or the setError method has been invoked

        if (charOut != null)
            flush();
        return trouble;
    
public voidclose()
Close the stream. This is done by flushing the stream and then closing the underlying output stream.

see
java.io.OutputStream#close()

 /* To avoid recursive closing */

                                    
       
        synchronized (this) {
            if (! closing) {
                closing = true;
                try {
                      charOut.close();
                }
                catch (IOException x) {
                    trouble = true;
                }
                charOut = null;
                byteOut = null;
            }
        }
    
private voidensureOpen()
Check to make sure that the stream has not been closed

        if (charOut == null)
            throw new IOException(
/* #ifdef VERBOSE_EXCEPTIONS */
/// skipped                       "Stream closed"
/* #endif */
            );
    
public voidflush()
Flush the stream. This is done by writing any buffered output bytes to the underlying output stream and then flushing that stream.

see
java.io.OutputStream#flush()

        synchronized (this) {
            try {
                ensureOpen();
                charOut.flush();
            }
            catch (IOException x) {
                trouble = true;
            }
        }
    
private voidnewLine()

        try {
            synchronized (this) {
                ensureOpen();
                charOut.write('\n");
            }
        } catch (IOException x) {
            trouble = true;
        }
    
public voidprint(boolean b)
Print a boolean value. The string produced by {@link java.lang.String#valueOf(boolean)} is translated into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the {@link #write(int)} method.

param
b The boolean to be printed

        write(b ? "true" : "false");
    
public voidprint(char c)
Print a character. The character is translated into one or more bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the {@link #write(int)} method.

param
c The char to be printed

        write(String.valueOf(c));
    
public voidprint(int i)
Print an integer. The string produced by {@link java.lang.String#valueOf(int)} is translated into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the {@link #write(int)} method.

param
i The int to be printed
see
java.lang.Integer#toString(int)

        write(String.valueOf(i));
    
public voidprint(long l)
Print a long integer. The string produced by {@link java.lang.String#valueOf(long)} is translated into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the {@link #write(int)} method.

param
l The long to be printed
see
java.lang.Long#toString(long)

        write(String.valueOf(l));
    
public voidprint(char[] s)
Print an array of characters. The characters are converted into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the {@link #write(int)} method.

param
s The array of chars to be printed
throws
NullPointerException If s is null

        write(s);
    
public voidprint(java.lang.String s)
Print a string. If the argument is null then the string "null" is printed. Otherwise, the string's characters are converted into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the {@link #write(int)} method.

param
s The String to be printed

        if (s == null) {
            s = "null";
        }
        write(s);
    
public voidprint(java.lang.Object obj)
Print an object. The string produced by the {@link java.lang.String#valueOf(Object)} method is translated into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the {@link #write(int)} method.

param
obj The Object to be printed
see
java.lang.Object#toString()

        write(String.valueOf(obj));
    
public voidprintln()
Terminate the current line by writing the line separator string. The line separator string is defined by the system property line.separator, and is not necessarily a single newline character ('\n').

        newLine();
    
public voidprintln(boolean x)
Print a boolean and then terminate the line. This method behaves as though it invokes {@link #print(boolean)} and then {@link #println()}.

param
x The boolean to be printed

        synchronized (this) {
            print(x);
            newLine();
        }
    
public voidprintln(char x)
Print a character and then terminate the line. This method behaves as though it invokes {@link #print(char)} and then {@link #println()}.

param
x The char to be printed.

        synchronized (this) {
            print(x);
            newLine();
        }
    
public voidprintln(int x)
Print an integer and then terminate the line. This method behaves as though it invokes {@link #print(int)} and then {@link #println()}.

param
x The int to be printed.

        synchronized (this) {
            print(x);
            newLine();
        }
    
public voidprintln(long x)
Print a long and then terminate the line. This method behaves as though it invokes {@link #print(long)} and then {@link #println()}.

param
x a The long to be printed.

        synchronized (this) {
            print(x);
            newLine();
        }
    
public voidprintln(char[] x)
Print an array of characters and then terminate the line. This method behaves as though it invokes {@link #print(char[])} and then {@link #println()}.

param
x an array of chars to print.

        synchronized (this) {
            print(x);
            newLine();
        }
    
public voidprintln(java.lang.String x)
Print a String and then terminate the line. This method behaves as though it invokes {@link #print(String)} and then {@link #println()}.

param
x The String to be printed.

        synchronized (this) {
            print(x);
            newLine();
        }
    
public voidprintln(java.lang.Object x)
Print an Object and then terminate the line. This method behaves as though it invokes {@link #print(Object)} and then {@link #println()}.

param
x The Object to be printed.

        synchronized (this) {
            print(x);
            newLine();
        }
    
protected voidsetError()
Set the error state of the stream to true.

since
JDK1.1

        trouble = true;
    
private voidwrite(java.lang.String s)

        try {
            synchronized (this) {
                ensureOpen();
                charOut.write(s);
            }
        } catch (IOException x) {
            trouble = true;
        }
    
public voidwrite(int b)
Write the specified byte to this stream. If the byte is a newline and automatic flushing is enabled then the flush method will be invoked.

Note that the byte is written as given; to write a character that will be translated according to the platform's default character encoding, use the print(char) or println(char) methods.

param
b The byte to be written
see
#print(char)
see
#println(char)

        try {
            synchronized (this) {
                ensureOpen();
                byteOut.write(b);
            }
        } catch (IOException x) {
            trouble = true;
        }
    
public voidwrite(byte[] buf, int off, int len)
Write len bytes from the specified byte array starting at offset off to this stream. If automatic flushing is enabled then the flush method will be invoked.

Note that the bytes will be written as given; to write characters that will be translated according to the platform's default character encoding, use the print(char) or println(char) methods.

param
buf A byte array
param
off Offset from which to start taking bytes
param
len Number of bytes to write

        try {
            synchronized (this) {
                ensureOpen();
                byteOut.write(buf, off, len);
            }
        } catch (IOException x) {
            trouble = true;
        }
    
private voidwrite(char[] buf)

        try {
            synchronized (this) {
                ensureOpen();
                charOut.write(buf);
            }
        } catch (IOException x) {
            trouble = true;
        }