Today, we will continue to analyze NIO non-blocking communication methods with the Android DDMS source code. Android123 will also share with you some of the technologies used in mobile phone and PC intercommunication. The Usage Details of SocketChannel and ByteBuffer in NIO can be found in today's article
Static void read (SocketChannel chan, byte [] data, int length, int timeout)
Throws TimeoutException, IOException {
ByteBuffer buf = ByteBuffer. wrap (data, 0, length! =-1? Length: data. length); // instantiate ByteBuffer from the byte array
Int numWaits = 0;
While (buf. position ()! = Buf. limit () {// receives data cyclically
Int count;
Count = chan. read (buf );
If (count <0 ){
Throw new IOException ("EOF"); // read to the end
} Else if (count = 0 ){
If (timeout! = 0 & numWaits * WAIT_TIME> timeout ){
Throw new TimeoutException ();
}
Try {
Thread. sleep (WAIT_TIME );
} Catch (InterruptedException ie ){
}
NumWaits ++;
} Else {
NumWaits = 0;
}
}
}
For SocketChannel write operations, the data sending code is as follows:
Static void write (SocketChannel chan, byte [] data, int length, int timeout)
Throws TimeoutException, IOException {
ByteBuffer buf = ByteBuffer. wrap (data, 0, length! =-1? Length: data. length );
Int numWaits = 0;
While (buf. position ()! = Buf. limit ()){
Int count;
Count = chan. write (buf); // send data from ByteBuffer
If (count <0 ){
Throw new IOException ("channel EOF ");
} Else if (count = 0 ){
If (timeout! = 0 & numWaits * WAIT_TIME> timeout ){
Throw new TimeoutException ();
}
Try {
Thread. sleep (WAIT_TIME );
} Catch (InterruptedException ie ){
}
NumWaits ++;
} Else {
NumWaits = 0;
}
}
}
You can use the setDevice method to select a specific device for ADB. In this way, when there is a simulator in the computer or multiple mobile phones are connected, you can select the device to be communicated through the device serial number.
Static void setDevice (SocketChannel adbChan, IDevice device)
Throws TimeoutException, AdbCommandRejectedException, IOException {
// If the device is not-1, then we first tell adb we're re looking to talk
// To a specific device
If (device! = Null ){
String msg = "host: transport:" + device. getSerialNumber (); // The last obtained serial number. android123 indicates that the adb get-serialno command is used.
Byte [] device_query = formAdbRequest (msg );
Write (adbChan, device_query );
AdbResponse resp = readAdbResponse (adbChan, false/* readDiagString */);
If (resp. okay = false ){
Throw new AdbCommandRejectedException (resp. message,
True/* errorDuringDeviceSelection */);
}
}
}
The code used to control the restart of the mobile phone through the PC. Of course, this requires the Root permission to execute
Public static void reboot (String into, InetSocketAddress adbSockAddr,
Device device) throws TimeoutException, AdbCommandRejectedException, IOException {
Byte [] request;
If (into = null ){
Request = formAdbRequest ("reboot:"); // $ NON-NLS-1 $
} Else {
Request = formAdbRequest ("reboot:" + into); // $ NON-NLS-1 $
}
SocketChannel adbChan = null;
Try {
AdbChan = SocketChannel. open (adbSockAddr );
AdbChan. configureBlocking (false );
// If the device is not-1, then we first tell adb we're re looking to talk
// To a specific device
SetDevice (adbChan, device );
Write (adbChan, request );
} Finally {
If (adbChan! = Null ){
AdbChan. close ();
}
}
}
We can see that each command is executed using a separate SocketChannel in non-blocking mode, which greatly enhances concurrency. Therefore, DDMS can process Logcat printing and display heap information, processing file management and so on. Concerning NIO server content, Android Development Network will focus on analyzing MonitorThread. java, let's talk about the NIO framework together.