K2Informatics.Erlnet.OtpInputStream.read_string C# (CSharp) Method

read_string() public method

public read_string ( ) : String
return String
        public String read_string()
        {
            int tag;
            int len;
            byte[] strbuf;
            tag = read1skip_version();
            switch (tag)
            {
                case OtpExternal.stringTag:
                    len = read2BE();
                    strbuf = new byte[len];
                    this.readN(strbuf);
                    return OtpErlangString.newString(strbuf);
                case OtpExternal.nilTag:
                    return "";
                case OtpExternal.binTag:                        // UTF8-PATCH
                    len = read4BE();
                    byte[] bin = new byte[len];
                    this.readN(bin);
                    return Encoding.UTF8.GetString(bin);
                case OtpExternal.listTag: // List when unicode +
                    len = read4BE();
                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < len; i++)
                    {
                        uint cp = (uint)read_int();
                        if (!OtpErlangString.isValidCodePoint((int)cp))
                        {
                            throw new OtpErlangDecodeException("Invalid CodePoint: " + cp);
                        }
                        if (1 <= (cp >> 16))
                        {
                            cp -= 0x10000;
                            char high = (char)((cp / 0x400) + 0xD800);
                            char low = (char)((cp % 0x400) + 0xDC00);
                            sb.Append(high);
                            sb.Append(low);
                        }
                        else
                        {
                            sb.Append((char)cp);
                        }
                    }
                    read_nil();
                    return sb.ToString();
                default:
                    throw new OtpErlangDecodeException("Wrong tag encountered, expected " + OtpExternal.stringTag
                                       + " or " + OtpExternal.listTag + ", got " + tag);
            }
        }

Usage Example

Ejemplo n.º 1
0
 /**
  * Create an Erlang string from a stream containing a string encoded in
  * Erlang external format.
  *
  * @param buf
  *            the stream containing the encoded string.
  *
  * @exception OtpErlangDecodeException
  *                if the buffer does not contain a valid external
  *                representation of an Erlang string.
  */
 public OtpErlangString(OtpInputStream buf)
 {
     str = buf.read_string();
 }