FileDocCategorySizeDatePackage
MultiLineReceiver.javaAPI DocAndroid 1.5 API4427Wed May 06 22:41:08 BST 2009com.android.ddmlib

MultiLineReceiver

public abstract class MultiLineReceiver extends Object implements IShellOutputReceiver
Base implementation of {@link IShellOutputReceiver}, that takes the raw data coming from the socket, and convert it into {@link String} objects.

Additionally, it splits the string by lines.

Classes extending it must implement {@link #processNewLines(String[])} which receives new parsed lines as they become available.

Fields Summary
private boolean
mTrimLines
private String
mUnfinishedLine
unfinished message line, stored for next packet
private final ArrayList
mArray
Constructors Summary
Methods Summary
public final voidaddOutput(byte[] data, int offset, int length)

        if (isCancelled() == false) {
            String s = null;
            try {
                s = new String(data, offset, length, "ISO-8859-1"); //$NON-NLS-1$
            } catch (UnsupportedEncodingException e) {
                // normal encoding didn't work, try the default one
                s = new String(data, offset,length);
            }

            // ok we've got a string
            if (s != null) {
                // if we had an unfinished line we add it.
                if (mUnfinishedLine != null) {
                    s = mUnfinishedLine + s;
                    mUnfinishedLine = null;
                }

                // now we split the lines
                mArray.clear();
                int start = 0;
                do {
                    int index = s.indexOf("\r\n", start); //$NON-NLS-1$

                    // if \r\n was not found, this is an unfinished line
                    // and we store it to be processed for the next packet
                    if (index == -1) {
                        mUnfinishedLine = s.substring(start);
                        break;
                    }

                    // so we found a \r\n;
                    // extract the line
                    String line = s.substring(start, index);
                    if (mTrimLines) {
                        line = line.trim();
                    }
                    mArray.add(line);

                    // move start to after the \r\n we found
                    start = index + 2;
                } while (true);

                if (mArray.size() > 0) {
                    // at this point we've split all the lines.
                    // make the array
                    String[] lines = mArray.toArray(new String[mArray.size()]);

                    // send it for final processing
                    processNewLines(lines);
                }
            }
        }
    
public voiddone()
Terminates the process. This is called after the last lines have been through {@link #processNewLines(String[])}.

        // do nothing.
    
public final voidflush()

        if (mUnfinishedLine != null) {
            processNewLines(new String[] { mUnfinishedLine });
        }

        done();
    
public abstract voidprocessNewLines(java.lang.String[] lines)
Called when new lines are being received by the remote process.

It is guaranteed that the lines are complete when they are given to this method.

param
lines The array containing the new lines.

public voidsetTrimLine(boolean trim)
Set the trim lines flag.

param
trim hether the lines are trimmed, or not.


                       
        
        mTrimLines = trim;