예제 #1
0
        internal static object UnmarshalDate(Stream stream)
        {
            DateTimeMarshaler m = new DateTimeMarshaler();

            m.Unmarshal(stream);
            return(m);
        }
예제 #2
0
        internal static void MarshalDate(Stream stream, object value, DateTimeType type)
        {
            DateTimeMarshaler m = new DateTimeMarshaler();

            m.ObjectToBytes(value, type);
            m.Marshal(stream);
        }
		internal static void MarshalDate (Stream stream, object value, DateTimeType type)
		{
			DateTimeMarshaler m = new DateTimeMarshaler ();
			m.ObjectToBytes (value, type);
			m.Marshal (stream);
		}
		internal static object UnmarshalDate (Stream stream)
		{
			DateTimeMarshaler m = new DateTimeMarshaler ();
			m.Unmarshal (stream);
			return m;
		}
예제 #5
0
        internal static object Unmarshal(Stream stream, ManagedConnection connection)
        {
            string table = connection == null ? null : connection.charsetTable;

            Debug.WriteLineIf(CLI.FnTrace.Enabled, "Marshaler.Unmarshal ()");

            BoxTag tag = (BoxTag)ReadByte(stream);

            Debug.WriteLineIf(marshalSwitch.Enabled, "tag: " + tag);

            switch (tag)
            {
            case BoxTag.DV_NULL:
#if false
                return(null);
#else
                return(0);
#endif

            case BoxTag.DV_DB_NULL:
                return(DBNull.Value);

            case BoxTag.DV_ARRAY_OF_POINTER:
            case BoxTag.DV_LIST_OF_POINTER:
            {
                int      n     = UnmarshalInt(stream);
                object[] array = new object[n];
                Debug.WriteLineIf(marshalSwitch.Enabled, "array start [" + n + "] {");
                for (int i = 0; i < n; i++)
                {
                    array[i] = Unmarshal(stream, connection);
                }
                Debug.WriteLineIf(marshalSwitch.Enabled, "array end");
                return(array);
            }

            case BoxTag.DV_ARRAY_OF_LONG:
            {
                int   n     = UnmarshalInt(stream);
                int[] array = new int[n];
                Debug.WriteLineIf(marshalSwitch.Enabled, "array start [" + n + "] {");
                for (int i = 0; i < n; i++)
                {
                    array[i] = UnmarshalLongInt(stream);
                }
                Debug.WriteLineIf(marshalSwitch.Enabled, "array end");
                return(array);
            }

            case BoxTag.DV_ARRAY_OF_LONG_PACKED:
            {
                int   n     = UnmarshalInt(stream);
                int[] array = new int[n];
                Debug.WriteLineIf(marshalSwitch.Enabled, "array start [" + n + "] {");
                for (int i = 0; i < n; i++)
                {
                    array[i] = UnmarshalLongInt(stream);
                }
                Debug.WriteLineIf(marshalSwitch.Enabled, "array end");
                return(array);
            }

            case BoxTag.DV_ARRAY_OF_FLOAT:
            {
                int     n     = UnmarshalInt(stream);
                float[] array = new float[n];
                Debug.WriteLineIf(marshalSwitch.Enabled, "array start [" + n + "] {");
                for (int i = 0; i < n; i++)
                {
                    array[i] = UnmarshalSingle(stream);
                }
                Debug.WriteLineIf(marshalSwitch.Enabled, "array end");
                return(array);
            }

            case BoxTag.DV_ARRAY_OF_DOUBLE:
            {
                int      n     = UnmarshalInt(stream);
                double[] array = new double[n];
                Debug.WriteLineIf(marshalSwitch.Enabled, "array start [" + n + "] {");
                for (int i = 0; i < n; i++)
                {
                    array[i] = UnmarshalDouble(stream);
                }
                Debug.WriteLineIf(marshalSwitch.Enabled, "array end");
                return(array);
            }

            case BoxTag.DV_LONG_WIDE:
            {
                int    i = 0, n = UnmarshalLongInt(stream);
                byte[] bytes = new byte[n];
                while (i < n)
                {
                    i += stream.Read(bytes, i, n - i);
                }
                return(Encoding.UTF8.GetString(bytes));
            }

            case BoxTag.DV_WIDE:
            {
                int    i = 0, n = UnmarshalShortInt(stream);
                byte[] bytes = new byte[n];
                while (i < n)
                {
                    i += stream.Read(bytes, i, n - i);
                }
                return(Encoding.UTF8.GetString(bytes));
            }

            case BoxTag.DV_C_STRING:
            case BoxTag.DV_STRING:
            case BoxTag.DV_LONG_CONT_STRING:
            {
                int    i = 0, n = UnmarshalLongInt(stream);
                byte[] bytes = new byte[n];
                while (i < n)
                {
                    i += stream.Read(bytes, i, n - i);
                }
#if IGNORE_ENCODING
                return(Encoding.GetEncoding("iso-8859-1").GetString(bytes));
#else
                if (connection.charset_utf8)
                {
                    return(Encoding.UTF8.GetString(bytes));
                }
                else
                {
                    return(Decode(table, bytes));
                }
#endif
            }

            case BoxTag.DV_BOX_FLAGS:
            {
                int    flags = UnmarshalLongInt(stream);
                object str   = Unmarshal(stream, connection);
                return(new SqlExtendedString(str.ToString(), flags));
            }

            case BoxTag.DV_RDF:
            {
                int    flags = ReadByte(stream);
                object box;
                short  type;
                short  lang;
                bool   is_complete = false;
                long   ro_id       = 0L;

                if (0 != (flags & (int)SqlRdfBoxFlags.RBS_CHKSUM))
                {
                    throw new SystemException("Invalid rdf box received.");
                }

                box = Unmarshal(stream, connection);
                if (0 != (flags & (int)SqlRdfBoxFlags.RBS_OUTLINED))
                {
                    if (0 != (flags & (int)SqlRdfBoxFlags.RBS_64))
                    {
                        ro_id = UnmarshalInt64(stream);
                    }
                    else
                    {
                        ro_id = UnmarshalLongInt(stream);
                    }
                }

                if (0 != (flags & (int)SqlRdfBoxFlags.RBS_COMPLETE))
                {
                    is_complete = true;
                }

                if (0 != (flags & (int)SqlRdfBoxFlags.RBS_HAS_TYPE))
                {
                    type = UnmarshalShort(stream);
                }
                else
                {
                    type = SqlRdfBox.DEFAULT_TYPE;
                }

                if (0 != (flags & (int)SqlRdfBoxFlags.RBS_HAS_LANG))
                {
                    lang = UnmarshalShort(stream);
                }
                else
                {
                    lang = SqlRdfBox.DEFAULT_LANG;
                }
                return(new SqlRdfBox(connection, box, is_complete, type, lang, ro_id));
            }

            case BoxTag.DV_C_SHORT:
            case BoxTag.DV_SHORT_STRING_SERIAL:
            case BoxTag.DV_SHORT_CONT_STRING:
            {
                int    i = 0, n = UnmarshalShortInt(stream);
                byte[] bytes = new byte[n];
                while (i < n)
                {
                    i += stream.Read(bytes, i, n - i);
                }
#if IGNORE_ENCODING
                return(Encoding.GetEncoding("iso-8859-1").GetString(bytes));
#else
                if (connection.charset_utf8)
                {
                    return(Encoding.UTF8.GetString(bytes));
                }
                else
                {
                    return(Decode(table, bytes));
                }
#endif
            }

            case BoxTag.DV_LONG_BIN:
            {
                int    i = 0, n = UnmarshalLongInt(stream);
                byte[] bytes = new byte[n];
                while (i < n)
                {
                    i += stream.Read(bytes, i, n - i);
                }
                return(bytes);
            }

            case BoxTag.DV_BIN:
            {
                int    i = 0, n = UnmarshalShortInt(stream);
                byte[] bytes = new byte[n];
                while (i < n)
                {
                    i += stream.Read(bytes, i, n - i);
                }
                return(bytes);
            }

            case BoxTag.DV_SINGLE_FLOAT:
                return(UnmarshalSingle(stream));

            case BoxTag.DV_IRI_ID:
                return(UnmarshalLongInt(stream));

            case BoxTag.DV_IRI_ID_8:
            case BoxTag.DV_INT64:
                return(UnmarshalInt64(stream));

            case BoxTag.DV_DOUBLE_FLOAT:
                return(UnmarshalDouble(stream));

            case BoxTag.DV_SHORT_INT:
            {
                int i = UnmarshalShortInt(stream);
                if (i > 127)
                {
                    i -= 256;
                }
                return(i);
            }

            case BoxTag.DV_LONG_INT:
                return(UnmarshalLongInt(stream));

            case BoxTag.DV_DATETIME:
            case BoxTag.DV_DATE:
            case BoxTag.DV_TIME:
            case BoxTag.DV_TIMESTAMP:
            case BoxTag.DV_TIMESTAMP_OBJ:
                return(DateTimeMarshaler.UnmarshalDate(stream));

            case BoxTag.DV_NUMERIC:
                return(UnmarshalNumeric(stream));

            case BoxTag.DV_BLOB_HANDLE:
            case BoxTag.DV_BLOB_WIDE_HANDLE:
                return(UnmarshalBlobHandle(stream, tag));

            case BoxTag.DV_OBJECT:
                return(UnmarshalObject(stream));

            case BoxTag.DV_STRING_SESSION:
                return(UnmarshalStrses(stream));

            default:
                throw new SystemException("Unknown data type " + tag + ".");
            }
        }
