Example #1
0
        static void ConvertAllAtOnce(Stream srcStream, MemoryIOStream <double> dstStream, double[] buffer, ValueTypeFormat format, ByteOrder byteOrder)
        {
            var binaryReader    = new BinaryReader(srcStream);
            var sizeOfT         = ValueTypeFormatUtils.SizeOf(format);
            var dstStreamLength = (int)srcStream.Length / sizeOfT;

            dstStream.Length = dstStreamLength;
            using (var dstStreamWriter = dstStream.GetWriter())
            {
                while (!dstStreamWriter.Eos)
                {
                    switch (format)
                    {
                    case ValueTypeFormat.Boolean:
                        var booleanBuffer = binaryReader.ReadBooleans(buffer.Length, byteOrder);
                        for (int i = 0; i < booleanBuffer.Length; i++)
                        {
                            buffer[i] = (double)Convert.ChangeType(booleanBuffer[i], TypeCode.Double);
                        }
                        dstStreamWriter.Write(buffer, 0, booleanBuffer.Length);
                        break;

                    case ValueTypeFormat.SByte:
                        var sbyteBuffer = binaryReader.ReadBytes(buffer.Length);
                        for (int i = 0; i < sbyteBuffer.Length; i++)
                        {
                            buffer[i] = (double)(SByte)sbyteBuffer[i];
                        }
                        dstStreamWriter.Write(buffer, 0, sbyteBuffer.Length);
                        break;

                    case ValueTypeFormat.Byte:
                        var byteBuffer = binaryReader.ReadBytes(buffer.Length);
                        for (int i = 0; i < byteBuffer.Length; i++)
                        {
                            buffer[i] = (double)byteBuffer[i];
                        }
                        dstStreamWriter.Write(buffer, 0, byteBuffer.Length);
                        break;

                    case ValueTypeFormat.Int16:
                        var int16Buffer = binaryReader.ReadInt16s(buffer.Length, byteOrder);
                        for (int i = 0; i < int16Buffer.Length; i++)
                        {
                            buffer[i] = (double)int16Buffer[i];
                        }
                        dstStreamWriter.Write(buffer, 0, int16Buffer.Length);
                        break;

                    case ValueTypeFormat.UInt16:
                        var uint16Buffer = binaryReader.ReadUInt16s(buffer.Length, byteOrder);
                        for (int i = 0; i < uint16Buffer.Length; i++)
                        {
                            buffer[i] = (double)uint16Buffer[i];
                        }
                        dstStreamWriter.Write(buffer, 0, uint16Buffer.Length);
                        break;

                    case ValueTypeFormat.Int32:
                        var int32Buffer = binaryReader.ReadInt32s(buffer.Length, byteOrder);
                        for (int i = 0; i < int32Buffer.Length; i++)
                        {
                            buffer[i] = (double)int32Buffer[i];
                        }
                        dstStreamWriter.Write(buffer, 0, int32Buffer.Length);
                        break;

                    case ValueTypeFormat.UInt32:
                        var uint32Buffer = binaryReader.ReadUInt32s(buffer.Length, byteOrder);
                        for (int i = 0; i < uint32Buffer.Length; i++)
                        {
                            buffer[i] = (double)uint32Buffer[i];
                        }
                        dstStreamWriter.Write(buffer, 0, uint32Buffer.Length);
                        break;

                    case ValueTypeFormat.Int64:
                        var int64Buffer = binaryReader.ReadInt64s(buffer.Length, byteOrder);
                        for (int i = 0; i < int64Buffer.Length; i++)
                        {
                            buffer[i] = (double)int64Buffer[i];
                        }
                        dstStreamWriter.Write(buffer, 0, int64Buffer.Length);
                        break;

                    case ValueTypeFormat.UInt64:
                        var uint64Buffer = binaryReader.ReadUInt64s(buffer.Length, byteOrder);
                        for (int i = 0; i < uint64Buffer.Length; i++)
                        {
                            buffer[i] = (double)uint64Buffer[i];
                        }
                        dstStreamWriter.Write(buffer, 0, uint64Buffer.Length);
                        break;

                    case ValueTypeFormat.Single:
                        var singleBuffer = binaryReader.ReadSingles(buffer.Length, byteOrder);
                        for (int i = 0; i < singleBuffer.Length; i++)
                        {
                            buffer[i] = (double)singleBuffer[i];
                        }
                        dstStreamWriter.Write(buffer, 0, singleBuffer.Length);
                        break;

                    case ValueTypeFormat.Double:
                        var itemRead = binaryReader.Read(buffer, 0, buffer.Length, byteOrder);
                        dstStreamWriter.Write(buffer, 0, itemRead);
                        break;

                    default:
                        throw new NotImplementedException();
                    }
                }
            }
        }
