Provides a stream for decoding Erlang terms from external format.
Inheritance: System.IO.MemoryStream
 public void Read_Boolean(byte[] buf, bool want)
 {
     using (var s = new OtpInputStream(buf))
     {
         Assert.AreEqual(want, s.ReadBoolean());
     }
 }
 public void Read_Atom(byte[] buf, string want)
 {
     using (var s = new OtpInputStream(buf))
     {
         string atom = s.ReadAtom();
         Assert.AreEqual(want, atom);
     }
 }
 public void Can_Peek(byte[] buf, bool want)
 {
     using (var s = new OtpInputStream(buf))
     {
         byte b = s.Read1();
         Assert.AreEqual(OtpExternal.VersionTag, b);
         b = s.Peek();
         Assert.AreEqual(OtpExternal.AtomTag, b);
         string atom = s.ReadAtom();
         Assert.AreEqual(want.ToString().ToLowerInvariant(), atom); 
     }
 }
 public void Read_Zero_Throws_At_End()
 {
     byte[] inbuf = { 0, 1, 2, 3 };
     byte[] outbuf = new byte[1];
     using (var s = new OtpInputStream(inbuf))
     {
         s.ReadN(outbuf);
         s.ReadN(outbuf);
         s.ReadN(outbuf);
         s.ReadN(outbuf);
         Assert.Throws(typeof(Exception), () => s.ReadN(outbuf, 0, 0));
     }
 }
        public void Read_Binary(string want)
        {
            byte[] buf = null;
            using (var os = new OtpOutputStream())
            {
                os.WriteStringAsBinary(want);
                buf = os.ToArray();
            }

            using (var s = new OtpInputStream(buf))
            {
                string got = s.ReadBinaryAsString();
                Assert.AreEqual(want, got);
            }
        }
        public void Read_Long(byte[] buf, long want)
        {
            long got = 0;
            using (var s = new OtpInputStream(buf))
            {
                got = s.ReadLong();
            }

            Assert.AreEqual(want, got);
        }
 public void Read_FloatAsDouble(byte[] buf, double want)
 {
     using (var s = new OtpInputStream(buf))
     {
         double got = s.ReadDouble();
         Assert.AreEqual(want, got);
     }
 }
Exemplo n.º 8
0
        public static RiakException MaybeRiakError(byte[] response)
        {
            RiakException rv = null;

            if (EnumerableUtil.IsNullOrEmpty(response))
            {
                string errMsg = "TTB request returned null or zero-length data buffer.";
                rv = new RiakException(0, errMsg, false);
            }

            using (var s = new OtpInputStream(response))
            {
                string atom;
                byte tag = s.Peek1SkipVersion();
                switch (tag)
                {
                    case OtpExternal.AtomTag:
                        atom = s.ReadAtom();
                        if (atom.Equals(RpbErrorRespAtom))
                        {
                            throw new RiakException(0, RpbErrorRespEmpty, false);
                        }

                        break;
                    case OtpExternal.SmallTupleTag:
                    case OtpExternal.LargeTupleTag:
                        int arity = s.ReadTupleHead();
                        if (arity >= 1)
                        {
                            tag = s.Peek();
                            if (tag == OtpExternal.AtomTag)
                            {
                                atom = s.ReadAtom();
                                if (atom.Equals(RpbErrorRespAtom))
                                {
                                    arity--; // We've read one item in the tuple
                                    string errMsg = RpbErrorRespEmpty;
                                    int errCode = 0;

                                    for (int i = 0; i < arity; ++i)
                                    {
                                        tag = s.Peek();
                                        if (tag == OtpExternal.BinTag)
                                        {
                                            errMsg = s.ReadBinaryAsString();
                                        }
                                        else if (s.IsLongTag(tag))
                                        {
                                            errCode = (int)s.ReadLong();
                                        }
                                        else
                                        {
                                            errMsg = string.Format("Unexpected tag {0} in {1}", tag, RpbErrorRespAtom);
                                            errCode = 0;
                                            break;
                                        }
                                    }

                                    rv = new RiakException(errCode, errMsg, false);
                                }
                            }
                        }

                        break;
                }
            }

            return rv;
        }
Exemplo n.º 9
0
        public static RiakException MaybeRiakError(byte[] response)
        {
            RiakException rv = null;

            if (EnumerableUtil.IsNullOrEmpty(response))
            {
                string errMsg = "TTB request returned null or zero-length data buffer.";
                rv = new RiakException(0, errMsg, false);
            }

            using (var s = new OtpInputStream(response))
            {
                string atom;
                byte   tag = s.Peek1SkipVersion();
                switch (tag)
                {
                case OtpExternal.AtomTag:
                    atom = s.ReadAtom();
                    if (atom.Equals(RpbErrorRespAtom))
                    {
                        throw new RiakException(0, RpbErrorRespEmpty, false);
                    }

                    break;

                case OtpExternal.SmallTupleTag:
                case OtpExternal.LargeTupleTag:
                    int arity = s.ReadTupleHead();
                    if (arity >= 1)
                    {
                        tag = s.Peek();
                        if (tag == OtpExternal.AtomTag)
                        {
                            atom = s.ReadAtom();
                            if (atom.Equals(RpbErrorRespAtom))
                            {
                                arity--;     // We've read one item in the tuple
                                string errMsg  = RpbErrorRespEmpty;
                                int    errCode = 0;

                                for (int i = 0; i < arity; ++i)
                                {
                                    tag = s.Peek();
                                    if (tag == OtpExternal.BinTag)
                                    {
                                        errMsg = s.ReadBinaryAsString();
                                    }
                                    else if (s.IsLongTag(tag))
                                    {
                                        errCode = (int)s.ReadLong();
                                    }
                                    else
                                    {
                                        errMsg  = string.Format("Unexpected tag {0} in {1}", tag, RpbErrorRespAtom);
                                        errCode = 0;
                                        break;
                                    }
                                }

                                rv = new RiakException(errCode, errMsg, false);
                            }
                        }
                    }

                    break;
                }
            }

            return(rv);
        }