FileDocCategorySizeDatePackage
LineOrientedOutputStream.javaAPI DocApache Ant 1.704215Wed Dec 13 06:16:22 GMT 2006org.apache.tools.ant.util

LineOrientedOutputStream

public abstract class LineOrientedOutputStream extends OutputStream
Invokes {@link #processLine processLine} whenever a full line has been written to this stream.

Tries to be smart about line separators.

Fields Summary
private static final int
INTIAL_SIZE
Initial buffer size.
private static final int
CR
Carriage return
private static final int
LF
Linefeed
private ByteArrayOutputStream
buffer
private boolean
skip
Constructors Summary
Methods Summary
public final voidclose()
Writes all remaining

throws
IOException if there is an error.

        if (buffer.size() > 0) {
            processBuffer();
        }
        super.close();
    
public final voidflush()
Flush this log stream

throws
IOException if there is an error.

        if (buffer.size() > 0) {
            processBuffer();
        }
    
protected voidprocessBuffer()
Converts the buffer to a string and sends it to processLine

throws
IOException if there is an error.

        try {
            processLine(buffer.toString());
        } finally {
            buffer.reset();
        }
    
protected abstract voidprocessLine(java.lang.String line)
Processes a line.

param
line the line to log.
throws
IOException if there is an error.

public final voidwrite(int cc)
Write the data to the buffer and flush the buffer, if a line separator is detected.

param
cc data to log (byte).
throws
IOException if there is an error.


                                      
           
        final byte c = (byte) cc;
        if ((c == LF) || (c == CR)) {
            if (!skip) {
              processBuffer();
            }
        } else {
            buffer.write(cc);
        }
        skip = (c == CR);
    
public final voidwrite(byte[] b, int off, int len)
Write a block of characters to the output stream

param
b the array containing the data
param
off the offset into the array where data starts
param
len the length of block
throws
IOException if the data cannot be written into the stream.

        // find the line breaks and pass other chars through in blocks
        int offset = off;
        int blockStartOffset = offset;
        int remaining = len;
        while (remaining > 0) {
            while (remaining > 0 && b[offset] != LF && b[offset] != CR) {
                offset++;
                remaining--;
            }
            // either end of buffer or a line separator char
            int blockLength = offset - blockStartOffset;
            if (blockLength > 0) {
                buffer.write(b, blockStartOffset, blockLength);
            }
            while (remaining > 0 && (b[offset] == LF || b[offset] == CR)) {
                write(b[offset]);
                offset++;
                remaining--;
            }
            blockStartOffset = offset;
        }