GetDecoder() public method

public GetDecoder ( ) : Decoder
return Decoder
        public override int GetCharCount(byte[] bytes, int index, int count)
        {
            int result;

            try
            {
                result = defaultEncoding.GetDecoder().GetCharCount(bytes, index, count, true);
            }
            catch (DecoderFallbackException)
            {
                result = isoEncoding.GetDecoder().GetCharCount(bytes, index, count, true);
            }

            return(result);
        }
        private void Init(Stream stream, Encoding encoding, bool detectEncodingFromByteOrderMarks, int bufferSize, bool leaveOpen)
        {
            _stream = stream;
            _encoding = encoding;
            _decoder = encoding.GetDecoder();
            if (bufferSize < MinBufferSize)
            {
                bufferSize = MinBufferSize;
            }

            _byteBuffer = new byte[bufferSize];
            _maxCharsPerBuffer = encoding.GetMaxCharCount(bufferSize);
            _charBuffer = new char[_maxCharsPerBuffer];
            _byteLen = 0;
            _bytePos = 0;
            _detectEncoding = detectEncodingFromByteOrderMarks;

            // Encoding.GetPreamble() always allocates and returns a new byte[] array for
            // encodings that have a preamble.
            // We can avoid repeated allocations for the default and commonly used Encoding.UTF8
            // encoding by using our own private cached instance of the UTF8 preamble.
            // We specifically look for Encoding.UTF8 because we know it has a preamble,
            // whereas other instances of UTF8Encoding may not have a preamble enabled, and
            // there's no public way to tell if the preamble is enabled for an instance other
            // than calling GetPreamble(), which we're trying to avoid.
            // This means that other instances of UTF8Encoding are excluded from this optimization.
            _preamble = object.ReferenceEquals(encoding, Encoding.UTF8) ?
                (s_utf8Preamble ?? (s_utf8Preamble = encoding.GetPreamble())) :
                encoding.GetPreamble();

            _checkPreamble = (_preamble.Length > 0);
            _isBlocked = false;
            _closable = !leaveOpen;
        }
