public void SendStdout(int requestId, BufferData data) { if (!_socket.Writable) { return; } // Send data with at most 65535 bytes in one record if (data.ByteLength <= 65535) { SendRecord(RecordType.Stdout, requestId, data); } // Split data with more than 64KB into multiple records else { int left = data.ByteLength; var it = data.GetEnumerator(); it.MoveNext(); var current = it.Current; while (left > 0) { var size = Math.Min(left, 65535); var header = new byte[8]; WriteRecordHeader(header, RecordType.Stdout, requestId, size); _socket.Write(header); left -= size; while (size > 0) { if (current.Count <= size) { _socket.Write(current); size -= current.Count; it.MoveNext(); current = it.Current; } else { _socket.Write(new ArraySegment <byte>(current.Array, current.Offset, size - current.Count)); current = new ArraySegment <byte>(current.Array, current.Offset + size, current.Count - size); size = 0; } } } } }