public void ShouldSerializeDateTime(
			[Values(1, 10, 100, 10000)]
			int numberOfEntries)
        {
            var randomDateTimes = Helpers.GetRandomDateTimes(numberOfEntries);

            var stream = new MemoryStream();
            using(var writer = new PrimitiveWriter(stream))
            {
                for(var i = 0; i < randomDateTimes.Length; i++)
                {
                    writer.Write(randomDateTimes[i]);
                }
            }
            var position = stream.Position;
            stream.Seek(0, SeekOrigin.Begin);

            using(var reader = new PrimitiveReader(stream))
            {
                for(var i = 0; i < randomDateTimes.Length; i++)
                {
                    Assert.AreEqual(randomDateTimes[i], reader.ReadDateTime());
                }
            }
            Assert.AreEqual(position, stream.Position, StreamCorruptedMessage);
        }
Exemplo n.º 2
0
 public void Read( Mp3Container container, PrimitiveReader reader )
 {
     ReadData( container, reader );
     if( OnRead != null ) {
         OnRead( container, this );
     }
 }
		public void ShouldWriteAndReadInts(
			[Values(1, 10, 100, 10000, 1000*1000)]
			int numberOfInts)
		{
			var randomInts = Helpers.GetRandomIntegers(numberOfInts);

			var stream = new MemoryStream();
            using(var writer = new PrimitiveWriter(stream, buffered))
			{
				for(var i = 0; i < randomInts.Length; i++)
				{
					writer.Write(randomInts[i]);
				}
			}
			var position = stream.Position;
			stream.Seek(0, SeekOrigin.Begin);

            using(var reader = new PrimitiveReader(stream, buffered))
			{
				for(var i = 0; i < randomInts.Length; i++)
				{
					Assert.AreEqual(randomInts[i], reader.ReadInt32());
				}
			}
			Assert.AreEqual(position, stream.Position, StreamCorruptedMessage);
		}
Exemplo n.º 4
0
        void ReadV2( PrimitiveReader reader )
        {
            int len = dataSize;

            while( len >= 4 ) {
                if( ReadV2Frame( reader, ref len ) ) break;
            }
            reader.SkipData( len );
        }
Exemplo n.º 5
0
 void ReadID31( PrimitiveReader reader )
 {
     MusicInfo[MusicInfoKeys.Title] = ReadFixedASCIIString( reader, 30 );
     MusicInfo[MusicInfoKeys.Artist] = ReadFixedASCIIString( reader, 30 );
     MusicInfo[MusicInfoKeys.Album] = ReadFixedASCIIString( reader, 30 );
     MusicInfo[MusicInfoKeys.YearReleased] = ReadFixedASCIIString( reader, 4 );
     // TODO: check for track number in comment field
     string comment = ReadFixedASCIIString( reader, 30 );
     byte genre = reader.ReadByte();
 }
Exemplo n.º 6
0
        bool ReadV2Frame( PrimitiveReader reader, ref int len )
        {
            string frameId = reader.ReadASCIIString( 4 ); len -= 4;
            if( frameId == "\0\0\0\0" ) return true;

            int frameDataSize = reader.ReadUInt24(); len -= ( frameDataSize + 3 );
            Console.WriteLine( "reading frame: " + frameId );
            Console.WriteLine( "   skipping frame." );
            reader.SkipData( frameDataSize );
            return false;
        }
Exemplo n.º 7
0
 void ReadID31Extended( PrimitiveReader reader )
 {
     // TODO: Concat these 3 strings with their ID31.1 strings
     string title = ReadFixedASCIIString( reader, 60 );
     string artist = ReadFixedASCIIString( reader, 60 );
     string album = ReadFixedASCIIString( reader, 60 );
     byte speed = reader.ReadByte();
     string genre = ReadFixedASCIIString( reader, 30 );
     string startTime = ReadFixedASCIIString( reader, 6 );
     string endTime = ReadFixedASCIIString( reader, 6 );
 }
Exemplo n.º 8
0
        public override void ReadMetadata()
        {
            PrimitiveReader reader = new PrimitiveReader( this );
            string signature = reader.ReadASCIIString( 4 );

            if( signature != "fLaC" ) {
                throw new InvalidDataException( "Invalid signature." );
            }
            reader.BigEndian = true;
            while( !ReadMetadataBlock( reader ) );
        }
