コード例 #1
0
ファイル: SyncService.cs プロジェクト: yuva2achieve/dot42
        /// <summary>
        /// Reads an error message from the opened <seealso cref="#mChannel"/>. </summary>
        /// <param name="result"> the current adb result. Must contain both FAIL and the length of the message. </param>
        /// <param name="timeOut">
        /// @return </param>
        /// <exception cref="TimeoutException"> in case of a timeout reading responses from the device. </exception>
        /// <exception cref="IOException"> </exception>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: private String readErrorMessage(byte[] result, final int timeOut) throws TimeoutException, java.io.IOException
//JAVA TO C# CONVERTER WARNING: 'final' parameters are not allowed in .NET:
        private string readErrorMessage(byte[] result, int timeOut)
        {
            if (checkResult(result, ID_FAIL))
            {
                int len = ArrayHelper.swap32bitFromArray(result, 4);

                if (len > 0)
                {
                    AdbHelper.read(mChannel, mBuffer, len, timeOut);

                    var message = mBuffer.getString(0, len, Encoding.Default.EncodingName);
                    Log.e("ddms", "transfer error: " + message);

                    return(message);
                }
            }

            return(null);
        }
コード例 #2
0
ファイル: SyncService.cs プロジェクト: yuva2achieve/dot42
        /// <summary>
        /// Returns the mode of the remote file. </summary>
        /// <param name="path"> the remote file </param>
        /// <returns> an Integer containing the mode if all went well or null
        ///      otherwise </returns>
        /// <exception cref="IOException"> </exception>
        /// <exception cref="TimeoutException"> in case of a timeout reading responses from the device. </exception>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: private Integer readMode(String path) throws TimeoutException, java.io.IOException
        private int?readMode(string path)
        {
            // create the stat request message.
            var msg = createFileReq(ID_STAT, path);

            AdbHelper.write(mChannel, msg, -1, DdmPreferences.timeOut);             // full length

            // read the result, in a byte array containing 4 ints
            // (id, mode, size, time)
            var statResult = new byte[16];

            AdbHelper.read(mChannel, statResult, -1, DdmPreferences.timeOut);             // full length

            // check we have the proper data back
            if (checkResult(statResult, ID_STAT) == false)
            {
                return(null);
            }

            // we return the mode (2nd int in the array)
            return(ArrayHelper.swap32bitFromArray(statResult, 4));
        }
コード例 #3
0
ファイル: SyncService.cs プロジェクト: yuva2achieve/dot42
        /// <summary>
        /// Push a single file </summary>
        /// <param name="localPath"> the local file to push </param>
        /// <param name="remotePath"> the remote file (length max is 1024) </param>
        /// <param name="monitor"> the monitor. The monitor must be started already.
        /// </param>
        /// <exception cref="SyncException"> if file could not be pushed </exception>
        /// <exception cref="IOException"> in case of I/O error on the connection. </exception>
        /// <exception cref="TimeoutException"> in case of a timeout reading responses from the device. </exception>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: private void doPushFile(String localPath, String remotePath, ISyncProgressMonitor monitor) throws SyncException, java.io.IOException, TimeoutException
        private void doPushFile(string localPath, string remotePath, ISyncProgressMonitor monitor)
        {
            byte[] msg;

            //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
            //ORIGINAL LINE: final int timeOut = DdmPreferences.getTimeOut();
            int timeOut = DdmPreferences.timeOut;

            try
            {
                var remotePathContent = remotePath.getBytes(AdbHelper.DEFAULT_ENCODING);

                if (remotePathContent.Length > REMOTE_PATH_MAX_LENGTH)
                {
                    throw new SyncException(SyncException.SyncError.REMOTE_PATH_LENGTH);
                }

                // create the header for the action
                //JAVA TO C# CONVERTER TODO TASK: Octal literals cannot be represented in C#:
                msg = createSendFileReq(ID_SEND, remotePathContent, 0644);
            }
            catch (ArgumentException e)
            {
                throw new SyncException(SyncException.SyncError.REMOTE_PATH_ENCODING, e);
            }

            // create the stream to read the file
            using (var fis = File.OpenRead(localPath))
            {
                // and send it. We use a custom try/catch block to make the difference between
                // file and network IO exceptions.
                AdbHelper.write(mChannel, msg, -1, timeOut);

                // create the buffer used to read.
                // we read max SYNC_DATA_MAX, but we need 2 4 bytes at the beginning.
                if (mBuffer == null)
                {
                    mBuffer = new byte[SYNC_DATA_MAX + 8];
                }
                Array.Copy(ID_DATA, 0, mBuffer, 0, ID_DATA.Length);

                // look while there is something to read
                while (true)
                {
                    // check if we're canceled
                    if (monitor.canceled == true)
                    {
                        throw new SyncException(SyncException.SyncError.CANCELED);
                    }

                    // read up to SYNC_DATA_MAX
                    int readCount = fis.Read(mBuffer, 8, SYNC_DATA_MAX);

                    if (readCount == -1)
                    {
                        // we reached the end of the file
                        break;
                    }

                    // now send the data to the device
                    // first write the amount read
                    ArrayHelper.swap32bitsToArray(readCount, mBuffer, 4);

                    // now write it
                    AdbHelper.write(mChannel, mBuffer, readCount + 8, timeOut);

                    // and advance the monitor
                    monitor.advance(readCount);
                }
            }

            // create the DONE message
            long time = Environment.TickCount / 1000;

            msg = createReq(ID_DONE, (int)time);

            // and send it.
            AdbHelper.write(mChannel, msg, -1, timeOut);

            // read the result, in a byte array containing 2 ints
            // (id, size)
            var result = new byte[8];

            AdbHelper.read(mChannel, result, -1, timeOut);         // full length

            if (checkResult(result, ID_OKAY) == false)
            {
                throw new SyncException(SyncException.SyncError.TRANSFER_PROTOCOL_ERROR, readErrorMessage(result, timeOut));
            }
        }
