RiakClient.Erlang.OtpInputStream.ReadLong C# (CSharp) Method

ReadLong() public method

Read an array of bytes
public ReadLong ( ) : long
return long
        public long ReadLong()
        {
            byte[] nb;
            byte tag = Read1SkipVersion();
            switch (tag)
            {
                case OtpExternal.SmallIntTag:
                    return Read1();
                case OtpExternal.IntTag:
                    nb = new byte[4];
                    ReadN(nb); // Big endian
                    if (BitConverter.IsLittleEndian)
                    {
                        Array.Reverse(nb);
                    }

                    return BitConverter.ToInt32(nb, 0);
                case OtpExternal.SmallBigTag:
                case OtpExternal.LargeBigTag:
                    int arity;
                    byte sign;
                    if (tag == OtpExternal.SmallBigTag)
                    {
                        arity = Read1();
                        sign = Read1();
                    }
                    else
                    {
                        arity = Read4BE();
                        sign = Read1();
                    }

                    if (arity > 8)
                    {
                        // FUTURE: support BigInteger
                        throw new Exception("integer is too big to decode");
                    }

                    nb = new byte[8];

                    // Value is little endian.
                    ReadN(nb, 0, arity);

                    if (!BitConverter.IsLittleEndian)
                    {
                        Array.Reverse(nb);
                    }

                    ulong val = BitConverter.ToUInt64(nb, 0);

                    if (sign == OtpExternal.Negative)
                    {
                        return -(long)val;
                    }

                    return (long)val;
                default:
                    throw new Exception("Not valid integer tag: " + tag);
            }
        }

Usage Example

Ejemplo n.º 1
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;
        }
All Usage Examples Of RiakClient.Erlang.OtpInputStream::ReadLong