Exemplo n.º 9
0
        void ReadV3( PrimitiveReader reader )
        {
            int len = dataSize;
            bool extendedHeader = ( headerFlags & 0x40 ) != 0;
            if( extendedHeader ) {
                ReadV3ExtendedHeader( reader, ref len );
            }

            while( len >= 4 ) {
                if( ReadV3Frame( reader, ref len ) ) break;
            }
            reader.SkipData( len );
        }
Exemplo n.º 10
0
 public TransformerCodec( uint dataSize, PrimitiveReader reader, int frequency, int channels,
     int bitsPerSample, Transformer transformer, int codecBitsPerSample)
 {
     info = new AudioChunk();
     this.dataSize = dataSize;
     this.reader = reader;
     info.Frequency = frequency;
     info.Channels = channels;
     info.BitsPerSample = bitsPerSample;
     this.transformer = transformer;
     this.codecBitsPerSample = codecBitsPerSample;
     bufferSize = (int)( frequency * channels * codecBitsPerSample / 8 );
 }
Exemplo n.º 11
0
        public IEnumerable<AudioChunk> StreamData( Stream source )
        {
            infoBuffer.Length = 0;
            info = new AudioChunk();
            PrimitiveReader reader = new PrimitiveReader( source );
            string signature = reader.ReadASCIIString( 4 );
            if( signature != "FORM" )
                throw new InvalidDataException( "Invalid initial signature." );

            reader.BigEndian = true;
            int formChunkSize = reader.ReadInt32();
            AppendInfoLine( 0, "-- Begin info --" );
            AppendInfoLine( 0, "{0} (Chunk size: {1} bytes, {2} KB, {3} MB)", signature,
                           formChunkSize, formChunkSize / 1024, formChunkSize / 1024 / 1024 );

            string format = reader.ReadASCIIString( 4 );
            switch( format ) {
                case "AIFF":
                    break;

                    //http://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/AIFF/Docs/AIFF-C.9.26.91.pdf
                case "AIFC":
                    aifcFormat = true;
                    break;

                default:
                    throw new InvalidDataException( "Invalid initial signature." );
            }

            AiffChunkHeader chunk;
            while( true ) {
                chunk = ReadChunkHeader( reader );
                AppendInfoLine( 1, "{0} (Chunk size: {1})", chunk.Signature, chunk.DataSize );
                if( chunk.Signature == "COMM" ) {
                    ProcessCommonChunk( chunk, reader );
                } else if( chunk.Signature == "SSND" ) {
                    break;
                } else {
                    SkipChunkData( reader, chunk.DataSize );
                }
            }
            AppendInfoLine( 0, "-- End info --" );
            fileinfo = infoBuffer.ToString();

            return StreamDataCore( chunk, reader );
        }
Exemplo n.º 12
0
 public override void ReadMetadata()
 {
     PrimitiveReader reader = new PrimitiveReader( this );
     while( true ) {
         string header = reader.ReadASCIIString( 4 );
         if( header == "TAG+" ) { // ID3 1.1 Extended tag
             ReadID31Extended( reader );
         } else if( header.StartsWith( "TAG" ) ) { // ID3 1
             reader.Seek( -1, SeekOrigin.Current );
             ReadID31( reader );
         } else if( header.StartsWith( "ID3" ) ) { // ID3 2
             ID3v2Frame tag = new ID3v2Frame( this, reader, (byte)header[3] );
         } else {
             // Unrecognised header, it's probably the start of the actual MPEG audio stream.
             reader.Seek( -4, SeekOrigin.Current );
             break;
         }
     }
 }