Example #2
0
        static void ConvertAllAtOnce(IInStream <double> srcStream, MemoryStream dstStream, double[] buffer, ValueTypeFormat format, ByteOrder byteOrder)
        {
            var sizeOfFormat = ValueTypeFormatUtils.SizeOf(format);

            dstStream.SetLength(srcStream.Length * sizeOfFormat);
            dstStream.Position = 0;
            var binaryWriter = new BinaryWriter(dstStream);

            using (var reader = srcStream.GetReader())
            {
                var itemsToRead = reader.Length;
                while (itemsToRead > 0)
                {
                    var itemsRead = reader.Read(buffer, 0, buffer.Length);
                    switch (format)
                    {
                    case ValueTypeFormat.Boolean:
                        var booleanBuffer = new Boolean[itemsRead];
                        for (int i = 0; i < booleanBuffer.Length; i++)
                        {
                            booleanBuffer[i] = (Boolean)Convert.ChangeType(buffer[i], typeof(Boolean));
                        }
                        binaryWriter.Write(booleanBuffer, 0, itemsRead, byteOrder);
                        break;

                    case ValueTypeFormat.SByte:
                        var sbyteBuffer = new Byte[itemsRead];
                        unchecked
                        {
                            for (int i = 0; i < sbyteBuffer.Length; i++)
                            {
                                sbyteBuffer[i] = (Byte)(SByte)buffer[i];
                            }
                        }
                        binaryWriter.Write(sbyteBuffer, 0, itemsRead);
                        break;

                    case ValueTypeFormat.Byte:
                        var byteBuffer = new Byte[itemsRead];
                        for (int i = 0; i < byteBuffer.Length; i++)
                        {
                            byteBuffer[i] = (Byte)buffer[i];
                        }
                        binaryWriter.Write(byteBuffer, 0, itemsRead);
                        break;

                    case ValueTypeFormat.Int16:
                        var int16Buffer = new Int16[itemsRead];
                        for (int i = 0; i < int16Buffer.Length; i++)
                        {
                            int16Buffer[i] = (Int16)buffer[i];
                        }
                        binaryWriter.Write(int16Buffer, 0, itemsRead, byteOrder);
                        break;

                    case ValueTypeFormat.UInt16:
                        var uint16Buffer = new UInt16[itemsRead];
                        for (int i = 0; i < uint16Buffer.Length; i++)
                        {
                            uint16Buffer[i] = (UInt16)buffer[i];
                        }
                        binaryWriter.Write(uint16Buffer, 0, itemsRead, byteOrder);
                        break;

                    case ValueTypeFormat.Int32:
                        var int32Buffer = new Int32[itemsRead];
                        for (int i = 0; i < int32Buffer.Length; i++)
                        {
                            int32Buffer[i] = (Int32)buffer[i];
                        }
                        binaryWriter.Write(int32Buffer, 0, itemsRead, byteOrder);
                        break;

                    case ValueTypeFormat.UInt32:
                        var uint32Buffer = new UInt32[itemsRead];
                        for (int i = 0; i < uint32Buffer.Length; i++)
                        {
                            uint32Buffer[i] = (UInt32)buffer[i];
                        }
                        binaryWriter.Write(uint32Buffer, 0, itemsRead, byteOrder);
                        break;

                    case ValueTypeFormat.Int64:
                        var int64Buffer = new Int64[itemsRead];
                        for (int i = 0; i < int64Buffer.Length; i++)
                        {
                            int64Buffer[i] = (Int64)buffer[i];
                        }
                        binaryWriter.Write(int64Buffer, 0, itemsRead, byteOrder);
                        break;

                    case ValueTypeFormat.UInt64:
                        var uint64Buffer = new UInt64[itemsRead];
                        for (int i = 0; i < uint64Buffer.Length; i++)
                        {
                            uint64Buffer[i] = (UInt64)buffer[i];
                        }
                        binaryWriter.Write(uint64Buffer, 0, itemsRead, byteOrder);
                        break;

                    case ValueTypeFormat.Single:
                        var singleBuffer = new Single[itemsRead];
                        for (int i = 0; i < singleBuffer.Length; i++)
                        {
                            singleBuffer[i] = (Single)buffer[i];
                        }
                        binaryWriter.Write(singleBuffer, 0, itemsRead, byteOrder);
                        break;

                    case ValueTypeFormat.Double:
                        binaryWriter.Write(buffer, 0, itemsRead, byteOrder);
                        break;

                    default:
                        break;
                    }
                    itemsToRead -= itemsRead;
                }
            }
        }