예제 #1
0
        private static object ReadValue(byte[] nbytes, BinaryReader nreader, TypeData ntype, ref int index)
        {
            //byte[] nres = null;
            byte nlen = nbytes[index];

            index++;
            if (nlen == 0)
            {
                return(DBNull.Value);
            }
            switch (ntype)
            {
            case TypeData.Int32:
                nreader.BaseStream.Seek(index, SeekOrigin.Begin);
                int iresult = nreader.ReadInt32();
                index = index + ((int)nreader.BaseStream.Position - index);
                return(iresult);

            //int iresult = StreamUtil.ByteArrayToInt(nbytes, index, nlen);
            //index = index + nlen;
            //return iresult;
            case TypeData.Int64:
//              long i64result = StreamUtil.ByteArrayToInt64(nbytes, index, nlen);
//              index = index + nlen;
//              return i64result;
                nreader.BaseStream.Seek(index, SeekOrigin.Begin);
                long i64result = nreader.ReadInt64();
                index = index + ((int)nreader.BaseStream.Position - index);
                return(i64result);

            case TypeData.Int16:
//              int i16result = StreamUtil.ByteArrayToShort(nbytes, index, nlen);
//              index = index + nlen;
//              return i16result;
                nreader.BaseStream.Seek(index, SeekOrigin.Begin);
                short i16result = nreader.ReadInt16();
                index = index + ((int)nreader.BaseStream.Position - index);
                return(i16result);

            case TypeData.Char:
                char nchar = (char)StreamUtil.ByteArrayToShort(nbytes, index, nlen);
                index = index + nlen;
                return(nchar);

            case TypeData.Byte:
                index = index + 1;
                return(nbytes[index - 1]);

            case TypeData.String:
                return(ReadString(nbytes, ref index));

            case TypeData.Double:
                nreader.BaseStream.Seek(index, SeekOrigin.Begin);
                Double doublevalue = nreader.ReadDouble();
                index = index + ((int)nreader.BaseStream.Position - index);
                return(doublevalue);

            case TypeData.Float:
                nreader.BaseStream.Seek(index, SeekOrigin.Begin);
                float floatvalue = nreader.ReadSingle();
                index = index + ((int)nreader.BaseStream.Position - index);
                return(floatvalue);

            case TypeData.Single:
                nreader.BaseStream.Seek(index, SeekOrigin.Begin);
                Single singlevalue = nreader.ReadSingle();
                index = index + ((int)nreader.BaseStream.Position - index);
                return(singlevalue);

            case TypeData.Boolean:
                nreader.BaseStream.Seek(index, SeekOrigin.Begin);
                bool boolvalue = nreader.ReadBoolean();
                index = index + ((int)nreader.BaseStream.Position - index);
                return(boolvalue);

            case TypeData.Decimal:
                nreader.BaseStream.Seek(index, SeekOrigin.Begin);
                decimal decimalvalue = nreader.ReadDecimal();
                index = index + ((int)nreader.BaseStream.Position - index);
                return(decimalvalue);

            case TypeData.TimeSpan:
                nreader.BaseStream.Seek(index, SeekOrigin.Begin);
                long itimeresult = nreader.ReadInt64();
                index = index + ((int)nreader.BaseStream.Position - index);
                return(new TimeSpan(itimeresult));

            case TypeData.DateTime:
                //              nwriter.Write((DateTime)nvalue);
                short Year        = StreamUtil.ByteArrayToShort(nbytes, index, 2);
                short Millisecond = 0;
                if (nlen > 8)
                {
                    Millisecond = StreamUtil.ByteArrayToShort(nbytes, index + 7, 2);
                }
                else
                if (nlen > 7)
                {
                    Millisecond = StreamUtil.ByteArrayToShort(nbytes, index + 7, 1);
                }
                byte seconds = 0;
                if (nlen > 6)
                {
                    seconds = nbytes[index + 6];
                }
                byte minutes = 0;
                if (nlen > 5)
                {
                    minutes = nbytes[index + 5];
                }
                byte hours = 0;
                if (nlen > 4)
                {
                    hours = nbytes[index + 4];
                }
                DateTime dvalue = new DateTime(Year, nbytes[index + 2], nbytes[index + 3], hours,
                                               minutes, seconds, Millisecond);
                index = index + nlen;
                return(dvalue);

            case TypeData.ByteArray:
                int blength = StreamUtil.ByteArrayToInt(nbytes, index, 4);
                index = index + 4;
                byte[] bbvalue = new byte[blength];
                if (blength > 0)
                {
                    Array.Copy(nbytes, index, bbvalue, 0, blength);
                    index = index + blength;
                }
                return(bbvalue);

            case TypeData.Object:
                string   realtype = ReadString(nbytes, ref index);
                TypeData newtype  = TypeToTypeData(System.Type.GetType(realtype));
                if (newtype == TypeData.Object)
                {
                    throw new Exception("Unsupported object datacolumn containint object");
                }
                return(ReadValue(nbytes, nreader, newtype, ref index));

            default:
                throw new Exception("Data type not supported in FastSerializer");
            }
        }
