FileDocCategorySizeDatePackage
BufferedPrintStream.javaAPI DocphoneME MR2 API (J2ME)7931Wed May 02 17:59:48 BST 2007util

BufferedPrintStream

public class BufferedPrintStream extends PrintStream
Like PrintStream, but does its own buffering. This mashing of abstraction layers saves many a methodcall. NOTE the lack of synchronization! Also note the lack of autoflush. Finally, note especially troublesome solution we use in determining when to flush buffers. This relies on relative speed of dealing with a Java exception and the size of the buffer, thus infrequency of its occurrence.

Fields Summary
private static final int
bufsize
private static final int
buflastindex
protected byte[]
buf
protected int
curindex
protected OutputStream
f
protected boolean
errorOccured
protected IOException
recentException
private static final byte
NL
private static byte[]
truth
private static byte[]
falsity
private static final int
nnumbuf
private byte[]
numbuf
Constructors Summary
public BufferedPrintStream(OutputStream file)

	super( file );// whatever...
	f = file;
	errorOccured = false;
	buf = new byte[ bufsize ];
	curindex = 0;
    
Methods Summary
public booleancheckException()

	flush();
	return errorOccured;
    
public voidclose()

	flush();
	buf = null;
	numbuf = null;
	try {
	    if ( f != null )
		f.close();
	}catch( IOException e ){
	    errorOccured = true;
	    recentException = e;
	}
	super.close();
	f = null;
    
public voidflush()

	try{
	    if ( curindex != 0 ){
		f.write( buf, 0, curindex );
	    }
	    f.flush();
	} catch (IOException e ){
	    errorOccured = true;
	    recentException = e;
	}
	curindex = 0;
    
protected voidflushit()


      
	try{
	    f.write( buf, 0, curindex );
	} catch (IOException e ){
	    errorOccured = true;
	    recentException = e;
	}
	curindex = 0;
    
public voidprint(char c)

	try{
	    buf[curindex] = (byte)c;
	    curindex++;
	    return;
	}catch( IndexOutOfBoundsException e ){
	    flushit();
	    buf[curindex++] = (byte)c;
	}
    
public voidprint(char[] s)

	int slen = s.length;
	for ( int i = 0; i < slen; i++ ){
	    try{
		buf[curindex] = (byte) s[i];
		curindex++;
		continue;
	    }catch( IndexOutOfBoundsException e ){
		flushit();
		buf[curindex++] = (byte) s[i];
	    }
	}
    
