public int Read(byte[] buffer, int offset, int count) { if (offset < 0) { throw new ArgumentException("Offset must be greater than or equal to 0"); } if (offset == 0) { return((int)_inputStream.Read(buffer, (nuint)count)); } return((int)_inputStream.Read(buffer, offset, (nuint)count)); }
public void HandleInputEvent(object sender, NSStreamEventArgs e) { Console.WriteLine("***** Input ***** " + e.StreamEvent); if (e.StreamEvent == NSStreamEvent.HasBytesAvailable) { var buffer = new byte[1024]; nint bytesRead = inputStream.Read(buffer, 3); var stringRead = Encoding.UTF8.GetString(buffer); Console.WriteLine("***** String ***** " + stringRead); } }
private byte[] ReadFromInputStream(NSInputStream stream, string clientUid) { var header = new MqttFixedHeader(); var headerByte = new byte[1]; nint receivedSize; // Read the fixed header do { receivedSize = stream.Read(headerByte, 0, (nuint)headerByte.Length); } while (receivedSize > 0 && header.AppendByte(headerByte[0])); if (!header.IsComplete) { _logger.LogMessage("Socket", LogLevel.Error, string.Format("Read header operation could not read header, aborting.")); return(null); } _logger.LogMessage("Socket", LogLevel.Verbose, string.Format("Received message header type '{0}' from client {1}.", header.MessageType, clientUid)); //_logger.LogMessage("Socket", LogLevel.Warning, // string.Format("Received message header=0x{0:X}, Remaining length={1}.", header.Buffer[0], header.RemainingLength)); // Create a buffer and read the remaining message var completeBuffer = header.CreateMessageBuffer(); receivedSize = 0; while (receivedSize < header.RemainingLength) { receivedSize += stream.Read(completeBuffer, header.HeaderSize + (int)receivedSize, (nuint)(header.RemainingLength - receivedSize)); } //_logger.LogMessage("Socket", LogLevel.Warning, // string.Format(" Bytes read= {0}.", receivedSize)); return(completeBuffer); }
//Download File public void DownloadFile(CRCloudMetaData metaData) { new System.Threading.Thread(new System.Threading.ThreadStart(() => { InvokeOnMainThread(() => { CloudStorageLogic cloudStorageLogic = new CloudStorageLogic(); NSInputStream inputStream = cloudStorageLogic.DownloadFileWithPath(_cloudStorage, metaData.Path); NSMutableData data = new NSMutableData(); inputStream.Open(); var int32Value = metaData.Size.Int32Value; var intValue = metaData.Size.NIntValue; var buffer = new byte[1024]; while (inputStream.HasBytesAvailable()) { var len = inputStream.Read(buffer, 1024); data.AppendBytes(buffer); } if (inputStream.HasBytesAvailable() == false) { inputStream.Close(); var documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); var bytes = data.Bytes; string localFilename = metaData.Name; string localPath = Path.Combine(documents, localFilename); byte[] managedArray = new byte[intValue]; Marshal.Copy(bytes, managedArray, 0, int32Value); File.WriteAllBytes(localPath, managedArray); helper.Alert("Download Completed", "File downloaded and saved to file sharing", _controller); } }); })).Start(); }
public unsafe void Read() { using (var data = NSData.FromArray(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 })) { using (var s = NSInputStream.FromData(data)) { byte[] arr = new byte[10]; s.Open(); Assert.IsTrue(s.HasBytesAvailable()); Assert.AreEqual((nint)2, s.Read(arr, 2), "#a 1"); Assert.AreEqual(0, arr [0], "#a[0]"); Assert.AreEqual(1, arr [1], "#a[1]"); } using (var s = new NSInputStream(data)) { byte[] arr = new byte[10]; s.Open(); Assert.IsTrue(s.HasBytesAvailable()); Assert.AreEqual((nint)2, s.Read(arr, 1, 2), "#b 1"); Assert.AreEqual(0, arr [0], "#b[0]"); Assert.AreEqual(0, arr [1], "#b[1]"); Assert.AreEqual(1, arr [2], "#b[2]"); } using (var s = new NSInputStream(data)) { byte[] arr = new byte[10]; s.Open(); Assert.IsTrue(s.HasBytesAvailable()); fixed(byte *ptr = &arr[2]) Assert.AreEqual((nint)2, s.Read((IntPtr)ptr, 2), "#c 1"); Assert.AreEqual(0, arr [0], "#c[0]"); Assert.AreEqual(0, arr [1], "#c[1]"); Assert.AreEqual(0, arr [2], "#c[2]"); Assert.AreEqual(1, arr [3], "#c[3]"); } } }