Exemplo n.º 13
0
        public override void ReadMetadata()
        {
            reader = new PrimitiveReader( stream );
            string signature = reader.ReadASCIIString( 4 );
            bool bigendian = false, reverseHeaders = false;

            if( signature == "RIFF" ) {
            } else if( signature == "RIFX" ) {
                bigendian = true;
            } else if( signature == "FFIR" ) {
                bigendian = true;
                reverseHeaders = true;
            } else {
                throw new InvalidDataException( "Invalid initial signature" );
            }

            reader.BigEndian = bigendian;
            int riffChunkSize = reader.ReadInt32();
            string format = reader.ReadASCIIString( 4 );
            if( !( format == "WAVE" || format == "EVAW" ) )
                throw new InvalidDataException( "Invalid format." );

            // Although technically a wave chunk is supposed to consist of
            // a 'fmt ' chunk followed by a 'data' chunk, this is not always the case.
            RiffChunkHeader chunk;
            while( true ) {
                chunk = ReadChunkHeader( reader, reverseHeaders );
                if( chunk.Signature == "fmt " ) {
                    ProcessFormatChunk( chunk, reader );
                    foundAudioInfo = true;
                } else if( chunk.Signature == "data" ) {
                    if( !foundAudioInfo ) {
                        Logger.Log( LoggingType.CodecError, "Data chunk found before format chunk.",
                                   "This usually indicates an improper encoder or a corrupted file." );
                        throw new InvalidOperationException( "Stream must be seekable when the data chunk is before the format chunk." );
                    }
                    break;
                } else {
                    reader.SkipData( chunk.DataSize );
                }
            }
            chunkDataSize = chunk.DataSize;
        }
Exemplo n.º 14
0
        public IEnumerable<AudioChunk> StreamData( Stream source )
        {
            infoBuffer.Length = 0;
            chunk = new AudioChunk();
            PrimitiveReader reader = new PrimitiveReader( source );
            string signature = reader.ReadASCIIString( 4 );
            if( signature != ".snd" ) {
                throw new InvalidDataException( "Invalid initial signature." );
            }

            AppendInfoLine( 0, "-- Begin info --" );
            reader.BigEndian = true;
            uint dataOffset = reader.ReadUInt32();
            AppendInfoLine( 0, "Data offset: {0}", dataOffset );
            uint dataSize = reader.ReadUInt32();
            if( dataSize == 0xFFFFFFFF ) {
                dataLength = source.Length - dataOffset;
            } else {
                dataLength = dataSize;
            }
            AppendInfoLine( 0, "Data length: {0}", dataLength );
            AuEncoding encoding = (AuEncoding)reader.ReadUInt32();
            AppendInfoLine( 0, "Encoding: {0}", encoding );
            uint sampleRate = reader.ReadUInt32();
            AppendInfoLine( 0, "Sample rate: {0}", sampleRate );
            uint channels = reader.ReadUInt32();
            AppendInfoLine( 0, "Channels: {0}", channels );
            if( dataOffset > 24 ) {
                int infoLength = (int)( dataOffset - 24 );
                string info = reader.ReadASCIIString( infoLength );
                AppendInfoLine( 0, "Info: {0}", info );
            }
            int bitsPerSample = bitsPerSampleEncoding[(int)encoding];
            int adjustedBitsPerSample = paddedBitsPerSampleEncoding[(int)encoding];
            bufferSize = (int)( sampleRate * channels * adjustedBitsPerSample / 8 );

            AppendInfoLine( 0, "-- End info --" );
            fileinfo = infoBuffer.ToString();

            return StreamDataCore( reader );
        }
Exemplo n.º 15
0
        private async Task <RawMessage> ReadRawAsync()
        {
            var mem = await ReadPacketBytesAsync();

            var reader = new PrimitiveReader(mem);

            if (Threshold == null)
            {
                return(ReadUncompressedRawMessage(reader));
            }

            var uncompressedLen = reader.ReadVarint();

            if (uncompressedLen < 0)
            {
                throw new ProtoException("Uncompressed length < 0");
            }
            else if (uncompressedLen == 0)
            {
                return(ReadUncompressedRawMessage(reader));
            }
            else
            {
                if (uncompressedLen < Threshold)
                {
                    throw new ProtoException("Received packet was compressed before threshold size was reached");
                }
                var uncBuf = new MemoryStream();
                ZlibDecompress(uncBuf, mem);
                if (uncBuf.Length != uncompressedLen)
                {
                    throw new ProtoException("Wrong uncompressed length was given in the received packet");
                }
                return(ReadUncompressedRawMessage(new PrimitiveReader(uncBuf)));
            }
        }