예제 #6
0
        internal static void Marshal(Stream stream, Hashtable map, object value)
        {
            Debug.WriteLineIf(CLI.FnTrace.Enabled, "Marshaler.Marshal ()");

            if (value == null)
            {
                Debug.WriteLineIf(marshalSwitch.Enabled, "value: null");
#if false
                throw new ArgumentNullException("value");
#else
                stream.WriteByte((byte)BoxTag.DV_NULL);
                return;
#endif
            }

            Debug.WriteLineIf(marshalSwitch.Enabled, "value: " + value);

            if (value is IMarshal)
            {
                Debug.WriteLineIf(marshalSwitch.Enabled, "type: " + value.GetType());
                ((IMarshal)value).Marshal(stream);
                return;
            }

            Type     type     = value.GetType();
            TypeCode typeCode = Type.GetTypeCode(type);
            Debug.WriteLineIf(marshalSwitch.Enabled, "type: " + type);
            Debug.WriteLineIf(marshalSwitch.Enabled, "typeCode: " + typeCode);
#if !WIN32_ONLY
            // Work around Mono bug returning TypeCode Object for DBNull.
            if (value == DBNull.Value && typeCode != TypeCode.DBNull)
            {
                typeCode = TypeCode.DBNull;
            }
#endif
            switch (typeCode)
            {
            case TypeCode.DBNull:
                stream.WriteByte((byte)BoxTag.DV_DB_NULL);
                return;

            case TypeCode.Int16:
                MarshalInt(stream, (short)value);
                return;

            case TypeCode.Int32:
                MarshalInt(stream, (int)value);
                return;

            case TypeCode.Int64:
                stream.WriteByte((byte)BoxTag.DV_INT64);
                MarshalLongInt64(stream, (Int64)value);
                return;

            case TypeCode.Single:
                stream.WriteByte((byte)BoxTag.DV_SINGLE_FLOAT);
                MarshalSingle(stream, (float)value);
                return;

            case TypeCode.Double:
                stream.WriteByte((byte)BoxTag.DV_DOUBLE_FLOAT);
                MarshalDouble(stream, (double)value);
                return;

            case TypeCode.String:
            {
                string s = (string)value;
#if IGNORE_ENCODING
                byte[] bytes = Encoding.GetEncoding("iso-8859-1").GetBytes(s);
#else
                byte[] bytes = Encode(map, s);
#endif
                if (bytes.Length < 256)
                {
                    stream.WriteByte((byte)BoxTag.DV_SHORT_STRING_SERIAL);
                    stream.WriteByte((byte)bytes.Length);
                }
                else
                {
                    stream.WriteByte((byte)BoxTag.DV_STRING);
                    MarshalLongInt(stream, bytes.Length);
                }
                stream.Write(bytes, 0, bytes.Length);
                return;
            }

            case TypeCode.Decimal:
            {
                MarshalNumeric(stream, (decimal)value);
                return;
            }

            case TypeCode.DateTime:
            {
                DateTimeMarshaler.MarshalDate(stream, value, DateTimeType.DT_TYPE_DATETIME);
                return;
            }

            case TypeCode.Object:
                if (type == typeof(TimeSpan))
                {
                    DateTimeMarshaler.MarshalDate(stream, value, DateTimeType.DT_TYPE_TIME);
                }
                else if (type == typeof(VirtuosoTimeSpan))
                {
                    DateTimeMarshaler.MarshalDate(stream, value, DateTimeType.DT_TYPE_TIME);
                }
                else if (type == typeof(VirtuosoDateTime))
                {
                    DateTimeMarshaler.MarshalDate(stream, value, DateTimeType.DT_TYPE_DATETIME);
                }
#if ADONET3
                else if (type == typeof(VirtuosoDateTimeOffset))
                {
                    DateTimeMarshaler.MarshalDate(stream, value, DateTimeType.DT_TYPE_DATETIME);
                }
#endif
                else if (type == typeof(int[]))
                {
                    stream.WriteByte((byte)BoxTag.DV_ARRAY_OF_LONG);

                    int[] array = (int[])value;
                    MarshalInt(stream, array.Length);
                    for (int i = 0; i < array.Length; i++)
                    {
                        MarshalLongInt(stream, array[i]);
                    }
                }
                else if (type == typeof(float[]))
                {
                    stream.WriteByte((byte)BoxTag.DV_ARRAY_OF_FLOAT);

                    float[] array = (float[])value;
                    MarshalInt(stream, array.Length);
                    for (int i = 0; i < array.Length; i++)
                    {
                        MarshalSingle(stream, array[i]);
                    }
                }
                else if (type == typeof(double[]))
                {
                    stream.WriteByte((byte)BoxTag.DV_ARRAY_OF_DOUBLE);

                    double[] array = (double[])value;
                    MarshalInt(stream, array.Length);
                    for (int i = 0; i < array.Length; i++)
                    {
                        MarshalDouble(stream, array[i]);
                    }
                }
                else if (type == typeof(object[]))
                {
                    stream.WriteByte((byte)BoxTag.DV_ARRAY_OF_POINTER);

                    object[] array = (object[])value;
                    MarshalInt(stream, array.Length);
                    for (int i = 0; i < array.Length; i++)
                    {
                        Marshal(stream, map, array[i]);
                    }
                }
                else if (type == typeof(BlobHandle))
                {
                    MarshalBlobHandle(stream, (BlobHandle)value);
                }
                else if (type == typeof(byte[]))
                {
                    byte[] bytes = (byte[])value;
#if true
                    if (bytes.Length < 256)
                    {
                        stream.WriteByte((byte)BoxTag.DV_BIN);
                        stream.WriteByte((byte)bytes.Length);
                    }
                    else
                    {
                        stream.WriteByte((byte)BoxTag.DV_LONG_BIN);
                        MarshalLongInt(stream, bytes.Length);
                    }
                    stream.Write(bytes, 0, bytes.Length);
#else
                    stream.Write(bytes, 0, bytes.Length);
#endif
                }
                else
                {
                    goto default;
                }
                return;

            default:
                throw new ArgumentException("Unsupported value type", "value");
            }
        }