Ejemplo n.º 1
0
 internal static byte[] EncodeToBytes(string str, Encoding encoding)
 {
     if (encoding == null)
     {
         encoding = ContentHelper.GetDefaultEncoding();
     }
     return(encoding.GetBytes(str));
 }
Ejemplo n.º 2
0
 internal static string DecodeStream(MemoryStream stream, Encoding encoding)
 {
     if (encoding == null)
     {
         encoding = ContentHelper.GetDefaultEncoding();
     }
     byte[] bytes = stream.ToArray();
     return(encoding.GetString(bytes));
 }
Ejemplo n.º 3
0
        internal static byte[] EncodeToBytes(string str, Encoding encoding)
        {
            if (encoding == null)
            {
                // just use the default encoding if one wasn't provided
                encoding = ContentHelper.GetDefaultEncoding();
            }

            return(encoding.GetBytes(str));
        }
Ejemplo n.º 4
0
        internal static string DecodeStream(Stream stream, Encoding encoding)
        {
            if (null == encoding)
            {
                // just use the default encoding if one wasn't provided
                encoding = ContentHelper.GetDefaultEncoding();
            }

            StringBuilder result  = new StringBuilder(capacity: ChunkSize);
            Decoder       decoder = encoding.GetDecoder();

            int useBufferSize = 64;

            if (useBufferSize < encoding.GetMaxCharCount(10))
            {
                useBufferSize = encoding.GetMaxCharCount(10);
            }
            char[] chars = new char[useBufferSize];


            byte[] bytes     = new byte[useBufferSize * 4];
            int    bytesRead = 0;

            do
            {
                // Read at most the number of bytes that will fit in the input buffer. The
                // return value is the actual number of bytes read, or zero if no bytes remain.
                bytesRead = stream.Read(bytes, 0, useBufferSize * 4);

                bool completed = false;
                int  byteIndex = 0;
                int  bytesUsed;
                int  charsUsed;

                while (!completed)
                {
                    // If this is the last input data, flush the decoder's internal buffer and state.
                    bool flush = (bytesRead == 0);
                    decoder.Convert(bytes, byteIndex, bytesRead - byteIndex,
                                    chars, 0, useBufferSize, flush,
                                    out bytesUsed, out charsUsed, out completed);

                    // The conversion produced the number of characters indicated by charsUsed. Write that number
                    // of characters to our result buffer
                    result.Append(chars, 0, charsUsed);

                    // Increment byteIndex to the next block of bytes in the input buffer, if any, to convert.
                    byteIndex += bytesUsed;
                }
            }while (bytesRead != 0);

            return(result.ToString());
        }
Ejemplo n.º 5
0
        internal static string DecodeStream(Stream stream, ref Encoding encoding)
        {
            bool isDefaultEncoding = false;

            if (encoding == null)
            {
                // Use the default encoding if one wasn't provided
                encoding          = ContentHelper.GetDefaultEncoding();
                isDefaultEncoding = true;
            }

            string content = StreamToString(stream, encoding);

            if (isDefaultEncoding)
            {
                do
                {
                    // check for a charset attribute on the meta element to override the default.
                    Match match = s_metaexp.Match(content);
                    if (match.Success)
                    {
                        Encoding localEncoding = null;
                        string   characterSet  = match.Groups["charset"].Value;

                        if (TryGetEncoding(characterSet, out localEncoding))
                        {
                            stream.Seek(0, SeekOrigin.Begin);
                            content = StreamToString(stream, localEncoding);
                            // report the encoding used.
                            encoding = localEncoding;
                        }
                    }
                } while (false);
            }

            return(content);
        }