Exemplo n.º 16
0
 public void ShouldHandleNotAlignedWrites()
 {
     const int iterationCount = 80000;
     var stream = new MemoryStream();
     using(var writer = new PrimitiveWriter(stream))
     {
         writer.Write((byte)1);
         for(var i = 0; i < iterationCount; i++)
         {
             writer.Write(int.MaxValue);
         }
     }
     var position = stream.Position;
     stream.Seek(0, SeekOrigin.Begin);
     using(var reader = new PrimitiveReader(stream))
     {
         Assert.AreEqual((byte)1, reader.ReadByte());
         for(var i = 0; i < iterationCount; i++)
         {
             Assert.AreEqual(int.MaxValue, reader.ReadInt32());
         }
     }
     Assert.AreEqual(position, stream.Position, StreamCorruptedMessage);
 }
Exemplo n.º 17
0
        public ID3v2Frame( Mp3Container container, PrimitiveReader reader, byte majorVersion )
        {
            revision = reader.ReadByte();
            headerFlags = reader.ReadByte();
            dataSize = ReadSyncSafeInt32( reader );
            reader.BigEndian = true;
            this.container = container;
            this.majorVersion = majorVersion;

            bool useUnsynch = ( headerFlags & 0x80 ) != 0;
            if( useUnsynch ) {
                reader.stream = new UnsynchronisationStream( reader.stream );
            }

            if( majorVersion == 3 ) {
                ReadV3( reader );
            } else {
                throw new NotImplementedException( "Support for ID3 version " + majorVersion + " not yet done." );
            }

            if( useUnsynch ) {
                reader.stream = ( (UnsynchronisationStream)reader.stream ).UnderlyingStream;
            }
        }
Exemplo n.º 18
0
 public async ValueTask Read(PrimitiveReader primitiveReader, CancellationToken cancellationToken)
 {
     _ = await primitiveReader.ReadBool(cancellationToken);
 }
Exemplo n.º 19
0
 public override void Read(PrimitiveReader reader)
 {
     Position = new Vec3(reader.ReadDouble(), reader.ReadDouble(), reader.ReadDouble());
     OnGround = reader.ReadBool();
 }
Exemplo n.º 20
0
        public async ValueTask Read(PrimitiveReader primitiveReader, CancellationToken cancellationToken)
        {
            byte key = await primitiveReader.ReadByte(cancellationToken);

            await index[key].Read(primitiveReader, cancellationToken);
        }
Exemplo n.º 21
0
 public virtual void Load(PrimitiveReader reader)
 {
 }
 public void ShouldThrowIfStreamPrematurelyFinishes()
 {
     var streamToRead = new MemoryStream();
     var streamToWrite = new MemoryStream();
     using(var reader = new PrimitiveReader(streamToRead, buffered))
     {
         Assert.Throws<EndOfStreamException>(() => reader.CopyTo(streamToWrite, 100));
     }
 }
Exemplo n.º 23
0
        private static RawMessage ReadUncompressedRawMessage(PrimitiveReader reader)
        {
            var id = reader.ReadVarint();

            return(new RawMessage(id, reader));
        }
Exemplo n.º 24
0
 public override void Read(PrimitiveReader reader)
 {
     WindowId       = reader.ReadSByte();
     EnchantmentPos = reader.ReadByte();
 }
Exemplo n.º 25
0
 public override void Read(PrimitiveReader reader)
 {
     Slot = reader.ReadShort();
 }
Exemplo n.º 26
0
 public PrimitiveField(PrimitiveReader reader, PrimitiveWriter writer)
 {
     Reader = reader;
     Writer = writer;
 }
Exemplo n.º 27
0
 public override void Read(PrimitiveReader reader)
 {
     WindowId = reader.ReadByte();
 }
Exemplo n.º 28
0
 internal override void Read(PrimitiveReader reader, int _) => Value = reader.ReadSByte();
Exemplo n.º 29
0
 internal override void Read(PrimitiveReader reader, int _) => Value = reader.ReadStringNbt();
Exemplo n.º 30
0
 internal abstract void Read(PrimitiveReader reader, int level);