예제 #2
0
        /// <summary>
        /// Obtains a memory stream from an expression resulting in a file name path or
        /// a database field
        /// </summary>
        /// <param name="expre">Expression to evaluate</param>
        /// <returns>A Memory stream or null if the result is not a stream</returns>
        public MemoryStream GetStreamFromExpression(string expre)
        {
            EvalIdentifier iden      = SearchIdentifier(expre);
            MemoryStream   memstream = null;

            if (iden != null)
            {
                if (iden is IdenField)
                {
                    Variant avalue = iden.Value;
                    if (avalue.VarType == VariantType.Binary)
                    {
                        memstream = avalue.GetStream();
                        memstream.Seek(0, System.IO.SeekOrigin.Begin);
                        // Search for Paradox graphic header

                        if (memstream.Length > 8)
                        {
                            byte[] buf = new byte[8];
                            memstream.Read(buf, 0, 8);
                            ParadoxGraphicHeader hd = new ParadoxGraphicHeader();
                            hd.Count = StreamUtil.ByteArrayToShort(buf, 0, 2);
                            hd.HType = StreamUtil.ByteArrayToShort(buf, 2, 2);
                            hd.Size  = StreamUtil.ByteArrayToInt(buf, 4, 4);
                            if ((hd.Count == 1) && (hd.HType == 0x0100))
                            {
                                MemoryStream astream = new MemoryStream();
                                buf = new byte[hd.Size];
                                int readed = memstream.Read(buf, 0, hd.Size);
                                astream.Write(buf, 0, readed);
                                memstream = astream;
                            }
                            memstream.Seek(0, System.IO.SeekOrigin.Begin);
                        }
                    }
                    else
                    if (avalue.VarType != VariantType.Null)
                    {
                        iden = null;
                    }
                }
                else
                {
                    iden = null;
                }
            }
            if (iden == null)
            {
                Variant avalue = EvaluateText(expre);
                if (avalue.VarType != VariantType.String)
                {
                    throw new NamedException("Field or file not found:" + expre, expre);
                }
                System.IO.FileInfo finfo = new System.IO.FileInfo(avalue.AsString);
                if (!finfo.Exists)
                {
                    throw new NamedException("File not found:" + avalue.AsString, avalue.AsString);
                }
                memstream = new MemoryStream();
                FileStream afile = new FileStream(avalue.AsString,
                                                  System.IO.FileMode.Open, System.IO.FileAccess.Read);
                try
                {
                    byte[] buf    = new byte[120000];
                    int    readed = afile.Read(buf, 0, 120000);
                    while (readed > 0)
                    {
                        memstream.Write(buf, 0, readed);
                        readed = afile.Read(buf, 0, 120000);
                    }
                }
                finally
                {
                    afile.Close();
                }
            }
            return(memstream);
        }