1 /* 2 * Copyright (C) 2007 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.ddmlib; 18 19 import java.io.UnsupportedEncodingException; 20 import java.util.ArrayList; 21 22 /** 23 * Base implementation of {@link IShellOutputReceiver}, that takes the raw data coming from the 24 * socket, and convert it into {@link String} objects. 25 * <p/>Additionally, it splits the string by lines. 26 * <p/>Classes extending it must implement {@link #processNewLines(String[])} which receives 27 * new parsed lines as they become available. 28 */ 29 public abstract class MultiLineReceiver implements IShellOutputReceiver { 30 31 private boolean mTrimLines = true; 32 33 /** unfinished message line, stored for next packet */ 34 private String mUnfinishedLine = null; 35 36 private final ArrayList<String> mArray = new ArrayList<String>(); 37 38 /** 39 * Set the trim lines flag. 40 * @param trim hether the lines are trimmed, or not. 41 */ setTrimLine(boolean trim)42 public void setTrimLine(boolean trim) { 43 mTrimLines = trim; 44 } 45 46 /* (non-Javadoc) 47 * @see com.android.ddmlib.adb.IShellOutputReceiver#addOutput( 48 * byte[], int, int) 49 */ 50 @Override addOutput(byte[] data, int offset, int length)51 public final void addOutput(byte[] data, int offset, int length) { 52 if (isCancelled() == false) { 53 String s = null; 54 try { 55 s = new String(data, offset, length, "UTF-8"); //$NON-NLS-1$ 56 } catch (UnsupportedEncodingException e) { 57 // normal encoding didn't work, try the default one 58 s = new String(data, offset,length); 59 } 60 61 // ok we've got a string 62 if (s != null) { 63 // if we had an unfinished line we add it. 64 if (mUnfinishedLine != null) { 65 s = mUnfinishedLine + s; 66 mUnfinishedLine = null; 67 } 68 69 // now we split the lines 70 mArray.clear(); 71 int start = 0; 72 do { 73 int index = s.indexOf("\r\n", start); //$NON-NLS-1$ 74 75 // if \r\n was not found, this is an unfinished line 76 // and we store it to be processed for the next packet 77 if (index == -1) { 78 mUnfinishedLine = s.substring(start); 79 break; 80 } 81 82 // so we found a \r\n; 83 // extract the line 84 String line = s.substring(start, index); 85 if (mTrimLines) { 86 line = line.trim(); 87 } 88 mArray.add(line); 89 90 // move start to after the \r\n we found 91 start = index + 2; 92 } while (true); 93 94 if (mArray.size() > 0) { 95 // at this point we've split all the lines. 96 // make the array 97 String[] lines = mArray.toArray(new String[mArray.size()]); 98 99 // send it for final processing 100 processNewLines(lines); 101 } 102 } 103 } 104 } 105 106 /* (non-Javadoc) 107 * @see com.android.ddmlib.adb.IShellOutputReceiver#flush() 108 */ 109 @Override flush()110 public final void flush() { 111 if (mUnfinishedLine != null) { 112 processNewLines(new String[] { mUnfinishedLine }); 113 } 114 115 done(); 116 } 117 118 /** 119 * Terminates the process. This is called after the last lines have been through 120 * {@link #processNewLines(String[])}. 121 */ done()122 public void done() { 123 // do nothing. 124 } 125 126 /** 127 * Called when new lines are being received by the remote process. 128 * <p/>It is guaranteed that the lines are complete when they are given to this method. 129 * @param lines The array containing the new lines. 130 */ processNewLines(String[] lines)131 public abstract void processNewLines(String[] lines); 132 } 133