Exemplo n.º 31
0
 public override void Read(PrimitiveReader reader)
 {
     Slot        = reader.ReadShort();
     ClickedItem = reader.ReadItemStackProto();
 }
Exemplo n.º 32
0
 protected abstract void ReadData( Mp3Container container, PrimitiveReader reader );
Exemplo n.º 33
0
 public override void Read(PrimitiveReader reader)
 {
     Position = new Vec3(reader.ReadDouble(), reader.ReadDouble(), reader.ReadDouble());
     Yaw      = reader.ReadFloat();
     Pitch    = reader.ReadFloat();
 }
Exemplo n.º 34
0
 public override void Read(PrimitiveReader reader)
 {
     WindowId = reader.ReadSByte();
     RecipeId = reader.ReadVarint();
     MakeAll  = reader.ReadBool();
 }
Exemplo n.º 35
0
 public RawMessage(int id, PrimitiveReader reader)
 {
     Reader = reader;
     Id     = id;
 }
Exemplo n.º 36
0
        public void ShouldWriteAndReadGuid(
			[Values(10, 1000, 100000)]
			int count
			)
        {
            var stream = new MemoryStream();
            var array = new Guid[count];
            using(var writer = new PrimitiveWriter(stream))
            {
                for(var i = 0; i < count; i++)
                {
                    var guid = Guid.NewGuid();
                    array[i] = guid;
                    writer.Write(guid);
                }
            }
            var position = stream.Position;
            stream.Seek(0, SeekOrigin.Begin);

            using(var reader = new PrimitiveReader(stream))
            {
                for(var i = 0; i < count; i++)
                {
                    Assert.AreEqual(array[i], reader.ReadGuid());
                }
            }
            Assert.AreEqual(position, stream.Position, StreamCorruptedMessage);
        }
        public void ShouldWriteAndReadULongs(
            [Values(1, 10, 100, 10000, 1000*1000)]
            int numberOfULongs)
        {
            var randomULongs = Helpers.GetRandomLongs(numberOfULongs).Select(x=>(ulong)x).ToArray();
            var stream = new MemoryStream();
            using(var writer = new PrimitiveWriter(stream, buffered))
            {
                for(var i = 0; i < randomULongs.Length; i++)
                {
                    writer.Write(randomULongs[i]);
                }
            }
            var position = stream.Position;
            stream.Seek(0, SeekOrigin.Begin);

            using(var reader = new PrimitiveReader(stream, buffered))
            {
                for(var i = 0; i < randomULongs.Length; i++)
                {
                    var read = reader.ReadUInt64();
                    Assert.AreEqual(randomULongs[i], read);
                }
            }
            Assert.AreEqual(position, stream.Position, StreamCorruptedMessage);
        }
Exemplo n.º 38
0
        public void ShouldWriteAndReadPartsOfByteArrays()
        {
            var arrays = new byte[100][];
            for(var i = 0; i < arrays.Length; i++)
            {
                arrays[i] = Enumerable.Range(0, 256).Select(x => (byte)x).ToArray();
            }

            var stream = new MemoryStream();
            using(var writer = new PrimitiveWriter(stream))
            {
                for(var i = 0; i < arrays.Length; i++)
                {
                    writer.Write(arrays[i], i, arrays[i].Length - i);
                }
            }

            var position = stream.Position;
            stream.Seek(0, SeekOrigin.Begin);
            using(var reader = new PrimitiveReader(stream))
            {
                for(var i = 0; i < arrays.Length; i++)
                {
                    var writtenLength = arrays[i].Length - i;
                    var writtenArray = reader.ReadBytes(writtenLength);
                    var subarray = new byte[writtenLength];
                    Array.Copy(arrays[i], i, subarray, 0, writtenLength);
                    CollectionAssert.AreEqual(subarray, writtenArray);
                }
            }
            Assert.AreEqual(position, stream.Position, StreamCorruptedMessage);
        }
Exemplo n.º 39
0
 static string ReadFixedASCIIString( PrimitiveReader reader, int count )
 {
     byte[] data = reader.ReadBytes( count );
     int index = Array.IndexOf( data, (byte)0 );
     return Encoding.ASCII.GetString( data, 0, index == -1 ? count : index );
 }
