public SerializationContext(Stream stream, SerializationContext parent)
 {
     this.stream = stream;
     if (parent == null)
     {
         workspace = new byte[InitialBufferLength];
         ioBuffer = new byte[IO_BUFFER_SIZE];
     }
     else
     {
         ReadFrom(parent);
     }
 }
 public int Serialize(IExtension extension)
 {
     if (extension == null) return 0;
     Stream stream = extension.BeginAppend();
     try
     {
         SerializationContext ctx = new SerializationContext(stream, null);
         int len = Serialize(ctx);
         ctx.Flush();
         extension.EndAppend(stream, true);
         return len;
     }
     catch
     {
         extension.EndAppend(stream, false);
         throw;
     }
 }
 private void TraceChangeOrigin(SerializationContext context)
 {
     long newPos = stream.Position, oldPos = context.stream.Position;
     if(oldPos != newPos)
     {
         Debug.WriteLine(new string('!', stackDepth) +
             string.Format(" re-based: {0} now {1}", oldPos, newPos),
             SerializationContext.DebugCategory);
     }
 }
        public void ReadFrom(SerializationContext context)
        {
            if (context == null) throw new ArgumentNullException("context");

            this.workspace = context.workspace;
            this.objectStack = context.objectStack;
            this.stackDepth = context.stackDepth;
            this.streamState = context.streamState;
            this.peekedValue = context.peekedValue;
            this.ioBuffer = context.ioBuffer;
            this.ioBufferIndex = context.ioBufferIndex;
            this.inputStreamAvailable = context.inputStreamAvailable;
            this.ioBufferEffectiveSize = context.ioBufferEffectiveSize;

            TraceChangeOrigin(context); // note ConditionalAttribute

            // IMPORTANT: don't copy the group stack; we want to
            // validate that the group-stack is empty when finding the end of a stream
        }
 public override int Serialize(SerializationContext context)
 {
     return WritePrefix(context) + context.EncodeInt32(wireValue);
 }
 protected int WritePrefix(SerializationContext context)
 {
     return prefix == 0 ? 0 : context.EncodeUInt32(prefix);
 }
 public abstract int Serialize(SerializationContext context);