Ejemplo n.º 1
0
        /// <summary>
        /// Function to write an array of value types to a stream using the GorgonBinaryWriter and reading it back again using the GorgonBinaryReader.
        /// </summary>
        /// <param name="stream">The stream that will receive the data.</param>
        private static void WriteArrayValues(MemoryStream stream)
        {
            stream.Position = 0;

            var writer = new GorgonBinaryWriter(stream, true);
            var reader = new GorgonBinaryReader(stream, true);

            try
            {
                var expected = new SomeTestData[3];

                for (int i = 1; i < 4; ++i)
                {
                    expected[i - 1] = new SomeTestData
                    {
                        Value1 = i,
                        Value2 = System.Math.PI * i,
                        Value3 = (short)(i & 2)
                    };
                }

                Console.ForegroundColor = ConsoleColor.Cyan;
                Console.WriteLine("Writing/Reading an array of value types to a memory stream.");
                Console.ForegroundColor = ConsoleColor.White;

                writer.WriteRange(expected);

                stream.Position = 0;

                var actual = new SomeTestData[4];
                reader.ReadRange(actual, 1);

                for (int i = 1; i < 4; ++i)
                {
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.Write($"[{i - 1}] ");
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.WriteLine($"int32 Value1 = {expected[i - 1].Value1}: {actual[i].Value1 == expected[i - 1].Value1}");
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.Write($"[{i - 1}] ");
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.WriteLine($"double Value2 = {expected[i - 1].Value2:0.00000}: {actual[i].Value2.EqualsEpsilon(expected[i - 1].Value2)}");
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.Write($"[{i - 1}] ");
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.WriteLine($"int16 Value3 = {expected[i - 1].Value3}: {actual[i].Value3 == expected[i - 1].Value3}");
                }

                stream.Position = 0;
            }
            finally
            {
                writer.Dispose();
                reader.Dispose();
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Function to determine if this codec can read the file or not.
        /// </summary>
        /// <param name="stream">Stream used to read the file information.</param>
        /// <returns>
        /// TRUE if the codec can read the file, FALSE if not.
        /// </returns>
        /// <remarks>
        /// This is the method we'll use to determine if the data in the stream can actually be read by our codec.  Typically this is done by a "magic number" consisting of a set of bytes
        /// that identify the data as the type we're expecting.  To retrieve the magic number we'll read in the meta data for the image, which may not seem efficient, but it gives us the
        /// ability to also check to ensure that the image stream contains enough information about the image to actually load it by comparing the size of the meta data in the stream with
        /// the required meta data size.
        /// <para>
        /// When overloading this method, the implementor should remember to reset the stream position back to the original position when they are done reading the data.  Failure to do so
        /// may cause undesirable results.
        /// </para>
        /// </remarks>
        /// <exception cref="System.ArgumentNullException">Thrown when the <paramref name="stream"/> parameter is NULL (Nothing in VB.Net).</exception>
        /// <exception cref="System.IO.IOException">Thrown when the <paramref name="stream"/> is write-only or if the stream cannot perform seek operations.</exception>
        /// <exception cref="System.IO.EndOfStreamException">Thrown when an attempt to read beyond the end of the stream is made.</exception>
        public override bool IsReadable(Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            if (!stream.CanRead)
            {
                throw new IOException("The stream is write only.");
            }

            if (!stream.CanSeek)
            {
                throw new IOException("Stream cannot perform seek operations.");
            }

            if (stream.Position + TvHeader.SizeInBytes >= stream.Length)
            {
                throw new EndOfStreamException();
            }

            // Remember the stream position.
            // If we fail to do this then the stream will be offset when we return and could cause corruption.
            long position             = stream.Position;
            GorgonBinaryReader reader = null;

            try
            {
                // Using the GorgonBinaryReader, we can pull in the data we need.
                reader = new GorgonBinaryReader(stream, true);

                // Retrieve our magic number.
                var header = reader.ReadValue <TvHeader>();

                // Ensure that the image size is valid and that the magic numbers match up.
                return(header.Width > 0 && header.Height > 0 && header.MagicValueData == MagicValue);
            }
            finally
            {
                if (reader != null)
                {
                    reader.Dispose();
                }

                // Restore the stream to its original placement.
                stream.Position = position;
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Function to read the meta data for image data within a stream.
        /// </summary>
        /// <param name="stream">The stream containing the metadata to read.</param>
        /// <returns>
        /// The image meta data as a <see cref="IGorgonImageInfo"/> value.
        /// </returns>
        /// <exception cref="IOException">Thrown when the <paramref name="stream"/> is write-only or if the stream cannot perform seek operations.
        /// <para>-or-</para>
        /// <para>Thrown if the file is corrupt or can't be read by the codec.</para>
        /// </exception>
        /// <exception cref="ArgumentNullException">Thrown when the <paramref name="stream"/> parameter is <b>null</b>.</exception>
        /// <exception cref="IOException">Thrown when the <paramref name="stream"/> is write-only or if the stream cannot perform seek operations.</exception>
        /// <exception cref="EndOfStreamException">Thrown when an attempt to read beyond the end of the stream is made.</exception>
        /// <remarks>
        /// <para>
        /// Ensure that the file can be read by calling the <see cref="IsReadable"/> method prior to calling this method.
        /// </para>
        /// </remarks>
        /// <seealso cref="IsReadable"/>
        public override IGorgonImageInfo GetMetaData(Stream stream)
        {
            int  headerSize = Unsafe.SizeOf <TgaHeader>();
            long position   = 0;


            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            if (!stream.CanRead)
            {
                throw new IOException(Resources.GORIMG_ERR_STREAM_IS_WRITEONLY);
            }

            if (!stream.CanSeek)
            {
                throw new IOException(Resources.GORIMG_ERR_STREAM_CANNOT_SEEK);
            }

            if (stream.Length - stream.Position < sizeof(uint) + headerSize)
            {
                throw new EndOfStreamException();
            }

            GorgonBinaryReader reader = null;

            try
            {
                position = stream.Position;
                reader   = new GorgonBinaryReader(stream, true);
                return(ReadHeader(reader, out _));
            }
            finally
            {
                stream.Position = position;
                reader?.Dispose();
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Function to write the contents pointed at by an unsafe pointer to a stream using the GorgonBinaryWriter and reading it back again using the GorgonBinaryReader.
        /// </summary>
        /// <param name="stream">The stream that will receive the data.</param>
        private static unsafe void WritePointer(MemoryStream stream)
        {
            stream.Position = 0;
            var writer = new GorgonBinaryWriter(stream, true);
            var reader = new GorgonBinaryReader(stream, true);

            try
            {
                var data = new SomeTestData
                {
                    Value1 = 1,
                    Value2 = 2.1,
                    Value3 = 3
                };

                Console.ForegroundColor = ConsoleColor.Cyan;
                Console.WriteLine("Writing/Reading pointer to memory into a memory stream.");
                Console.ForegroundColor = ConsoleColor.White;

                writer.Write(&data, Unsafe.SizeOf <SomeTestData>());

                stream.Position = 0;

                SomeTestData readData = default;
                reader.Read(&readData, Unsafe.SizeOf <SomeTestData>());

                Console.WriteLine($"int32 Value1 = 1: {readData.Value1 == 1}");
                Console.WriteLine($"double Value2 = 2.1: {readData.Value2.EqualsEpsilon(2.1)}");
                Console.WriteLine($"int16 Value3 = 3: {readData.Value3 == 3}");

                stream.Position = 0;
            }
            finally
            {
                writer.Dispose();
                reader.Dispose();
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Function to determine if this codec can read the font data within the stream or not.
        /// </summary>
        /// <param name="stream">The stream that is used to read the font data.</param>
        /// <returns><b>true</b> if the codec can read the file, <b>false</b> if not.</returns>
        /// <exception cref="ArgumentNullException">Thrown when the <paramref name="stream"/> parameter is <b>null</b>.</exception>
        /// <exception cref="IOException">Thrown when the <paramref name="stream"/> is write-only or if the stream cannot perform seek operations.</exception>
        /// <exception cref="EndOfStreamException">Thrown when an attempt to read beyond the end of the stream is made.</exception>
        /// <remarks>
        /// <para>
        /// Implementors should ensure that the stream position is restored prior to exiting this method. Failure to do so may cause problems when reading the data from the stream.
        /// </para>
        /// </remarks>
        public override bool IsReadable(Stream stream)
        {
            ulong fileHeaderID = FileHeader.ChunkID();

            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            if (!stream.CanRead)
            {
                throw new IOException(Resources.GORGFX_ERR_STREAM_WRITE_ONLY);
            }

            if (!stream.CanSeek)
            {
                throw new IOException(Resources.GORGFX_ERR_STREAM_NO_SEEK);
            }

            if (stream.Length - stream.Position < FileHeader.Length)
            {
                return(false);
            }

            long position = stream.Position;
            var  reader   = new GorgonBinaryReader(stream, true);

            try
            {
                ulong fileData = reader.ReadUInt64();
                return(fileData == fileHeaderID);
            }
            finally
            {
                reader.Dispose();
                stream.Position = position;
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Function to write the contents of a value type to a stream using the GorgonBinaryWriter and reading it back again using the GorgonBinaryReader.
        /// </summary>
        /// <param name="stream">The stream that will receive the data.</param>
        private static void WriteByRefValueType(MemoryStream stream)
        {
            stream.Position = 0;
            var writer = new GorgonBinaryWriter(stream, true);
            var reader = new GorgonBinaryReader(stream, true);

            try
            {
                var data = new SomeTestData
                {
                    Value1 = 1234,
                    Value2 = 3.1459,
                    Value3 = 42
                };

                Console.ForegroundColor = ConsoleColor.Cyan;
                Console.WriteLine("Writing/Reading a value type to a memory stream.");
                Console.ForegroundColor = ConsoleColor.White;

                writer.WriteValue(ref data);

                stream.Position = 0;

                reader.ReadValue(out SomeTestData readData);

                Console.WriteLine($"int32 Value1 = 1234: {readData.Value1 == 1234}");
                Console.WriteLine($"double Value2 = 3.1459: {readData.Value2.EqualsEpsilon(3.1459)}");
                Console.WriteLine($"int16 Value3 = 42: {readData.Value3 == 42}");

                stream.Position = 0;
            }
            finally
            {
                writer.Dispose();
                reader.Dispose();
            }
        }