Exemplo n.º 40
0
 protected override void ReadData( Mp3Container container, PrimitiveReader reader )
 {
     value = ReadNullTerminatedString( reader, ref DataSize );
     if( DataSize > 0 ) {
         reader.SkipData( DataSize );
     }
     Console.WriteLine( value );
 }
Exemplo n.º 41
0
 public static ChatRoot ReadChatRootProto(this PrimitiveReader reader)
 {
     return(JsonConvert.DeserializeObject <ChatRoot>(reader.ReadStringProto(ProtoConsts.MaxStringChars)));
 }
Exemplo n.º 42
0
 public override void Read(PrimitiveReader reader)
 {
     OnGround = reader.ReadBool();
 }
Exemplo n.º 43
0
 public override void Read(PrimitiveReader reader)
 {
     Message = reader.ReadStringProto(256);
 }
Exemplo n.º 44
0
 public override void Read(PrimitiveReader reader)
 {
     Name = reader.ReadStringProto(16);
 }
Exemplo n.º 45
0
 public override void Read(PrimitiveReader reader)
 {
     RightPaddleTurning = reader.ReadBool();
     LeftPaddleTurning  = reader.ReadBool();
 }
Exemplo n.º 46
0
 public override void Read(PrimitiveReader reader)
 {
     Payload = reader.ReadLong();
 }
Exemplo n.º 47
0
        protected string ReadNullTerminatedString( PrimitiveReader reader, ref int bytesLeft )
        {
            int index = 0;
            Encoding textEncoding = Encoding.GetEncoding( 28591 );
            byte[] stringData = new byte[bytesLeft];

            while( bytesLeft > 0 ) {
                byte part = reader.ReadByte();
                if( part == 0x00 ) break;

                stringData[index++] = part; bytesLeft--;
            }
            return textEncoding.GetString( stringData, 0, index );
        }
Exemplo n.º 48
0
 public override void Read(PrimitiveReader reader)
 {
     Channel = reader.ReadStringProto(20);
     Data    = new byte[reader.BaseStream.Length];
     reader.ReadFully(Data, 0, Data.Length);
 }
Exemplo n.º 49
0
        public override void ReadMetadata()
        {
            reader = new PrimitiveReader( stream );
            string signature = reader.ReadASCIIString( 4 );
            if( signature != ".snd" ) {
                throw new InvalidDataException( "Invalid initial signature." );
            }

            reader.BigEndian = true;
            uint dataOffset = reader.ReadUInt32();
            dataLength = reader.ReadUInt32();
            if( dataLength == 0xFFFFFFFF ) {
                dataLength = (uint)( reader.Length - dataOffset );
            }

            AuEncoding encoding = (AuEncoding)reader.ReadUInt32();
            Metadata["AU encoding"] = encoding.ToString();
            freq = reader.ReadInt32();
            Metadata[MetadataKeys.SampleRate] = freq.ToString();
            channels = reader.ReadInt32();
            Metadata[MetadataKeys.Channels] = channels.ToString();

            if( dataOffset > 24 ) {
                int infoLength = (int)( dataOffset - 24 );
                string info = reader.ReadASCIIString( infoLength );
                Metadata["File comment"] = info;
            }

            transformer = EmptyTransformer.Instance;
            switch( encoding ) {
                case AuEncoding.Int8G711uLaw:
                    transformer = MuLawTransformer.Instance;
                    bitsPerSample = 16; codecBitsPerSample = 8;
                    break;

                case AuEncoding.Int8LinearPcm:
                    bitsPerSample = codecBitsPerSample = 8;
                    break;

                case AuEncoding.Int16LinearPcm:
                    bitsPerSample = codecBitsPerSample = 16;
                    transformer = BigEndian16BitTo16BitTransformer.Instance;
                    break;

                case AuEncoding.Int24LinearPcm:
                    bitsPerSample = 16; codecBitsPerSample = 24;
                    transformer = BigEndian24BitTo16BitTransformer.Instance;
                    break;

                case AuEncoding.Int32LinearPcm:
                    bitsPerSample = 16; codecBitsPerSample = 32;
                    transformer = BigEndian32BitTo16BitTransformer.Instance;
                    break;

                case AuEncoding.Float32LinearPcm:
                    bitsPerSample = 16; codecBitsPerSample = 32;
                    transformer = BigEndianFloat32To16BitTransformer.Instance;
                    break;

                case AuEncoding.Float64LinearPcm:
                    bitsPerSample = 16; codecBitsPerSample = 64;
                    transformer = BigEndianFloat64To16BitTransformer.Instance;
                    break;

                case AuEncoding.Int8G711ALaw:
                    transformer = ALawTransformer.Instance;
                    bitsPerSample = 16; codecBitsPerSample = 8;
                    break;

                default:
                    throw new NotSupportedException( "Unsupported audio format: " + encoding );
            }
            Metadata[MetadataKeys.BitsPerSample] = bitsPerSample.ToString();
        }
