Marshal() public method

public Marshal ( BinaryWriter dataOut ) : void
dataOut System.IO.BinaryWriter
return void
Beispiel #1
0
        public void Marshal(Object o, BinaryWriter ds)
        {
            int size = 1;

            if (o != null)
            {
                DataStructure            c    = (DataStructure)o;
                byte                     type = c.GetDataStructureType();
                BaseDataStreamMarshaller dsm  = dataMarshallers[type & 0xFF];
                if (dsm == null)
                {
                    throw new IOException("Unknown data type: " + type);
                }

                if (tightEncodingEnabled)
                {
                    BooleanStream bs = new BooleanStream();
                    size += dsm.TightMarshal1(this, c, bs);
                    size += bs.MarshalledSize();

                    if (!sizePrefixDisabled)
                    {
                        ds.Write(size);
                    }

                    ds.Write(type);
                    bs.Marshal(ds);
                    dsm.TightMarshal2(this, c, ds, bs);
                }
                else
                {
                    BinaryWriter looseOut = ds;
                    MemoryStream ms       = null;
                    // If we are prefixing then we need to first write it to memory,
                    // otherwise we can write direct to the stream.
                    if (!sizePrefixDisabled)
                    {
                        ms       = new MemoryStream();
                        looseOut = new OpenWireBinaryWriter(ms);
                        looseOut.Write(size);
                    }

                    looseOut.Write(type);
                    dsm.LooseMarshal(this, c, looseOut);

                    if (!sizePrefixDisabled)
                    {
                        ms.Position = 0;
                        looseOut.Write((int)ms.Length - 4);
                        ds.Write(ms.GetBuffer(), 0, (int)ms.Length);
                    }
                }
            }
            else
            {
                ds.Write(size);
                ds.Write(NULL_TYPE);
            }
        }
Beispiel #2
0
        public void Marshal(Object o, BinaryWriter ds)
        {
            int size = 1;
            if(o != null)
            {
                DataStructure c = (DataStructure) o;
                byte type = c.GetDataStructureType();
                BaseDataStreamMarshaller dsm = GetDataStreamMarshallerForType(type);

                if(tightEncodingEnabled)
                {
                    BooleanStream bs = new BooleanStream();
                    size += dsm.TightMarshal1(this, c, bs);
                    size += bs.MarshalledSize();

                    if(!sizePrefixDisabled)
                    {
                        ds.Write(size);
                    }

                    ds.Write(type);
                    bs.Marshal(ds);
                    dsm.TightMarshal2(this, c, ds, bs);
                }
                else
                {
                    BinaryWriter looseOut = ds;
                    MemoryStream ms = null;

                    // If we are prefixing then we need to first write it to memory,
                    // otherwise we can write direct to the stream.
                    if(!sizePrefixDisabled)
                    {
                        ms = new MemoryStream();
                        looseOut = new EndianBinaryWriter(ms);
                        looseOut.Write(size);
                    }

                    looseOut.Write(type);
                    dsm.LooseMarshal(this, c, looseOut);

                    if(!sizePrefixDisabled)
                    {
                        ms.Position = 0;
                        looseOut.Write((int) ms.Length - 4);
                        ds.Write(ms.GetBuffer(), 0, (int) ms.Length);
                    }
                }
            }
            else
            {
                ds.Write(size);
                ds.Write(NULL_TYPE);
            }
        }
Beispiel #3
0
        public void Marshal(Object o, BinaryWriter ds)
        {
            int size = 1;

            if (o != null)
            {
                DataStructure            c    = (DataStructure)o;
                byte                     type = c.GetDataStructureType();
                BaseDataStreamMarshaller dsm;
                bool                     _tightEncodingEnabled;
                bool                     _sizePrefixDisabled;

                lock (this.marshalLock)
                {
                    dsm = GetDataStreamMarshallerForType(type);
                    _tightEncodingEnabled = this.tightEncodingEnabled;
                    _sizePrefixDisabled   = this.sizePrefixDisabled;
                }

                if (_tightEncodingEnabled)
                {
                    BooleanStream bs = new BooleanStream();
                    size += dsm.TightMarshal1(this, c, bs);
                    size += bs.MarshalledSize();

                    if (!_sizePrefixDisabled)
                    {
                        ds.Write(size);
                    }

                    ds.Write(type);
                    bs.Marshal(ds);
                    dsm.TightMarshal2(this, c, ds, bs);
                }
                else
                {
                    BinaryWriter looseOut = ds;
                    MemoryStream ms       = null;

                    // If we are prefixing then we need to first write it to memory,
                    // otherwise we can write direct to the stream.
                    if (!_sizePrefixDisabled)
                    {
                        ms       = new MemoryStream();
                        looseOut = new EndianBinaryWriter(ms);
                        looseOut.Write(size);
                    }

                    looseOut.Write(type);
                    dsm.LooseMarshal(this, c, looseOut);

                    if (!_sizePrefixDisabled)
                    {
                        ms.Position = 0;
                        looseOut.Write((int)ms.Length - 4);
                        ds.Write(ms.GetBuffer(), 0, (int)ms.Length);
                    }
                }
            }
            else
            {
                ds.Write(size);
                ds.Write(NULL_TYPE);
            }
        }
        protected void AssertMarshalBooleans(int count, GetBooleanValueDelegate valueDelegate)
        {
            BooleanStream bs = new BooleanStream();
            for(int i = 0; i < count; i++)
            {
                bs.WriteBoolean(valueDelegate(i, count));
            }
            MemoryStream buffer = new MemoryStream();
            BinaryWriter ds = new EndianBinaryWriter(buffer);
            bs.Marshal(ds);
            ds.Write(endOfStreamMarker);

            // now lets read from the stream

            MemoryStream ins = new MemoryStream(buffer.ToArray());
            BinaryReader dis = new EndianBinaryReader(ins);
            bs = new BooleanStream();
            bs.Unmarshal(dis);

            for(int i = 0; i < count; i++)
            {
                bool expected = valueDelegate(i, count);

                try
                {
                    bool actual = bs.ReadBoolean();
                    Assert.AreEqual(expected, actual);
                }
                catch(Exception e)
                {
                    Assert.Fail("Failed to parse bool: " + i + " out of: " + count + " due to: " + e);
                }
            }
            int marker = dis.ReadInt32();
            Assert.AreEqual(endOfStreamMarker, marker, "did not match: " + endOfStreamMarker + " and " + marker);

            // lets try read and we should get an exception
            try
            {
                dis.ReadByte();
                Assert.Fail("Should have reached the end of the stream");
            }
            catch(EndOfStreamException)
            {
            }
        }