Пример #1
0
        public static string ReadString(byte[] nbytes, ref int index)
        {
            int alen = StreamUtil.ByteArrayToInt(nbytes, index, 4);

            index = index + 4;
            if (alen > 0)
            {
                string astring = nencoder.GetString(nbytes, index, alen);
                index = index + alen;
                return(astring);
            }
            else
            {
                return("");
            }

/*          int nchars = StreamUtil.ByteArrayToInt(nbytes, index, 4);
 *        index = index + 4;
 *        if (nchars == 0)
 *          return "";
 *        StringBuilder nbuilder = new StringBuilder();
 *        short nchar;
 *        for (int i = 0; i < nchars; i++)
 *        {
 *          nchar = (short)((short)nbytes[index] + (((short)nbytes[index + 1]) << 8));
 *          nbuilder.Append((char)nchar);
 *          index = index + 2;
 *        }
 *        return nbuilder.ToString();*/
        }
Пример #2
0
        public static DataSet DeSerializeDataSet(byte[] nbytes)
        {
            BinaryReader nreader  = new BinaryReader(new MemoryStream(nbytes));
            DataSet      ndataset = new DataSet();
            int          index    = 0;

            if (nbytes.Length < 8)
            {
                throw new Exception("Incorrect header in DeserializeDataSet");
            }
            if ((nbytes[0] != 10) || (nbytes[1] != 11) || (nbytes[2] != 12) || (nbytes[3] != 13))
            {
                throw new Exception("Incorrect header in DeserializeDataSet");
            }
            index = 4;
            int ndatatables = StreamUtil.ByteArrayToInt(nbytes, index, 4);

            index = index + 4;
            for (int indextable = 0; indextable < ndatatables; indextable++)
            {
                string    tablename = ReadString(nbytes, ref index);
                DataTable newtable  = new DataTable(tablename);
                newtable.CaseSensitive = true;
                int colcount = StreamUtil.ByteArrayToInt(nbytes, index, 4);
                index = index + 4;
                TypeData[] coltypes = new TypeData[colcount];
                for (int indexcol = 0; indexcol < colcount; indexcol++)
                {
                    TypeData ntype = (TypeData)StreamUtil.ByteArrayToInt(nbytes, index, 4);
                    coltypes[indexcol] = ntype;
                    index = index + 4;
                    string     colname = ReadString(nbytes, ref index);
                    DataColumn newcol  = newtable.Columns.Add(colname, TypeDataToType(ntype));
                }
                // Constraint
                int colsprim = StreamUtil.ByteArrayToInt(nbytes, index, 4);
                index = index + 4;
                if (colsprim < 0)
                {
                    throw new Exception("Incorrect format colsprim");
                }
                DataColumn[] colprim = new DataColumn[colsprim];
                for (int indexprim = 0; indexprim < colsprim; indexprim++)
                {
                    string colprimname = ReadString(nbytes, ref index);
                    colprim[indexprim] = newtable.Columns[colprimname];
                    if (colprim[indexprim] == null)
                    {
                        throw new Exception("Column not found in table " +
                                            tablename + " column " + colprim[indexprim]);
                    }
                }
                // Read rows
                int rowcount = StreamUtil.ByteArrayToInt(nbytes, index, 4);
                index = index + 4;
                if (rowcount > 0)
                {
                    object[] rowvalues = new object[colcount];
                    //for (int acol = 0; acol < colcount; acol++)
                    //rowvalues[acol] = DBNull.Value;
                    for (int rowindex = 0; rowindex < rowcount; rowindex++)
                    {
                        for (int colindex = 0; colindex < colcount; colindex++)
                        {
                            rowvalues[colindex] = ReadValue(nbytes, nreader, coltypes[colindex], ref index);
                        }
                        newtable.Rows.Add(rowvalues);
                    }
                }
                if (colprim.Length > 0)
                {
                    newtable.Constraints.Add("PRIM" + newtable.TableName, colprim, true);
                }
                ndataset.Tables.Add(newtable);
            }
            return(ndataset);
        }
Пример #3
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");
            }
        }
Пример #4
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);
        }