Exemplo n.º 50
0
 public TypeStampReader(PrimitiveReader reader, VersionToleranceLevel versionToleranceLevel)
 {
     this.reader = reader;
     this.versionToleranceLevel = versionToleranceLevel;
     stampCache = new Dictionary <Type, List <FieldInfoOrEntryToOmit> >();
 }
Exemplo n.º 51
0
        protected string ReadNullTerminatedEncodedString( PrimitiveReader reader, ref int bytesLeft )
        {
            byte encoding = reader.ReadByte(); bytesLeft--;
            byte[] stringData = null;
            Encoding textEncoding = null;
            int index = 0;

            if( encoding == 0 ) {
                textEncoding = Encoding.GetEncoding( 28591 );
                stringData = new byte[bytesLeft];

                while( bytesLeft > 0 ) {
                    byte part = reader.ReadByte();
                    if( part == 0x00 ) break;

                    stringData[index++] = part; bytesLeft--;
                }
            } else if( encoding == 1 ) {
                ushort bom = reader.ReadUInt16(); bytesLeft -= 2;
                stringData = new byte[bytesLeft];

                if( bom == 0xFEFF ) {
                    textEncoding = Encoding.BigEndianUnicode;
                } else if( bom == 0xFFFE ) {
                    textEncoding = Encoding.Unicode;
                } else {
                    throw new InvalidDataException( "Invalid bom: " + bom );
                }

                while( bytesLeft > 0 ) {
                    byte part1 = reader.ReadByte();
                    byte part2 = reader.ReadByte();
                    if( part1 == 0x00 && part2 == 0x00 ) break;

                    stringData[index++] = part1; bytesLeft--;
                    stringData[index++] = part2; bytesLeft--;
                }
            } else {
                throw new NotSupportedException( "Unsupported encoding " + encoding );
            }
            return textEncoding.GetString( stringData, 0, index );
        }
Exemplo n.º 52
0
 public abstract void Read(PrimitiveReader reader);
Exemplo n.º 53
0
        public void ShouldWriteAndReadByteArray(
			[Values(10, 1000, 100000)]
			int count
			)
        {
            var stream = new MemoryStream();
            byte[] array;
            using(var writer = new PrimitiveWriter(stream))
            {
                array = new byte[count];
                Helpers.Random.NextBytes(array);
                writer.Write(array);
            }
            var position = stream.Position;
            stream.Seek(0, SeekOrigin.Begin);

            byte[] copy;
            using(var reader = new PrimitiveReader(stream))
            {
                copy = reader.ReadBytes(count);
            }
            CollectionAssert.AreEqual(array, copy);
            Assert.AreEqual(position, stream.Position, StreamCorruptedMessage);
        }
Exemplo n.º 54
0
 public override void Read(PrimitiveReader reader)
 {
     WindowId  = reader.ReadSByte();
     ActionNum = reader.ReadShort();
     Accepted  = reader.ReadBool();
 }
Exemplo n.º 55
0
        public void ShouldWriteAndReadNegativeInt()
        {
            var value = -Helpers.Random.Next();
            var stream = new MemoryStream();
            using(var writer = new PrimitiveWriter(stream))
            {
                writer.Write(value);
            }
            var position = stream.Position;
            stream.Seek(0, SeekOrigin.Begin);

            using(var reader = new PrimitiveReader(stream))
            {
                Assert.AreEqual(value, reader.ReadInt32());
            }
            Assert.AreEqual(position, stream.Position, StreamCorruptedMessage);
        }
