Exemplo n.º 1
0
            /// <exception cref="System.IO.IOException"/>
            public virtual void ReadFields(BinaryReader reader)
            {
                int length = ProtoUtil.ReadRawVarint32(@in);

                theResponseRead = new byte[length];
                @in.ReadFully(theResponseRead);
            }
Exemplo n.º 2
0
            /// <exception cref="System.IO.IOException"/>
            private static byte[] ReadVarintBytes(BinaryReader reader)
            {
                int length = ProtoUtil.ReadRawVarint32(@in);

                byte[] bytes = new byte[length];
                @in.ReadFully(bytes);
                return(bytes);
            }
Exemplo n.º 3
0
 /// <summary>
 /// Try to instantiate a protocol buffer of the given message class
 /// from the given input stream.
 /// </summary>
 /// <param name="protoClass">the class of the generated protocol buffer</param>
 /// <param name="dataIn">the input stream to read from</param>
 /// <returns>the instantiated Message instance</returns>
 /// <exception cref="System.IO.IOException">if an IO problem occurs</exception>
 private static Message TryInstantiateProtobuf(Type protoClass, BinaryReader dataIn)
 {
     try
     {
         if (dataIn is InputStream)
         {
             // We can use the built-in parseDelimitedFrom and not have to re-copy
             // the data
             MethodInfo parseMethod = GetStaticProtobufMethod(protoClass, "parseDelimitedFrom"
                                                              , typeof(InputStream));
             return((Message)parseMethod.Invoke(null, (InputStream)dataIn));
         }
         else
         {
             // Have to read it into a buffer first, since protobuf doesn't deal
             // with the BinaryReader interface directly.
             // Read the size delimiter that writeDelimitedTo writes
             int size = ProtoUtil.ReadRawVarint32(dataIn);
             if (size < 0)
             {
                 throw new IOException("Invalid size: " + size);
             }
             byte[] data = new byte[size];
             dataIn.ReadFully(data);
             MethodInfo parseMethod = GetStaticProtobufMethod(protoClass, "parseFrom", typeof(
                                                                  byte[]));
             return((Message)parseMethod.Invoke(null, data));
         }
     }
     catch (TargetInvocationException e)
     {
         if (e.InnerException is IOException)
         {
             throw (IOException)e.InnerException;
         }
         else
         {
             throw new IOException(e.InnerException);
         }
     }
     catch (MemberAccessException)
     {
         throw new Exception("Could not access parse method in " + protoClass);
     }
 }