public voidprint(int i)

 // for integer conversions

        
	if ( i < 0 ){
	    if ( i == Integer.MIN_VALUE ){
		// special case, not worth bothering with.
		print( Integer.toString(Integer.MIN_VALUE));
		return;
	    }
	    i = -i;
	    // since even negative numbers hardly happen,
	    // we don't mind this extra call...
	    write('-");
	}
	int n = nnumbuf;
	do {
	   numbuf[--n]= (byte)('0"+(i%10));
	   i /= 10;
	}while( i != 0 );
	int nsz = nnumbuf-n;
	try{
	    System.arraycopy( numbuf, n, buf, curindex, nsz );
	}catch( IndexOutOfBoundsException e ){
	    flushit();
	    System.arraycopy( numbuf, n, buf, curindex, nsz );
	}
	curindex += nsz;
    
public voidprint(long l)

	if ( l < 0 ){
	    if ( l == Long.MIN_VALUE ){
		// special case, not worth bothering with.
		print( Long.toString(Long.MIN_VALUE));
		return;
	    }
	    l = -l;
	    // since even negative numbers hardly happen,
	    // we don't mind this extra call...
	    write('-");
	}
	int n = nnumbuf;
	do {
	   numbuf[--n]= (byte)('0"+(byte)(l%10L));
	   l /= 10L;
	}while( l != 0L );
	int nsz = nnumbuf-n;
	try{
	    System.arraycopy( numbuf, n, buf, curindex, nsz-n );
	}catch( IndexOutOfBoundsException e ){
	    flushit();
	    System.arraycopy( numbuf, n, buf, curindex, nsz-n );
	}
	curindex += nsz;
    
public voidprint(java.lang.Object o)

	if ( o == null )
	    print( "(null)");
	else
	    print( o.toString() );
    
public voidprint(java.lang.String s)

	int slen = s.length();
	try{
	    s.getBytes( 0, slen, buf, curindex );
	}catch( IndexOutOfBoundsException e ){
	    flushit();
	    s.getBytes( 0, slen, buf, curindex );
	}
	curindex += slen;
    
public voidprint(boolean b)


         
	if ( b ){
	    write( truth, 0, 4 );
	} else {
	    write( falsity, 0, 5 );
	}
    
public voidprintln(char c)

	try {
	    buf[curindex] = (byte)c;
	    curindex++;
	}catch( IndexOutOfBoundsException e ){
	    flushit();
	    buf[curindex++] = (byte)c;
	    buf[curindex++] = NL;
	    return;
	}
	try{
	    buf[curindex] = NL;
	    curindex++;
	    return;
	}catch( IndexOutOfBoundsException e ){
	    flushit();
	    buf[curindex++] = NL;
	}
    
public voidprintln(char[] s)

	// enough pain to not be worth replicating the above
	print( s );
	//if ( curindex >= bufsize-1 ) flushit();
	// count on print to have left one space at end of buf for us.
	try{
	    buf[curindex] = NL;
	    curindex++;
	}catch( IndexOutOfBoundsException e ){
	    flushit();
	    buf[curindex++] = NL;
	}
    
public voidprintln(int i)

	// enough pain to not be worth replicating the above
	print( i );
	try{
	    buf[curindex] = NL;
	    curindex++;
	}catch( IndexOutOfBoundsException e ){
	    flushit();
	    buf[curindex++] = NL;
	}
    
public voidprintln(long l)

	// enough pain to not be worth replicating the above
	print( l );
	try{
	    buf[curindex] = NL;
	    curindex++;
	}catch( IndexOutOfBoundsException e ){
	    flushit();
	    buf[curindex++] = NL;
	}
    
public voidprintln(java.lang.Object o)

	if ( o == null )
	    println( "(null)");
	else
	    println( o.toString() );
    
public voidprintln(java.lang.String s)

	int slen = s.length();
	try{
	    s.getBytes( 0, slen, buf, curindex );
	}catch( IndexOutOfBoundsException e ){
	    flushit();
	    s.getBytes( 0, slen, buf, curindex );
	}
	curindex += slen;
	try{
	    buf[curindex] = NL;
	    curindex++;
	}catch( IndexOutOfBoundsException e ){
	    flushit();
	    buf[curindex++] = NL;
	}
    
public voidprintln()

	try{
	    buf[curindex] = NL;
	    curindex++;
	}catch( IndexOutOfBoundsException e ){
	    flushit();
	    buf[curindex++] = NL;
	}
    
public voidprintln(boolean b)

	if ( b ){
	    write( truth, 0, 5 );
	} else {
	    write( falsity, 0, 6 );
	}
    
public voidwrite(int b)

	try {
	    buf[curindex] = (byte)b;
	    curindex++;
	    return;
	}catch( IndexOutOfBoundsException e ){
	    flushit();
	    buf[curindex++] = (byte)b;
	}
    
public voidwrite(byte[] b, int off, int len)

	try{
	    if (len == 1 ){
		buf[curindex] = b[off];
		curindex++;
		return;
	    } else {
		System.arraycopy( b, off, buf, curindex, len );
		curindex += len;
		return;
	    }
	}catch( IndexOutOfBoundsException e ){
	    flushit();
	    System.arraycopy( b, off, buf, curindex, len );
	    curindex += len;
	}