ReadUtf8() public static method

public static ReadUtf8 ( Stream stream, int bytesCount, StringBuilder builder, bool replace ) : int
stream Stream
bytesCount int
builder StringBuilder
replace bool
return int
Example #1
0
        private static CBORObject ReadString(Stream stream, char firstChar)
        {
            var builder = new StringBuilder();

            if (firstChar < (int)'0' && firstChar > (int)'9')
            {
                throw new CBORException("Invalid integer encoding");
            }
            builder.Append(firstChar);
            while (true)
            {
                int c = stream.ReadByte();
                if (c < 0)
                {
                    throw new CBORException("Premature end of data");
                }
                if (c >= (int)'0' && c <= (int)'9')
                {
                    builder.Append((char)c);
                }
                else if (c == (int)':')
                {
                    break;
                }
                else
                {
                    throw new CBORException("Invalid integer encoding");
                }
            }
            string s = builder.ToString();

            if (s.Length >= 2 && s[0] == '0' && s[1] == '0')
            {
                throw new CBORException("Invalid integer encoding");
            }
            EInteger numlength = EInteger.FromString(s);

            if (!numlength.CanFitInInt32())
            {
                throw new CBORException("Length too long");
            }
            builder = new StringBuilder();
            switch (DataUtilities.ReadUtf8(
                        stream,
                        numlength.ToInt32Checked(),
                        builder,
                        false))
            {
            case -2:
                throw new CBORException("Premature end of data");

            case -1:
                throw new CBORException("Invalid UTF-8");
            }
            return(CBORObject.FromObject(builder.ToString()));
        }
Example #2
0
        private static CBORObject readString(Stream stream, char firstChar)
        {
            var builder = new StringBuilder();

            if (firstChar < (int)'0' && firstChar > (int)'9')
            {
                throw new CBORException("Invalid integer encoding");
            }
            builder.Append(firstChar);
            while (true)
            {
                int c = stream.ReadByte();
                if (c < 0)
                {
                    throw new CBORException("Premature end of data");
                }
                if (c >= (int)'0' && c <= (int)'9')
                {
                    builder.Append((char)c);
                }
                else if (c == (int)':')
                {
                    break;
                }
                else
                {
                    throw new CBORException("Invalid integer encoding");
                }
            }
            CBORObject number = CBORDataUtilities.ParseJSONNumber(
                builder.ToString(),
                true,
                true);
            var length = 0;

            try {
                length = number.AsInt32();
            } catch (ArithmeticException ex) {
                throw new CBORException("Length too long", ex);
            }
            builder = new StringBuilder();
            switch (DataUtilities.ReadUtf8(stream, length, builder, false))
            {
            case -2:
                throw new CBORException("Premature end of data");

            case -1:
                throw new CBORException("Invalid UTF-8");
            }
            return(CBORObject.FromObject(builder.ToString()));
        }
Example #3
0
        /// <include file='../docs.xml'
        /// path='docs/doc[@name="M:PeterO.DataUtilities.ReadUtf8ToString(System.IO.Stream,System.Int32,System.Boolean)"]/*'/>
        public static string ReadUtf8ToString(
            Stream stream,
            int bytesCount,
            bool replace)
        {
            var builder = new StringBuilder();

            if (DataUtilities.ReadUtf8(stream, bytesCount, builder, replace) == -1)
            {
                throw new IOException(
                          "Unpaired surrogate code point found.",
                          new ArgumentException("Unpaired surrogate code point found."));
            }
            return(builder.ToString());
        }