MultiLineReceiverpublic abstract class MultiLineReceiver extends Object implements IShellOutputReceiverBase 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 | mUnfinishedLineunfinished message line, stored for next packet | private final ArrayList | mArray |
Methods Summary |
---|
public final void | addOutput(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 void | done()Terminates the process. This is called after the last lines have been through
{@link #processNewLines(String[])}.
// do nothing.
| public final void | flush()
if (mUnfinishedLine != null) {
processNewLines(new String[] { mUnfinishedLine });
}
done();
| public abstract void | processNewLines(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.
| public void | setTrimLine(boolean trim)Set the trim lines flag.
mTrimLines = trim;
|
|