Example #3
0
 /// <summary>
 /// Create new C file access
 /// </summary>
 public CFile(Stream stream, Encoding encoding = null)
 {
     this._Stream = stream;
     EOF = _Stream == null;
     this._Encoding = encoding ?? Encoding.GetEncoding("Windows-1252");
     this._Decoder = _Encoding.GetDecoder();
 }
 public BinaryReader(Stream input, Encoding encoding)
 {
     if (input == null)
     {
         throw new ArgumentNullException("input");
     }
     if (encoding == null)
     {
         throw new ArgumentNullException("encoding");
     }
     if (!input.CanRead)
     {
         throw new ArgumentException(Environment.GetResourceString("Argument_StreamNotReadable"));
     }
     this.m_stream = input;
     this.m_decoder = encoding.GetDecoder();
     this.m_maxCharsSize = encoding.GetMaxCharCount(0x80);
     int maxByteCount = encoding.GetMaxByteCount(1);
     if (maxByteCount < 0x10)
     {
         maxByteCount = 0x10;
     }
     this.m_buffer = new byte[maxByteCount];
     this.m_charBuffer = null;
     this.m_charBytes = null;
     this.m_2BytesPerChar = encoding is UnicodeEncoding;
     this.m_isMemoryStream = this.m_stream.GetType() == typeof(MemoryStream);
 }
        public HttpRequestStreamReader(Stream stream, Encoding encoding, int bufferSize)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            if (!stream.CanRead)
            {
                throw new ArgumentException(Resources.HttpRequestStreamReader_StreamNotReadable, nameof(stream));
            }

            if (encoding == null)
            {
                throw new ArgumentNullException(nameof(encoding));
            }

            _stream = stream;
            _encoding = encoding;
            _decoder = encoding.GetDecoder();

            if (bufferSize < MinBufferSize)
            {
                bufferSize = MinBufferSize;
            }

            _byteBufferSize = bufferSize;
            _byteBuffer = new byte[bufferSize];
            var maxCharsPerBuffer = encoding.GetMaxCharCount(bufferSize);
            _charBuffer = new char[maxCharsPerBuffer];
        }
 public ContentReader(Stream stream, long length, Encoding encoding = null) {
     _stream = stream;
     _length = length;
     if (encoding != null) {
         _decoder = encoding.GetDecoder();
     }
 }
        /// <summary>
        /// Constructs a new binary reader with the given bit converter, reading
        /// to the given stream, using the given encoding.
        /// </summary>
        /// <param name="bitConverter">Converter to use when reading data</param>
        /// <param name="stream">Stream to read data from</param>
        /// <param name="encoding">Encoding to use when reading character data</param>
        public EndianBinaryReader(EndianBitConverter bitConverter, Stream stream, Encoding encoding)
        {
            if (bitConverter == null)
            {
                throw new ArgumentNullException("bitConverter");
            }
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }
            if (encoding == null)
            {
                throw new ArgumentNullException("encoding");
            }
            if (!stream.CanRead)
            {
                throw new ArgumentException("Stream isn't writable", "stream");
            }
            this.stream = stream;
            this.bitConverter = bitConverter;
            this.encoding = encoding;
            this.decoder = encoding.GetDecoder();
            this.minBytesPerChar = 1;

            if (encoding is UnicodeEncoding)
            {
                minBytesPerChar = 2;
            }
        }
 public EndianBinaryReader(EndianBitConverter bitConverter, Stream stream, System.Text.Encoding encoding)
 {
     this.disposed = false;
     this.buffer = new byte[0x10];
     this.charBuffer = new char[1];
     if (bitConverter == null)
     {
         throw new ArgumentNullException("bitConverter");
     }
     if (stream == null)
     {
         throw new ArgumentNullException("stream");
     }
     if (encoding == null)
     {
         throw new ArgumentNullException("encoding");
     }
     if (!stream.CanRead)
     {
         throw new ArgumentException("Stream isn't writable", "stream");
     }
     this.stream = stream;
     this.bitConverter = bitConverter;
     this.encoding = encoding;
     this.decoder = encoding.GetDecoder();
     this.minBytesPerChar = 1;
     if (encoding is UnicodeEncoding)
     {
         this.minBytesPerChar = 2;
     }
 }
Example #9
0
        public BinaryReader(Stream input, Encoding encoding, bool leaveOpen) {
            if (input==null) {
                throw new ArgumentNullException(nameof(input));
            }
            if (encoding==null) {
                throw new ArgumentNullException(nameof(encoding));
            }
            if (!input.CanRead)
                throw new ArgumentException(Environment.GetResourceString("Argument_StreamNotReadable"));
            Contract.EndContractBlock();
            m_stream = input;
            m_decoder = encoding.GetDecoder();
            m_maxCharsSize = encoding.GetMaxCharCount(MaxCharBytesSize);
            int minBufferSize = encoding.GetMaxByteCount(1);  // max bytes per one char
            if (minBufferSize < 16) 
                minBufferSize = 16;
            m_buffer = new byte[minBufferSize];
            // m_charBuffer and m_charBytes will be left null.

            // For Encodings that always use 2 bytes per char (or more), 
            // special case them here to make Read() & Peek() faster.
            m_2BytesPerChar = encoding is UnicodeEncoding;
            // check if BinaryReader is based on MemoryStream, and keep this for it's life
            // we cannot use "as" operator, since derived classes are not allowed
            m_isMemoryStream = (m_stream.GetType() == typeof(MemoryStream));
            m_leaveOpen = leaveOpen;

            Contract.Assert(m_decoder!=null, "[BinaryReader.ctor]m_decoder!=null");
        }
