2929import java .io .IOException ;
3030import java .io .InputStream ;
3131import java .io .OutputStream ;
32+ import java .nio .charset .Charsets ;
3233import java .util .ArrayList ;
3334import java .util .concurrent .BlockingQueue ;
3435import java .util .concurrent .LinkedBlockingQueue ;
@@ -122,13 +123,15 @@ private void listenToSocket() throws IOException {
122123
123124 for (int i = 0 ; i < count ; i ++) {
124125 if (buffer [i ] == 0 ) {
125- final String rawEvent = new String (buffer , start , i - start );
126+ final String rawEvent = new String (
127+ buffer , start , i - start , Charsets .UTF_8 );
126128 if (LOGD ) Slog .d (TAG , "RCV <- " + rawEvent );
127129
128130 try {
129131 final NativeDaemonEvent event = NativeDaemonEvent .parseRawEvent (
130132 rawEvent );
131133 if (event .isClassUnsolicited ()) {
134+ // TODO: migrate to sending NativeDaemonEvent instances
132135 mCallbackHandler .sendMessage (mCallbackHandler .obtainMessage (
133136 event .getCode (), event .getRawEvent ()));
134137 } else {
@@ -213,7 +216,7 @@ private String sendCommandLocked(String cmd, Object... args)
213216 throw new NativeDaemonConnectorException ("missing output stream" );
214217 } else {
215218 try {
216- mOutputStream .write (builder .toString ().getBytes ());
219+ mOutputStream .write (builder .toString ().getBytes (Charsets . UTF_8 ));
217220 } catch (IOException e ) {
218221 throw new NativeDaemonConnectorException ("problem sending command" , e );
219222 }
@@ -223,9 +226,62 @@ private String sendCommandLocked(String cmd, Object... args)
223226 }
224227
225228 /**
226- * Issue a command to the native daemon and return the responses.
229+ * Issue the given command to the native daemon and return a single expected
230+ * response.
231+ *
232+ * @throws NativeDaemonConnectorException when problem communicating with
233+ * native daemon, or if the response matches
234+ * {@link NativeDaemonEvent#isClassClientError()} or
235+ * {@link NativeDaemonEvent#isClassServerError()}.
227236 */
228- public NativeDaemonEvent [] execute (String cmd , Object ... args )
237+ public NativeDaemonEvent execute (Command cmd ) throws NativeDaemonConnectorException {
238+ return execute (cmd .mCmd , cmd .mArguments .toArray ());
239+ }
240+
241+ /**
242+ * Issue the given command to the native daemon and return a single expected
243+ * response.
244+ *
245+ * @throws NativeDaemonConnectorException when problem communicating with
246+ * native daemon, or if the response matches
247+ * {@link NativeDaemonEvent#isClassClientError()} or
248+ * {@link NativeDaemonEvent#isClassServerError()}.
249+ */
250+ public NativeDaemonEvent execute (String cmd , Object ... args )
251+ throws NativeDaemonConnectorException {
252+ final NativeDaemonEvent [] events = executeForList (cmd , args );
253+ if (events .length != 1 ) {
254+ throw new NativeDaemonConnectorException (
255+ "Expected exactly one response, but received " + events .length );
256+ }
257+ return events [0 ];
258+ }
259+
260+ /**
261+ * Issue the given command to the native daemon and return any
262+ * {@link NativeDaemonEvent#isClassContinue()} responses, including the
263+ * final terminal response.
264+ *
265+ * @throws NativeDaemonConnectorException when problem communicating with
266+ * native daemon, or if the response matches
267+ * {@link NativeDaemonEvent#isClassClientError()} or
268+ * {@link NativeDaemonEvent#isClassServerError()}.
269+ */
270+ public NativeDaemonEvent [] executeForList (Command cmd ) throws NativeDaemonConnectorException {
271+ return executeForList (cmd .mCmd , cmd .mArguments .toArray ());
272+ }
273+
274+ /**
275+ * Issue the given command to the native daemon and return any
276+ * {@link NativeDaemonEvent#isClassContinue()} responses, including the
277+ * final terminal response.
278+ *
279+ * @throws NativeDaemonConnectorException when problem communicating with
280+ * native daemon, or if the response matches
281+ * {@link NativeDaemonEvent#isClassClientError()} or
282+ * {@link NativeDaemonEvent#isClassServerError()}.
283+ */
284+ public NativeDaemonEvent [] executeForList (String cmd , Object ... args )
229285 throws NativeDaemonConnectorException {
230286 synchronized (mDaemonLock ) {
231287 return executeLocked (cmd , args );
@@ -270,7 +326,7 @@ private NativeDaemonEvent[] executeLocked(String cmd, Object... args)
270326 @ Deprecated
271327 public ArrayList <String > doCommand (String cmd ) throws NativeDaemonConnectorException {
272328 final ArrayList <String > rawEvents = Lists .newArrayList ();
273- final NativeDaemonEvent [] events = execute (cmd );
329+ final NativeDaemonEvent [] events = executeForList (cmd );
274330 for (NativeDaemonEvent event : events ) {
275331 rawEvents .add (event .getRawEvent ());
276332 }
@@ -281,11 +337,12 @@ public ArrayList<String> doCommand(String cmd) throws NativeDaemonConnectorExcep
281337 * Issues a list command and returns the cooked list of all
282338 * {@link NativeDaemonEvent#getMessage()} which match requested code.
283339 */
340+ @ Deprecated
284341 public String [] doListCommand (String cmd , int expectedCode )
285342 throws NativeDaemonConnectorException {
286343 final ArrayList <String > list = Lists .newArrayList ();
287344
288- final NativeDaemonEvent [] events = execute (cmd );
345+ final NativeDaemonEvent [] events = executeForList (cmd );
289346 for (int i = 0 ; i < events .length - 1 ; i ++) {
290347 final NativeDaemonEvent event = events [i ];
291348 final int code = event .getCode ();
@@ -351,6 +408,26 @@ public NativeDaemonFailureException(String command, NativeDaemonEvent event) {
351408 }
352409 }
353410
411+ /**
412+ * Command builder that handles argument list building.
413+ */
414+ public static class Command {
415+ private String mCmd ;
416+ private ArrayList <Object > mArguments = Lists .newArrayList ();
417+
418+ public Command (String cmd , Object ... args ) {
419+ mCmd = cmd ;
420+ for (Object arg : args ) {
421+ appendArg (arg );
422+ }
423+ }
424+
425+ public Command appendArg (Object arg ) {
426+ mArguments .add (arg );
427+ return this ;
428+ }
429+ }
430+
354431 /** {@inheritDoc} */
355432 public void monitor () {
356433 synchronized (mDaemonLock ) { }
0 commit comments