Exemplo n.º 56
0
 public override void Read(PrimitiveReader reader)
 {
     KeepAliveId = reader.ReadVarint();
 }
Exemplo n.º 57
0
        public void ShouldWriteAndReadStrings(
			[Values(1, 100, 10000)]
			int numberOfStrings,
			[Values(true, false)]
			bool withLongStrings)
        {
            const int maxLength = 100;
            const int longStringLength = 8000;
            const int longStringProbability = 10;

            var random = Helpers.Random;
            var strings = new string[numberOfStrings];
            for(var i = 0; i < strings.Length; i++)
            {
                int length;
                if(withLongStrings && random.Next()%longStringProbability == 0)
                {
                    length  = longStringLength;
                }
                else
                {
                    length = random.Next(maxLength);
                }
                strings[i] = Helpers.GetRandomString(length);
            }

            var stream = new MemoryStream();
            using(var writer = new PrimitiveWriter(stream))
            {
                for(var i = 0; i < strings.Length; i++)
                {
                    writer.Write(strings[i]);
                }
            }
            var position = stream.Position;
            stream.Seek(0, SeekOrigin.Begin);

            using(var reader = new PrimitiveReader(stream))
            {
                for(var i = 0; i < strings.Length; i++)
                {
                    Assert.AreEqual(strings[i], reader.ReadString());
                }
            }
            Assert.AreEqual(position, stream.Position, StreamCorruptedMessage);
        }
Exemplo n.º 58
0
 public void Load(PrimitiveReader reader)
 {
     reader.ReadInt32();
     reader.ReadInt32();
 }
Exemplo n.º 59
0
 public void ShouldReadAndWriteLimits()
 {
     var stream = new MemoryStream();
     using(var writer = new PrimitiveWriter(stream))
     {
         writer.Write(byte.MinValue);
         writer.Write(byte.MaxValue);
         writer.Write(sbyte.MinValue);
         writer.Write(sbyte.MaxValue);
         writer.Write(short.MinValue);
         writer.Write(short.MaxValue);
         writer.Write(ushort.MinValue);
         writer.Write(ushort.MaxValue);
         writer.Write(int.MinValue);
         writer.Write(int.MaxValue);
         writer.Write(uint.MinValue);
         writer.Write(uint.MaxValue);
         writer.Write(long.MinValue);
         writer.Write(long.MaxValue);
         writer.Write(ulong.MinValue);
         writer.Write(ulong.MaxValue);
     }
     var position = stream.Position;
     stream.Seek(0, SeekOrigin.Begin);
     using(var reader = new PrimitiveReader(stream))
     {
         Assert.AreEqual(byte.MinValue, reader.ReadByte());
         Assert.AreEqual(byte.MaxValue, reader.ReadByte());
         Assert.AreEqual(sbyte.MinValue, reader.ReadSByte());
         Assert.AreEqual(sbyte.MaxValue, reader.ReadSByte());
         Assert.AreEqual(short.MinValue, reader.ReadInt16());
         Assert.AreEqual(short.MaxValue, reader.ReadInt16());
         Assert.AreEqual(ushort.MinValue, reader.ReadUInt16());
         Assert.AreEqual(ushort.MaxValue, reader.ReadUInt16());
         Assert.AreEqual(int.MinValue, reader.ReadInt32());
         Assert.AreEqual(int.MaxValue, reader.ReadInt32());
         Assert.AreEqual(uint.MinValue, reader.ReadUInt32());
         Assert.AreEqual(uint.MaxValue, reader.ReadUInt32());
         Assert.AreEqual(long.MinValue, reader.ReadInt64());
         Assert.AreEqual(long.MaxValue, reader.ReadInt64());
         Assert.AreEqual(ulong.MinValue, reader.ReadUInt64());
         Assert.AreEqual(ulong.MaxValue, reader.ReadUInt64());
     }
     Assert.AreEqual(position, stream.Position, StreamCorruptedMessage);
 }
Exemplo n.º 60
0
 public override void Read(PrimitiveReader reader)
 {
     Yaw   = reader.ReadFloat();
     Pitch = reader.ReadFloat();
 }