コード例 #4
0
ファイル: SyncService.cs プロジェクト: yuva2achieve/dot42
        /// <summary>
        /// Pulls a remote file </summary>
        /// <param name="remotePath"> the remote file (length max is 1024) </param>
        /// <param name="localPath"> the local destination </param>
        /// <param name="monitor"> the monitor. The monitor must be started already. </param>
        /// <exception cref="SyncException"> if file could not be pushed </exception>
        /// <exception cref="IOException"> in case of I/O error on the connection. </exception>
        /// <exception cref="TimeoutException"> in case of a timeout reading responses from the device. </exception>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: private void doPullFile(String remotePath, String localPath, ISyncProgressMonitor monitor) throws java.io.IOException, SyncException, TimeoutException
        private void doPullFile(string remotePath, string localPath, ISyncProgressMonitor monitor)
        {
            var pullResult = new byte[8];

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int timeOut = DdmPreferences.getTimeOut();
            int timeOut = DdmPreferences.timeOut;

            try
            {
                var remotePathContent = remotePath.getBytes(AdbHelper.DEFAULT_ENCODING);

                if (remotePathContent.Length > REMOTE_PATH_MAX_LENGTH)
                {
                    throw new SyncException(SyncException.SyncError.REMOTE_PATH_LENGTH);
                }

                // create the full request message
                var msg = createFileReq(ID_RECV, remotePathContent);

                // and send it.
                AdbHelper.write(mChannel, msg, -1, timeOut);

                // read the result, in a byte array containing 2 ints
                // (id, size)
                AdbHelper.read(mChannel, pullResult, -1, timeOut);

                // check we have the proper data back
                if (checkResult(pullResult, ID_DATA) == false && checkResult(pullResult, ID_DONE) == false)
                {
                    throw new SyncException(SyncException.SyncError.TRANSFER_PROTOCOL_ERROR, readErrorMessage(pullResult, timeOut));
                }
            }
            catch (ArgumentException e)
            {
                throw new SyncException(SyncException.SyncError.REMOTE_PATH_ENCODING, e);
            }

            // access the destination file
            // create the stream to write in the file. We use a new try/catch block to differentiate
            // between file and network io exceptions.
            FileStream fos = null;

            try
            {
                fos = File.Create(localPath);
            }
            catch (IOException e)
            {
                Log.e("ddms", string.Format("Failed to open local file {0} for writing, Reason: {1}", localPath, e));
                throw new SyncException(SyncException.SyncError.FILE_WRITE_ERROR);
            }

            using (fos)
            {
                // the buffer to read the data
                var data = new byte[SYNC_DATA_MAX];

                // loop to get data until we're done.
                while (true)
                {
                    // check if we're cancelled
                    if (monitor.canceled == true)
                    {
                        throw new SyncException(SyncException.SyncError.CANCELED);
                    }

                    // if we're done, we stop the loop
                    if (checkResult(pullResult, ID_DONE))
                    {
                        break;
                    }
                    if (checkResult(pullResult, ID_DATA) == false)
                    {
                        // hmm there's an error
                        throw new SyncException(SyncException.SyncError.TRANSFER_PROTOCOL_ERROR, readErrorMessage(pullResult, timeOut));
                    }
                    int length = ArrayHelper.swap32bitFromArray(pullResult, 4);
                    if (length > SYNC_DATA_MAX)
                    {
                        // buffer overrun!
                        // error and exit
                        throw new SyncException(SyncException.SyncError.BUFFER_OVERRUN);
                    }

                    // now read the length we received
                    AdbHelper.read(mChannel, data, length, timeOut);

                    // get the header for the next packet.
                    AdbHelper.read(mChannel, pullResult, -1, timeOut);

                    // write the content in the file
                    fos.Write(data, 0, length);

                    monitor.advance(length);
                }

                fos.Flush();
            }
        }