From e180359d34f3309ac7be938d43209de3acb1e1b6 Mon Sep 17 00:00:00 2001 From: Bon-Yong Lee Date: Tue, 7 May 2013 11:11:40 +0900 Subject: [PATCH] [Title] Fix unsafed refactoring [Desc.] [Issue] --- .../src/org/tizen/sdblib/ArrayHelper.java | 110 +++++++++++++++++++++ .../tizen/sdblib/SdbCommandRejectedException.java | 76 ++++++++++++++ .../src/org/tizen/sdblib/SdbShellProcess.java | 109 ++++++++++++++++++++ .../sdblib/ShellCommandUnresponsiveException.java | 35 +++++++ .../src/org/tizen/sdblib/TimeoutException.java | 35 +++++++ 5 files changed, 365 insertions(+) create mode 100755 org.tizen.common.sdblib/src/org/tizen/sdblib/ArrayHelper.java create mode 100755 org.tizen.common.sdblib/src/org/tizen/sdblib/SdbCommandRejectedException.java create mode 100755 org.tizen.common.sdblib/src/org/tizen/sdblib/SdbShellProcess.java create mode 100755 org.tizen.common.sdblib/src/org/tizen/sdblib/ShellCommandUnresponsiveException.java create mode 100755 org.tizen.common.sdblib/src/org/tizen/sdblib/TimeoutException.java diff --git a/org.tizen.common.sdblib/src/org/tizen/sdblib/ArrayHelper.java b/org.tizen.common.sdblib/src/org/tizen/sdblib/ArrayHelper.java new file mode 100755 index 0000000..6778c85 --- /dev/null +++ b/org.tizen.common.sdblib/src/org/tizen/sdblib/ArrayHelper.java @@ -0,0 +1,110 @@ +/* + * Copyright (C) 2007 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.tizen.sdblib; + +/** + * Utility class providing array to int/long conversion for data received from devices through sdb. + */ +public final class +ArrayHelper +{ + + protected ArrayHelper() {} + + /** + * Swaps an unsigned value around, and puts the result in an array that can be sent to a device. + * @param value The value to swap. + * @param dest the destination array + * @param offset the offset in the array where to put the swapped value. + * Array length must be at least offset + 4 + */ + public static + void + swap32bitsToArray( + final int value, + final byte[] dest, + final int offset + ) + { + dest[offset] = (byte)(value & 0x000000FF); + dest[offset + 1] = (byte)((value & 0x0000FF00) >> 8); + dest[offset + 2] = (byte)((value & 0x00FF0000) >> 16); + dest[offset + 3] = (byte)((value & 0xFF000000) >> 24); + } + + /** + * Reads a signed 32 bit integer from an array coming from a device. + * @param value the array containing the int + * @param offset the offset in the array at which the int starts + * @return the integer read from the array + */ + public static + int + swap32bitFromArray( + final byte[] value, + final int offset + ) + { + return (((int)value[offset]) & 0x000000FF) + | ((((int)value[offset + 1]) & 0x000000FF) << 8) + | ((((int)value[offset + 2]) & 0x000000FF) << 16) + | ((((int)value[offset + 3]) & 0x000000FF) << 24); + } + + /** + * Reads an unsigned 16 bit integer from an array coming from a device, + * and returns it as an 'int' + * @param value the array containing the 16 bit int (2 byte). + * @param offset the offset in the array at which the int starts + * Array length must be at least offset + 2 + * @return the integer read from the array. + */ + public static + int + swapU16bitFromArray( + final byte[] value, + final int offset + ) + { + return (((int)value[offset]) & 0x000000FF ) + | ((((int)value[offset + 1]) & 0x000000FF) << 8); + } + + /** + * Reads a signed 64 bit integer from an array coming from a device. + * @param value the array containing the int + * @param offset the offset in the array at which the int starts + * Array length must be at least offset + 8 + * @return the integer read from the array + */ + public static + long + swap64bitFromArray( + final byte[] value, + final int offset + ) + { + return (((long)value[offset]) & 0x00000000000000FFL) + | ((((long)value[offset + 1]) & 0x00000000000000FFL) << 8) + | ((((long)value[offset + 2]) & 0x00000000000000FFL) << 16) + | ((((long)value[offset + 3]) & 0x00000000000000FFL) << 24) + | ((((long)value[offset + 4]) & 0x00000000000000FFL) << 32) + | ((((long)value[offset + 5]) & 0x00000000000000FFL) << 40) + | ((((long)value[offset + 6]) & 0x00000000000000FFL) << 48) + | ((((long)value[offset + 7]) & 0x00000000000000FFL) << 56); + } +} diff --git a/org.tizen.common.sdblib/src/org/tizen/sdblib/SdbCommandRejectedException.java b/org.tizen.common.sdblib/src/org/tizen/sdblib/SdbCommandRejectedException.java new file mode 100755 index 0000000..0aea583 --- /dev/null +++ b/org.tizen.common.sdblib/src/org/tizen/sdblib/SdbCommandRejectedException.java @@ -0,0 +1,76 @@ +/* + * Copyright (C) 2010 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.tizen.sdblib; + +import java.io.IOException; + +/** + * Exception thrown when sdb refuses a command. + */ +public class +SdbCommandRejectedException +extends IOException +{ + /** + * + */ + private static final long serialVersionUID = 3466997028942667337L; + + private final boolean bDeviceOffline; + private final boolean bErrorDuringDeviceSelection; + + public + SdbCommandRejectedException( + final String message + ) + { + this( message, false ); + } + + public + SdbCommandRejectedException( + final String message, + final boolean errorDuringDeviceSelection + ) + { + super( message ); + bErrorDuringDeviceSelection = errorDuringDeviceSelection; + bDeviceOffline = "device offline".equals(message); + } + + /** + * Returns true if the error is due to the device being offline. + */ + public + boolean + isDeviceOffline() + { + return bDeviceOffline; + } + + /** + * Returns whether sdb refused to target a given device for the command. + *

If false, sdb refused the command itself, if true, it refused to target the given + * device. + */ + public + boolean + wasErrorDuringDeviceSelection() + { + return bErrorDuringDeviceSelection; + } +} diff --git a/org.tizen.common.sdblib/src/org/tizen/sdblib/SdbShellProcess.java b/org.tizen.common.sdblib/src/org/tizen/sdblib/SdbShellProcess.java new file mode 100755 index 0000000..87e052e --- /dev/null +++ b/org.tizen.common.sdblib/src/org/tizen/sdblib/SdbShellProcess.java @@ -0,0 +1,109 @@ +/* + * Common + * + * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: + * Hyunsik Noh + * BonYong Lee + * Kangho Kim + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * - S-Core Co., Ltd + * + */ +package org.tizen.sdblib; + +import java.io.BufferedWriter; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.OutputStreamWriter; + +import org.tizen.sdblib.Log; + +public class SdbShellProcess extends Process { + + public Process process = null; + public BufferedWriter bw = null; + + public SdbShellProcess(Process process, String command) { + this.process = process; + bw = new BufferedWriter(new OutputStreamWriter(this.process.getOutputStream())); + try { + bw.write(command + ";exit"); + bw.newLine(); + bw.flush(); + } catch (IOException e) { + Log.e("sdb", "buffer write failed:" + e); + } + } + + private void interrupt() { + char c = 0x03; //standard code representing "Ctrl-C" sequence + try { + bw.write(c); + bw.flush(); + Thread.sleep(250); // wait for terminate + } catch (IOException e) { + Log.e("sdb", "buffer write failed:" + e); + } catch (InterruptedException e) { + Log.e("sdb", "buffer interruted:" + e); + } finally { + try { + bw.close(); + } catch (IOException e) { + Log.e("sdb", "close failed:" + e); + } + } + } + + @Override + public void destroy() { + interrupt(); + try { + process.exitValue(); + } + catch ( final IllegalThreadStateException e ) + { + process.destroy(); + } + } + + @Override + public int exitValue() { + return process.exitValue(); + } + + @Override + public InputStream getErrorStream() { + return process.getErrorStream(); + } + + @Override + public InputStream getInputStream() { + return process.getInputStream(); + } + + @Override + public OutputStream getOutputStream() { + return process.getOutputStream(); + } + + @Override + public int waitFor() throws InterruptedException { + return process.waitFor(); + } +} diff --git a/org.tizen.common.sdblib/src/org/tizen/sdblib/ShellCommandUnresponsiveException.java b/org.tizen.common.sdblib/src/org/tizen/sdblib/ShellCommandUnresponsiveException.java new file mode 100755 index 0000000..5b7db1f --- /dev/null +++ b/org.tizen.common.sdblib/src/org/tizen/sdblib/ShellCommandUnresponsiveException.java @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2010 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.tizen.sdblib; + +import java.io.IOException; + +/** + * Exception thrown when a shell command executed on a device takes too long to send its output. + *

The command may not actually be unresponsive, it just has spent too much time not outputting + * any thing to the console. + */ +public class +ShellCommandUnresponsiveException +extends IOException +{ + + /** + * + */ + private static final long serialVersionUID = 8123258430753698798L; +} diff --git a/org.tizen.common.sdblib/src/org/tizen/sdblib/TimeoutException.java b/org.tizen.common.sdblib/src/org/tizen/sdblib/TimeoutException.java new file mode 100755 index 0000000..e8803de --- /dev/null +++ b/org.tizen.common.sdblib/src/org/tizen/sdblib/TimeoutException.java @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2010 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.tizen.sdblib; + +import java.io.IOException; + +/** + * Exception thrown when a connection to sdb failed with a timeout. + * + */ +public class +TimeoutException +extends IOException +{ + + /** + * + */ + private static final long serialVersionUID = -4311065054176283079L; + +} -- 2.7.4