1 // 2 // Copyright (c) 2010-2018 Antmicro 3 // Copyright (c) 2011-2015 Realtime Embedded 4 // 5 // This file is licensed under the MIT License. 6 // Full license text is available in 'licenses/MIT.txt'. 7 // 8 using System; 9 using System.IO; 10 11 namespace Antmicro.Renode.UserInterface 12 { 13 public class StreamToEventConverter : Stream 14 { StreamToEventConverter()15 public StreamToEventConverter() 16 { 17 } 18 19 public event Action<byte[]> BytesWritten; 20 Write(byte[] buffer, int offset, int count)21 public override void Write(byte[] buffer, int offset, int count) 22 { 23 if(IgnoreWrites) 24 { 25 return; 26 } 27 var bytesWritten = BytesWritten; 28 var realData = new byte[count]; 29 Array.Copy(buffer, offset, realData, 0, count); 30 bytesWritten(realData); 31 } 32 33 public bool IgnoreWrites { get; set; } 34 35 public override bool CanRead 36 { 37 get 38 { 39 return false; 40 } 41 } 42 43 public override bool CanSeek 44 { 45 get 46 { 47 return false; 48 } 49 } 50 51 public override bool CanWrite 52 { 53 get 54 { 55 return true; 56 } 57 } 58 59 public override long Length 60 { 61 get 62 { 63 throw new NotSupportedException(); 64 } 65 } 66 67 public override long Position 68 { 69 get 70 { 71 throw new NotSupportedException(); 72 } 73 set 74 { 75 throw new NotSupportedException(); 76 } 77 } 78 Flush()79 public override void Flush() 80 { 81 82 } 83 Read(byte[] buffer, int offset, int count)84 public override int Read(byte[] buffer, int offset, int count) 85 { 86 throw new NotSupportedException(); 87 } 88 Seek(long offset, SeekOrigin origin)89 public override long Seek(long offset, SeekOrigin origin) 90 { 91 throw new NotSupportedException(); 92 } 93 SetLength(long value)94 public override void SetLength(long value) 95 { 96 throw new NotSupportedException(); 97 } 98 } 99 } 100 101