コード例 #1
0
ファイル: AdbHelper.cs プロジェクト: Xtremrules/dot42
		/// <summary>
		/// Reads from the socket until the array is filled, the optional length
		/// is reached, or no more data is coming (because the socket closed or the
		/// timeout expired). After "timeout" milliseconds since the
		/// previous successful read, this will return whether or not new data has
		/// been found.
		/// </summary>
		/// <param name="chan"> the opened socket to read from. It must be in non-blocking
		///      mode for timeouts to work </param>
		/// <param name="data"> the buffer to store the read data into. </param>
		/// <param name="length"> the length to read or -1 to fill the data buffer completely </param>
		/// <param name="timeout"> The timeout value. A timeout of zero means "wait forever". </param>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: static void read(java.nio.channels.SocketChannel chan, byte[] data, int length, int timeout) throws TimeoutException, java.io.IOException
		internal static void read(SocketChannel chan, byte[] data, int length, int timeout)
		{
			ByteBuffer buf = ByteBuffer.wrap(data, 0, length != -1 ? length : data.Length);
			int numWaits = 0;

			while (buf.position != buf.limit)
			{
				int count;

				count = chan.read(buf);
				if (count < 0)
				{
					Log.d("ddms", "read: channel EOF");
					throw new IOException("EOF");
				}
				else if (count == 0)
				{
					// TODO: need more accurate timeout?
					if (timeout != 0 && numWaits * WAIT_TIME > timeout)
					{
						Log.d("ddms", "read: timeout");
						throw new TimeoutException();
					}
					// non-blocking spin
                    Thread.Sleep(WAIT_TIME);
					numWaits++;
				}
				else
				{
					numWaits = 0;
				}
			}
		}
コード例 #2
0
ファイル: DeviceMonitor.cs プロジェクト: Xtremrules/dot42
        /// <summary>
        /// Fills a buffer from a socket. </summary>
        /// <param name="socket"> </param>
        /// <param name="buffer"> </param>
        /// <returns> the content of the buffer as a string, or null if it failed to convert the buffer. </returns>
        /// <exception cref="IOException"> </exception>
        //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
        //ORIGINAL LINE: private String read(java.nio.channels.SocketChannel socket, byte[] buffer) throws java.io.IOException
        private string read(SocketChannel socket, byte[] buffer)
        {
            ByteBuffer buf = ByteBuffer.wrap(buffer, 0, buffer.Length);

            while (buf.position != buf.limit)
            {
                int count;

                count = socket.read(buf);
                if (count < 0)
                {
                    throw new IOException("EOF");
                }
            }

            try
            {
                return buffer.getString(0, buf.position, AdbHelper.DEFAULT_ENCODING);
            }
            catch (ArgumentException)
            {
                // we'll return null below.
            }

            return null;
        }