Immutable Stream implementation for byte arrays
Inheritance: Stream
Example #1
0
        public void TestByteStream()
        {
            byte[] data = new byte[1024];
            for(int i = 0; i<1024; i++)
                data[i] = (byte)(i%256);

            ByteStream bStream = new ByteStream(data);

            byte[] buffer = new byte[512];
            bStream.Read(buffer, 0, 512);
            for(int bufferIndex = 0; bufferIndex<buffer.Length; bufferIndex++)
            {
                if(buffer[bufferIndex] != bufferIndex % 256)
                    throw new Exception("Expecting " + (bufferIndex).ToString());
            }

            bStream.Seek(0, SeekOrigin.Begin);
            if(bStream.ReadByte() != 0)
                throw new Exception("Expecting expected 0");

            if(bStream.ReadByte() != 1)
                throw new Exception("Expecting expected 1");

            bStream.Seek(510, SeekOrigin.Current);
            bStream.Read(buffer, 0, 512);
            for(int i = 0; i<buffer.Length; i++)
            {
                if(buffer[i] != (i+512)%256)
                    throw new Exception("Expecting " + (i+512).ToString());
            }
        }
Example #2
0
        /// <summary>
        /// Executes this Request and waits for the response (if this request awaits response)
        /// </summary>
        public virtual SubsystemResponse Execute()
        {
            if(ResponseType == null || ResponseType == typeof(NoResponse))
            {
                _ctx.PacketTransmitter.TransmitWithoutResponse(ConvertToDataPacket());
                return null;
            }
            else
            {
                DataPacket responseDataPacket = _ctx.PacketTransmitter.TransmitWithResponse(ConvertToDataPacket());
                if(responseDataPacket.IsResponse == false)
                    throw new NotSupportedException("Received response packet with isResponse==false, something went wrong");

                using(ByteStream src = new ByteStream(responseDataPacket.Payload))
                {
                    //Reads the RequestIdentifier, but this is not needed for response packets
                    StreamHelper.ReadUInt16(src);

                    SubsystemResponse response = CreateResponse();
                    response.Read(src);
                    return response;
                }
            }
        }