Example #10
0
 public EndianBinaryReader(EndianBitConverter bitConverter, Stream stream, System.Text.Encoding encoding)
 {
     this.disposed   = false;
     this.buffer     = new byte[0x10];
     this.charBuffer = new char[1];
     if (bitConverter == null)
     {
         throw new ArgumentNullException("bitConverter");
     }
     if (stream == null)
     {
         throw new ArgumentNullException("stream");
     }
     if (encoding == null)
     {
         throw new ArgumentNullException("encoding");
     }
     if (!stream.CanRead)
     {
         throw new ArgumentException("Stream isn't writable", "stream");
     }
     this.stream          = stream;
     this.bitConverter    = bitConverter;
     this.encoding        = encoding;
     this.decoder         = encoding.GetDecoder();
     this.minBytesPerChar = 1;
     if (encoding is UnicodeEncoding)
     {
         this.minBytesPerChar = 2;
     }
 }
Example #11
0
        public EndianReader(Stream input, Endianness endianess, Encoding encoding, bool leaveOpen)
        {
            if (input == null)
            {
                throw new ArgumentNullException(nameof(input));
            }
            if (encoding == null)
            {
                throw new ArgumentNullException(nameof(encoding));
            }
            if (!input.CanRead)
                throw new ArgumentException("Can't read from the output stream", nameof(input));
            Contract.EndContractBlock();

            BaseStream = input;
            decoder = encoding.GetDecoder();
            maxCharsSize = encoding.GetMaxCharCount(MaxCharBytesSize);
            var minBufferSize = encoding.GetMaxByteCount(1);  // max bytes per one char
            if (minBufferSize < 16)
                minBufferSize = 16;
            buffer = new byte[minBufferSize];
            // m_charBuffer and m_charBytes will be left null.

            // For Encodings that always use 2 bytes per char (or more), 
            // special case them here to make Read() & Peek() faster.
            use2BytesPerChar = encoding is UnicodeEncoding;
            this.leaveOpen = leaveOpen;

            Endianness = endianess;
            resolvedEndianess = EndianessHelper.Resolve(endianess);

            Contract.Assert(decoder != null, "[EndianReader.ctor]m_decoder!=null");
        }
Example #12
0
        public string DecodeByteToString(byte[] byteArray, System.Text.Encoding encoding)
        {
            char[] chars          = new char[byteArray.Length + 1];
            System.Text.Decoder d = encoding.GetDecoder();
            int charLen           = d.GetChars(byteArray, 0, byteArray.Length, chars, 0);

            return(new System.String(chars));
        }
 public PositionedStreamReader(Stream stream, Encoding encoding)
 {
     this.stream = stream;
     this.encoding = encoding;
     decoder = encoding.GetDecoder();
     bufferpos = 0;
     readPosition = 0;
 }
 public static string ConvertEncoding(string value, Encoding src, Encoding trg)
 {
     Decoder dec = src.GetDecoder();
     byte[] ba = trg.GetBytes(value);
     int len = dec.GetCharCount(ba, 0, ba.Length);
     char[] ca = new char[len];
     dec.GetChars(ba, 0, ba.Length, ca, 0);
     return new string(ca);
 }
Example #15
0
 TextSource(Encoding encoding)
 {
     _buffer = new Byte[BufferSize];
     _chars = new Char[BufferSize + 1];
     _raw = new MemoryStream();
     _index = 0;
     _encoding = encoding ?? TextEncoding.Utf8;
     _decoder = _encoding.GetDecoder();
 }
Example #16
0
 public AsyncLineReader(Stream stream, Encoding encoding, int bufferSize, int maxLineLength)
 {
     this.stream = stream;
     this.decoder = encoding.GetDecoder();
     this.readBuffer = new byte[bufferSize];
     this.decodeBuffer = new char[bufferSize];
     this.maxLineLength = maxLineLength;
     StartRead();
 }
 public CancellableStreamReader(Stream stream, Encoding encoding)
 {
     _stream = stream;
     _decoder = encoding.GetDecoder();
     _byteBuffer = new byte[BufferLength];
     _buffer = new char[encoding.GetMaxCharCount(BufferLength)];
     _bufferedLength = -1;
     _bufferCursor = -1;
 }
