FileDocCategorySizeDatePackage
LoggingPrintStream.javaAPI DocAndroid 5.1 API9202Thu Mar 12 22:22:10 GMT 2015com.android.internal.os

LoggingPrintStream

public abstract class LoggingPrintStream extends PrintStream
A print stream which logs output line by line. {@hide}

Fields Summary
private final StringBuilder
builder
private ByteBuffer
encodedBytes
A buffer that is initialized when raw bytes are first written to this stream. It may contain the leading bytes of multi-byte characters. Between writes this buffer is always ready to receive data; ie. the position is at the first unassigned byte and the limit is the capacity.
private CharBuffer
decodedChars
A buffer that is initialized when raw bytes are first written to this stream. Between writes this buffer is always clear; ie. the position is zero and the limit is the capacity.
private CharsetDecoder
decoder
Decodes bytes to characters using the system default charset. Initialized when raw bytes are first written to this stream.
private final Formatter
formatter
Constructors Summary
protected LoggingPrintStream()


      
        super(new OutputStream() {
            public void write(int oneByte) throws IOException {
                throw new AssertionError();
            }
        });
    
Methods Summary
public synchronized java.io.PrintStreamappend(char c)

        print(c);
        return this;
    
public synchronized java.io.PrintStreamappend(java.lang.CharSequence csq)

        builder.append(csq);
        flush(false);
        return this;
    
public synchronized java.io.PrintStreamappend(java.lang.CharSequence csq, int start, int end)

        builder.append(csq, start, end);
        flush(false);
        return this;
    
public booleancheckError()
Always returns false.

        return false;
    
public voidclose()
Ignored.

 /* ignored */ 
public synchronized voidflush()

        flush(true);
    
private voidflush(boolean completely)
Searches buffer for line breaks and logs a message for each one.

param
completely true if the ending chars should be treated as a line even though they don't end in a line break

        int length = builder.length();

        int start = 0;
        int nextBreak;

        // Log one line for each line break.
        while (start < length
                && (nextBreak = builder.indexOf("\n", start)) != -1) {
            log(builder.substring(start, nextBreak));
            start = nextBreak + 1;
        }

        if (completely) {
            // Log the remainder of the buffer.
            if (start < length) {
                log(builder.substring(start));
            }
            builder.setLength(0);
        } else {
            // Delete characters leading up to the next starting point.
            builder.delete(0, start);
        }
    
public java.io.PrintStreamformat(java.lang.String format, java.lang.Object args)

        return format(Locale.getDefault(), format, args);
    
public synchronized java.io.PrintStreamformat(java.util.Locale l, java.lang.String format, java.lang.Object args)


    
       
                  
        if (format == null) {
            throw new NullPointerException("format");
        }

        formatter.format(l, format, args);
        flush(false);
        return this;
    
protected abstract voidlog(java.lang.String line)
Logs the given line.

public synchronized voidprint(char[] charArray)

        builder.append(charArray);
        flush(false);
    
public synchronized voidprint(char ch)

        builder.append(ch);
        if (ch == '\n") {
            flush(false);
        }
    
public synchronized voidprint(double dnum)

        builder.append(dnum);
    
public synchronized voidprint(float fnum)

        builder.append(fnum);
    
public synchronized voidprint(int inum)

        builder.append(inum);
    
public synchronized voidprint(long lnum)

        builder.append(lnum);
    
public synchronized voidprint(java.lang.Object obj)

        builder.append(obj);
        flush(false);
    
public synchronized voidprint(java.lang.String str)

        builder.append(str);
        flush(false);
    
public synchronized voidprint(boolean bool)

        builder.append(bool);
    
public java.io.PrintStreamprintf(java.lang.String format, java.lang.Object args)

        return format(format, args);
    
public java.io.PrintStreamprintf(java.util.Locale l, java.lang.String format, java.lang.Object args)

        return format(l, format, args);
    
public synchronized voidprintln()

        flush(true);
    
public synchronized voidprintln(char[] charArray)

        builder.append(charArray);
        flush(true);
    
public synchronized voidprintln(char ch)

        builder.append(ch);
        flush(true);
    
public synchronized voidprintln(double dnum)

        builder.append(dnum);
        flush(true);
    
public synchronized voidprintln(float fnum)

        builder.append(fnum);
        flush(true);
    
public synchronized voidprintln(int inum)

        builder.append(inum);
        flush(true);
    
public synchronized voidprintln(long lnum)

        builder.append(lnum);
        flush(true);
    
public synchronized voidprintln(java.lang.Object obj)

        builder.append(obj);
        flush(true);
    
public synchronized voidprintln(java.lang.String s)

        if (builder.length() == 0 && s != null) {
            // Optimization for a simple println.
            int length = s.length();

            int start = 0;
            int nextBreak;

            // Log one line for each line break.
            while (start < length
                    && (nextBreak = s.indexOf('\n", start)) != -1) {
                log(s.substring(start, nextBreak));
                start = nextBreak + 1;
            }

            if (start < length) {
                log(s.substring(start));
            }
        } else {
            builder.append(s);
            flush(true);
        }
    
public synchronized voidprintln(boolean bool)

        builder.append(bool);
        flush(true);
    
protected voidsetError()
Ignored.

 /* ignored */ 
public voidwrite(int oneByte)

        write(new byte[] { (byte) oneByte }, 0, 1);
    
public voidwrite(byte[] buffer)

        write(buffer, 0, buffer.length);
    
public synchronized voidwrite(byte[] bytes, int start, int count)

        if (decoder == null) {
            encodedBytes = ByteBuffer.allocate(80);
            decodedChars = CharBuffer.allocate(80);
            decoder = Charset.defaultCharset().newDecoder()
                    .onMalformedInput(CodingErrorAction.REPLACE)
                    .onUnmappableCharacter(CodingErrorAction.REPLACE);
        }

        int end = start + count;
        while (start < end) {
            // copy some bytes from the array to the long-lived buffer. This
            // way, if we end with a partial character we don't lose it.
            int numBytes = Math.min(encodedBytes.remaining(), end - start);
            encodedBytes.put(bytes, start, numBytes);
            start += numBytes;

            encodedBytes.flip();
            CoderResult coderResult;
            do {
                // decode bytes from the byte buffer into the char buffer
                coderResult = decoder.decode(encodedBytes, decodedChars, false);

                // copy chars from the char buffer into our string builder
                decodedChars.flip();
                builder.append(decodedChars);
                decodedChars.clear();
            } while (coderResult.isOverflow());
            encodedBytes.compact();
        }
        flush(false);