private void doPullFile(String remotePath,String localPath,ISyncProgressMonitor monitor) throws IOException, SyncException, TimeoutException {
  byte[] msg;
  byte[] pullResult=new byte[8];
  final int timeOut=DdmPreferences.getTimeOut();
  try {
    byte[] remotePathContent=remotePath.getBytes(AdbHelper.DEFAULT_ENCODING);
    if (remotePathContent.length > REMOTE_PATH_MAX_LENGTH) {
      throw new SyncException(SyncError.REMOTE_PATH_LENGTH);
    }
    msg=createFileReq(ID_RECV,remotePathContent);
    AdbHelper.write(mChannel,msg,-1,timeOut);
    AdbHelper.read(mChannel,pullResult,-1,timeOut);
    if (!checkResult(pullResult,ID_DATA) && !checkResult(pullResult,ID_DONE)) {
      throw new SyncException(SyncError.TRANSFER_PROTOCOL_ERROR,readErrorMessage(pullResult,timeOut));
    }
  }
 catch (  UnsupportedEncodingException e) {
    throw new SyncException(SyncError.REMOTE_PATH_ENCODING,e);
  }
  File f=new File(localPath);
  try (FileOutputStream fos=new FileOutputStream(f)){
    byte[] data=new byte[SYNC_DATA_MAX];
    while (true) {
      if (monitor.isCanceled()) {
        throw new SyncException(SyncError.CANCELED);
      }
      if (checkResult(pullResult,ID_DONE)) {
        break;
      }
      if (!checkResult(pullResult,ID_DATA)) {
        throw new SyncException(SyncError.TRANSFER_PROTOCOL_ERROR,readErrorMessage(pullResult,timeOut));
      }
      int length=ArrayHelper.swap32bitFromArray(pullResult,4);
      if (length > SYNC_DATA_MAX) {
        throw new SyncException(SyncError.BUFFER_OVERRUN);
      }
      AdbHelper.read(mChannel,data,length,timeOut);
      AdbHelper.read(mChannel,pullResult,-1,timeOut);
      fos.write(data,0,length);
      monitor.advance(length);
    }
    fos.flush();
  }
 catch (  IOException e) {
    Log.e("ddms",String.format("Failed to open local file %s for writing, Reason: %s",f.getAbsolutePath(),e.toString()));
    throw new SyncException(SyncError.FILE_WRITE_ERROR);
  }
}

private void doPullFile ( String remotePath , String localPath , ISyncProgressMonitor monitor ) throws IOException , SyncException , TimeoutException { byte [ ] msg ; byte [ ] pullResult = new byte [ 8 ] ; final int timeOut = DdmPreferences . getTimeOut ( ) ; try { byte [ ] remotePathContent = remotePath . getBytes ( AdbHelper . DEFAULT_ENCODING ) ; if ( remotePathContent . length > REMOTE_PATH_MAX_LENGTH ) { throw new SyncException ( SyncError . REMOTE_PATH_LENGTH ) ; } msg = createFileReq ( ID_RECV , remotePathContent ) ; AdbHelper . write ( mChannel , msg , - 1 , timeOut ) ; AdbHelper . read ( mChannel , pullResult , - 1 , timeOut ) ; if ( ! checkResult ( pullResult , ID_DATA ) && ! checkResult ( pullResult , ID_DONE ) ) { throw new SyncException ( SyncError . TRANSFER_PROTOCOL_ERROR , readErrorMessage ( pullResult , timeOut ) ) ; } } catch ( UnsupportedEncodingException e ) { throw new SyncException ( SyncError . REMOTE_PATH_ENCODING , e ) ; } File f = new File ( localPath ) ; try ( FileOutputStream fos = new FileOutputStream ( f ) ) { byte [ ] data = new byte [ SYNC_DATA_MAX ] ; while ( true ) { if ( monitor . isCanceled ( ) ) { throw new SyncException ( SyncError . CANCELED ) ; } if ( checkResult ( pullResult , ID_DONE ) ) { break ; } if ( ! checkResult ( pullResult , ID_DATA ) ) { throw new SyncException ( SyncError . TRANSFER_PROTOCOL_ERROR , readErrorMessage ( pullResult , timeOut ) ) ; } int length = ArrayHelper . swap32bitFromArray ( pullResult , 4 ) ; if ( length > SYNC_DATA_MAX ) { throw new SyncException ( SyncError . BUFFER_OVERRUN ) ; } AdbHelper . read ( mChannel , data , length , timeOut ) ; AdbHelper . read ( mChannel , pullResult , - 1 , timeOut ) ; fos . write ( data , 0 , length ) ; monitor . advance ( length ) ; } fos . flush ( ) ; } catch ( IOException e ) { Log . e ( "ddms" , String . format ( "Failed to open local file %s for writing, Reason: %s" , f . getAbsolutePath ( ) , e . toString ( ) ) ) ; throw new SyncException ( SyncError . FILE_WRITE_ERROR ) ; } }
pulls a remote file