Example #18
0
 public AsyncTextReader(IAsyncDataSource dataSource, Encoding encoding, int bufferSize = DefaultBufferSize)
     : base()
 {
     _DataSource = dataSource;
     _Encoding = encoding;
     _Decoder = _Encoding.GetDecoder();
     _BufferSize = Math.Max(MinimumBufferSize, bufferSize);
     AllocateBuffer();
 }
Example #19
0
		public MiniStream (IMiniStreamSink sink, Encoding encoding, int buf_size) {
			if (sink == null)
				throw new ArgumentNullException ();

			this.sink = sink;
			this.sb = new StringBuilder ();
			this.decoder = encoding.GetDecoder ();

			this.buf = new char[buf_size];
		}
 public MyBinaryReader(Stream stream, Encoding encoding)
     : base(stream, encoding)
 {
     this.m_decoder = encoding.GetDecoder();
     this.m_maxCharsSize = encoding.GetMaxCharCount(0x80);
     int maxByteCount = encoding.GetMaxByteCount(1);
     if (maxByteCount < 0x10)
     {
         maxByteCount = 0x10;
     }
 }
Example #21
0
        /// <summary>Initializes a new reusable reader.</summary>
        /// <param name="encoding">The Encoding to use.  Defaults to UTF8.</param>
        /// <param name="bufferSize">The size of the buffer to use when reading from the stream.</param>
        public ReusableTextReader(Encoding encoding = null, int bufferSize = 1024)
        {
            if (encoding == null)
            {
                encoding = Encoding.UTF8;
            }

            _decoder = encoding.GetDecoder();
            _bytes = new byte[bufferSize];
            _chars = new char[encoding.GetMaxCharCount(_bytes.Length)];
        }
		public BinaryReader(Stream input, Encoding encoding) {
			if (input == null || encoding == null) 
				throw new ArgumentNullException(Locale.GetText ("Input or Encoding is a null reference."));
			if (!input.CanRead)
				throw new ArgumentException(Locale.GetText ("The stream doesn't support reading."));

			m_stream = input;
			m_encoding = encoding;
			decoder = encoding.GetDecoder ();
			m_buffer = new byte [32];
		}
 internal DryadLinqBinaryReader(NativeBlockStream stream, Encoding encoding)
 {
     this.m_nativeStream = stream;
     this.m_encoding = encoding;
     this.m_decoder = encoding.GetDecoder();
     this.m_curDataBlockInfo.DataBlock = null;
     this.m_curDataBlockInfo.BlockSize = -1;
     this.m_curDataBlockInfo.ItemHandle = IntPtr.Zero;
     this.m_curDataBlock = this.m_curDataBlockInfo.DataBlock;
     this.m_curBlockSize = this.m_curDataBlockInfo.BlockSize;
     this.m_curBlockPos = -1;
     this.m_isClosed = false;
 }
Example #24
0
        public static String convert(string sourceValue, Encoding source, Encoding target)
        {
            Encoder encoder = source.GetEncoder();
            Decoder decoder = target.GetDecoder();

            byte[] cpBytes = source.GetBytes(sourceValue);
            int bytesCount = source.GetByteCount(sourceValue);
            byte[] utfBytes = Encoding.Convert(source, target, cpBytes);
            char[] utfChars = new char[bytesCount];
            decoder.GetChars(utfBytes, 0, utfBytes.Length, utfChars, 0);

            return new String(utfChars);
        }
Example #25
0
 static public int GetDecoder(IntPtr l)
 {
     try {
         System.Text.Encoding self = (System.Text.Encoding)checkSelf(l);
         var ret = self.GetDecoder();
         pushValue(l, true);
         pushValue(l, ret);
         return(2);
     }
     catch (Exception e) {
         return(error(l, e));
     }
 }
