示例#1
0
        private void insertFileHeader(bool compressContent)
        {
            OutStream.WriteByte(0x58);             // X
            OutStream.WriteByte(0x4e);             // N
            OutStream.WriteByte(0x42);             // B

            OutStream.WriteByte(getPlatformCode(targetPlatform));
            OutStream.WriteByte(getVersionCode());
            OutStream.WriteByte(getFormatCode(compressContent));

            Write(0);                   // Dummy the number of bytes
        }
示例#2
0
        public void testCorruptStream()
        {
            OutputCollector  collect = new OutputCollector();
            CompressionCodec codec   = new ZlibCodec();
            OutStream        @out    = new OutStream("test", 500, codec, collect);

            PositionCollector[] positions = new PositionCollector[1024];
            for (int i = 0; i < 1024; ++i)
            {
                positions[i] = new PositionCollector();
                @out.getPosition(positions[i]);
                @out.WriteByte((byte)i);
            }
            @out.Flush();

            // now try to read the stream with a buffer that is too small
            ByteBuffer inBuf = ByteBuffer.allocate(collect.buffer.size());

            collect.buffer.setByteBuffer(inBuf, 0, collect.buffer.size());
            inBuf.flip();
            InStream @in = InStream.create(null, "test", new ByteBuffer[] { inBuf },
                                           new long[] { 0 }, inBuf.remaining(), codec, 100);

            byte[] contents = new byte[1024];
            try
            {
                @in.Read(contents, 0, contents.Length);
                fail();
            }
            catch (IllegalArgumentException iae)
            {
                // EXPECTED
            }

            // make a corrupted header
            inBuf.clear();
            inBuf.put((byte)32);
            inBuf.put((byte)0);
            inBuf.flip();
            @in = InStream.create(null, "test2", new ByteBuffer[] { inBuf }, new long[] { 0 },
                                  inBuf.remaining(), codec, 300);
            try
            {
                @in.ReadByte();
                fail();
            }
            catch (InvalidOperationException ise)
            {
                // EXPECTED
            }
        }
示例#3
0
        public void testUncompressed()
        {
            OutputCollector collect = new OutputCollector();
            OutStream       @out    = new OutStream("test", 100, null, collect);

            PositionCollector[] positions = new PositionCollector[1024];
            for (int i = 0; i < 1024; ++i)
            {
                positions[i] = new PositionCollector();
                @out.getPosition(positions[i]);
                @out.WriteByte((byte)i);
            }
            @out.Flush();
            Assert.Equal(1024, collect.buffer.size());
            for (int i = 0; i < 1024; ++i)
            {
                Assert.Equal((byte)i, collect.buffer.get(i));
            }
            ByteBuffer inBuf = ByteBuffer.allocate(collect.buffer.size());

            collect.buffer.setByteBuffer(inBuf, 0, collect.buffer.size());
            inBuf.flip();
#pragma warning disable 612
            InStream @in = InStream.create(null, "test", new ByteBuffer[] { inBuf },
                                           new long[] { 0 }, inBuf.remaining(), null, 100);
#pragma warning restore 612
            Assert.Equal("uncompressed stream test position: 0 length: 1024" +
                         " range: 0 offset: 0 limit: 0",
                         @in.ToString());
            for (int i = 0; i < 1024; ++i)
            {
                int x = @in.ReadByte();
                Assert.Equal(i & 0xff, x);
            }
            for (int i = 1023; i >= 0; --i)
            {
                @in.seek(positions[i]);
                Assert.Equal(i & 0xff, @in.ReadByte());
            }
        }
示例#4
0
        public void testCompressed()
        {
            OutputCollector  collect = new OutputCollector();
            CompressionCodec codec   = new ZlibCodec();
            OutStream        @out    = new OutStream("test", 300, codec, collect);

            PositionCollector[] positions = new PositionCollector[1024];
            for (int i = 0; i < 1024; ++i)
            {
                positions[i] = new PositionCollector();
                @out.getPosition(positions[i]);
                @out.WriteByte((byte)i);
            }
            @out.Flush();
            Assert.Equal("test", @out.ToString());
            Assert.Equal(961, collect.buffer.size());
            ByteBuffer inBuf = ByteBuffer.allocate(collect.buffer.size());

            collect.buffer.setByteBuffer(inBuf, 0, collect.buffer.size());
            inBuf.flip();
            InStream @in = InStream.create(null, "test", new ByteBuffer[] { inBuf },
                                           new long[] { 0 }, inBuf.remaining(), codec, 300);

            Assert.Equal("compressed stream test position: 0 length: 961 range: 0" +
                         " offset: 0 limit: 0 range 0 = 0 to 961",
                         @in.ToString());
            for (int i = 0; i < 1024; ++i)
            {
                int x = @in.ReadByte();
                Assert.Equal(i & 0xff, x);
            }
            Assert.Equal(0, @in.available());
            for (int i = 1023; i >= 0; --i)
            {
                @in.seek(positions[i]);
                Assert.Equal(i & 0xff, @in.ReadByte());
            }
        }
示例#5
0
 public void WriteNullTerminatedString(string value)
 {
     Write(encoding.GetBytes(value));
     OutStream.WriteByte(0);
 }
示例#6
0
 public void WriteNull()
 {
     OutStream.WriteByte(0);
 }
示例#7
0
文件: Packet.cs 项目: dzamkov/DUIP
        /// <summary>
        /// Writes a packet to a stream.
        /// </summary>
        public static void Write(Packet Packet, OutStream Stream)
        {
            // Build flags and header
            PacketFlags flags = PacketFlags.Empty;
            if (Packet.AcknowledgementNumber.HasValue)
                flags |= PacketFlags.Acknowledgement;
            if (Packet.RoundTripTime.HasValue)
                flags |= PacketFlags.RoundTripTime;
            if (Packet.ChunkData != null)
            {
                flags |= PacketFlags.Chunk;
                if (Packet.ChunkInitial)
                    flags |= PacketFlags.ChunkInitial;
                if (Packet.ChunkFinal)
                    flags |= PacketFlags.ChunkFinal;
            }
            if (Packet.PingRequest)
                flags |= PacketFlags.PingRequest;
            if (Packet.PingResponse)
                flags |= PacketFlags.PingResponse;
            if (Packet.Disconnect)
                flags |= PacketFlags.Disconnect;
            Stream.WriteByte((byte)flags);
            Stream.WriteInt(Packet.SequenceNumber);

            // Additional information
            int ack;
            if (Packet.AcknowledgementNumber.TryGetValue(out ack))
                Stream.WriteInt(ack);

            double rtt;
            if (Packet.RoundTripTime.TryGetValue(out rtt))
                Stream.WriteDouble(rtt);

            // Chunk
            if (Packet.ChunkData != null)
                Stream.Write(Packet.ChunkData, 0, Packet.ChunkData.Length);
        }
示例#8
0
文件: Message.cs 项目: dzamkov/DUIP
 /// <summary>
 /// Writes the given message to a stream.
 /// </summary>
 public static void Write(Message Message, OutStream Stream)
 {
     MessageType type = MessageType.ForType(Message.GetType());
     Stream.WriteByte(type.ID);
     type.Write(Message, Stream);
 }