Example #26
0
		public BinaryReader(Stream input, Encoding encoding) {
			if (input == null || encoding == null) 
				throw new ArgumentNullException(Locale.GetText ("Input or Encoding is a null reference."));
			if (!input.CanRead)
				throw new ArgumentException(Locale.GetText ("The stream doesn't support reading."));

			m_stream = input;
			m_encoding = encoding;
			decoder = encoding.GetDecoder ();
			
			// internal buffer size is documented to be between 16 and the value
			// returned by GetMaxByteCount for the specified encoding
			m_buffer = new byte [Math.Max (16, encoding.GetMaxByteCount (1))];
		}
Example #27
0
        private CancellationTokenSource _disposalTokenSource;    // Used to indicate that a cancellation is requested due to disposal

        internal SqlSequentialTextReader(SqlDataReader reader, int columnIndex, Encoding encoding)
        {
            Debug.Assert(reader != null, "Null reader when creating sequential textreader");
            Debug.Assert(columnIndex >= 0, "Invalid column index when creating sequential textreader");
            Debug.Assert(encoding != null, "Null encoding when creating sequential textreader");

            _reader = reader;
            _columnIndex = columnIndex;
            _encoding = encoding;
            _decoder = encoding.GetDecoder();
            _leftOverBytes = null;
            _peekedChar = -1;
            _currentTask = null;
            _disposalTokenSource = new CancellationTokenSource();
        }
 private void Init(Process process, Stream stream, UserCallBack callback, Encoding encoding, int bufferSize) {
     this.process = process;
     this.stream = stream;
     this.encoding = encoding;
     this.userCallBack = callback;
     decoder = encoding.GetDecoder();
     if (bufferSize < MinBufferSize) bufferSize = MinBufferSize;
     byteBuffer = new byte[bufferSize];
     _maxCharsPerBuffer = encoding.GetMaxCharCount(bufferSize);
     charBuffer = new char[_maxCharsPerBuffer];
     cancelOperation = false;
     eofEvent = new ManualResetEvent(false);
     sb = null;
     this.bLastCarriageReturn = false;
 }
Example #29
0
        public TcpBinaryReader(Stream input, Encoding encoding)
            : base(input, encoding)
        {
            m_stream = input;
            this.m_decoder = encoding.GetDecoder();
            this.m_maxCharsSize = encoding.GetMaxCharCount(0x80);
            int maxByteCount = encoding.GetMaxByteCount(1);
            if (maxByteCount < 0x10)
            {
                maxByteCount = 0x10;
            }

            this.m_buffer = new byte[maxByteCount];
            this.m_2BytesPerChar = encoding is UnicodeEncoding;
            this.m_isMemoryStream = this.m_stream.GetType() == typeof(MemoryStream);
        }
Example #30
0
        internal NpgsqlBuffer(Stream underlying, int size, Encoding textEncoding)
        {
            if (size < MinimumBufferSize) {
                throw new ArgumentOutOfRangeException("size", size, "Buffer size must be at least " + MinimumBufferSize);
            }
            Contract.EndContractBlock();

            Underlying = underlying;
            Size = size;
            _buf = new byte[Size];
            TextEncoding = textEncoding;
            _textDecoder = TextEncoding.GetDecoder();
            _textEncoder = TextEncoding.GetEncoder();
            _tempCharBuf = new char[1024];
            _workspace = new byte[8];
        }
Example #31
0
 public static string ReadLine(this Stream stream, Encoding encoding = null)
 {
     encoding = encoding ?? Encoding.UTF8;
     var result = new StringBuilder();
     var decoder = encoding.GetDecoder();
     while (true)
     {
         var val = ReadMinimum(stream, decoder);
         if (val == null) throw new EndOfStreamException();
         if (val == "\n") return result.ToString();
         if (val != "\r")
         {
             result.Append(val);
         }
     }
 }
Example #32
0
		// If we're starting from the beginning of the file, we need to skip the BOM since it's
		// not part of the file. If we're starting partway through, there's no need to do that.
		//
		// It may seem strange allowing specifying the FileAccess, but it is useful as a safeguard
		// against other people already having the file open with e.g. FileAccess.Write and FileShare.Read.
		Utf8LineReader(string path, bool skipBOM, FileAccess access, FileShare share) {
			disposeStream = true;
			BaseStream = new FileStream(path, FileMode.Open, access, share);

			encoding = Encoding.UTF8;
			decoder = encoding.GetDecoder();
			lineBuilder = new StringBuilder();

			offset = 0;
			buffer = new byte[BufferSize];
			bytesInBuffer = 0;
			Line = 0;

			if (skipBOM)
				SkipBOM();
		}
Example #33
0
 /// <summary>
 /// Initializes an instance of DryadLinqTextReader.
 /// </summary>
 /// <param name="stream">A native stream to read from.</param>
 /// <param name="encoding">The text encoding.</param>
 public DryadLinqTextReader(NativeBlockStream stream, Encoding encoding)
 {
     this.m_nativeStream = stream;
     this.m_encoding = encoding;
     this.m_decoder = encoding.GetDecoder();
     this.m_curDataBlockInfo.DataBlock = null;
     this.m_curDataBlockInfo.BlockSize = -1;
     this.m_curDataBlockInfo.ItemHandle = IntPtr.Zero;
     this.m_curBlockPos = 0;
     this.m_decodeUnitCharSize = this.m_encoding.GetMaxCharCount(DecodeUnitByteSize);
     this.m_charBuff = new char[this.m_decodeUnitCharSize + 2];  //allow 2 bytes for trailing newline
     this.m_charBuffEnd = 0;
     this.m_curLineStart = 0;
     this.m_curLineEnd = 0;
     this.m_isClosed = false;
 }
Example #34
0
        // Creates a new AsyncStreamReader for the given stream. The
        // character encoding is set by encoding and the buffer size,
        // in number of 16-bit characters, is set by bufferSize.
        internal AsyncStreamReader(Stream stream, Action<string> callback, Encoding encoding)
        {
            Debug.Assert(stream != null && encoding != null && callback != null, "Invalid arguments!");
            Debug.Assert(stream.CanRead, "Stream must be readable!");

            _stream = stream;
            _userCallBack = callback;
            _decoder = encoding.GetDecoder();
            _byteBuffer = new byte[DefaultBufferSize];

            // This is the maximum number of chars we can get from one iteration in loop inside ReadBuffer.
            // Used so ReadBuffer can tell when to copy data into a user's char[] directly, instead of our internal char[].
            int maxCharsPerBuffer = encoding.GetMaxCharCount(DefaultBufferSize);
            _charBuffer = new char[maxCharsPerBuffer];

            _cts = new CancellationTokenSource();
            _messageQueue = new Queue<string>();
        }
Example #35
0
        /// <summary>
        /// Decodes a URL, like System.Web.HttpUtility.UrlDecode
        /// </summary>
        /// <returns>The decoded URL</returns>
        /// <param name="value">The URL fragment to decode</param>
        /// <param name="encoding">The encoding to use</param>
        public static string UrlDecode(string value, System.Text.Encoding encoding = null)
        {
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            encoding = encoding ?? System.Text.Encoding.UTF8;

            var decoder = encoding.GetDecoder();
            var inbuf   = new byte[8];
            var outbuf  = new char[8];

            return(RE_NUMBER.Replace(value, (m) =>
            {
                if (m.Value == "+")
                {
                    return " ";
                }

                try
                {
                    var hex = m.Groups["number"].Value;
                    var bytelen = hex.Length / 2;
                    Utility.HexStringAsByteArray(hex, inbuf);
                    var c = decoder.GetChars(inbuf, 0, bytelen, outbuf, 0);
                    return new string(outbuf, 0, c);
                }
                catch
                {
                }

                //Fallback
                return m.Value;
            }));
        }