public override int GetByteCount(char[] chars, int index, int count) { int result; try { result = isoEncoding.GetEncoder().GetByteCount(chars, index, count, true); } catch (EncoderFallbackException) { result = defaultEncoding.GetEncoder().GetByteCount(chars, index, count, true); } return(result); }
// Protected default constructor that sets the output stream // to a null stream (a bit bucket). protected BinaryWriter() { OutStream = Stream.Null; _buffer = new byte[16]; _encoding = EncodingCache.UTF8NoBOM; _encoder = _encoding.GetEncoder(); }
/// <summary> /// Encodes a URL, like System.Web.HttpUtility.UrlEncode /// </summary> /// <returns>The encoded URL</returns> /// <param name="value">The URL fragment to encode</param> /// <param name="encoding">The encoding to use</param> public static string UrlEncode(string value, System.Text.Encoding encoding = null, string spacevalue = "+") { if (value == null) { throw new ArgumentNullException(nameof(value)); } encoding = encoding ?? System.Text.Encoding.UTF8; var encoder = encoding.GetEncoder(); var inbuf = new char[1]; var outbuf = new byte[4]; return(RE_ESCAPECHAR.Replace(value, (m) => { if (m.Value == " ") { return spacevalue; } inbuf[0] = m.Value[0]; try { var len = encoder.GetBytes(inbuf, 0, 1, outbuf, 0, true); return "%" + BitConverter.ToString(outbuf, 0, len).Replace("-", "%"); } catch { } //Fallback return m.Value; })); }
// Protected default constructor that sets the output stream // to a null stream (a bit bucket). protected BinaryWriter() { OutStream = Stream.Null; _buffer = new byte[16]; _encoding = new UTF8Encoding(false, true); _encoder = _encoding.GetEncoder(); }
private void Init(Stream stream, System.Text.Encoding encoding, int bufferSize) { this.stream = stream; this.encoding = encoding; this.encoder = encoding.GetEncoder(); if (bufferSize < 0x80) { bufferSize = 0x80; } this.charBuffer = new char[bufferSize]; this.byteBuffer = new byte[encoding.GetMaxByteCount(bufferSize)]; this.charLen = bufferSize; if (stream.CanSeek && (stream.Position > 0L)) { this.haveWrittenPreamble = true; } this.closable = true; if (Mda.StreamWriterBufferedDataLost.Enabled) { string cs = null; if (Mda.StreamWriterBufferedDataLost.CaptureAllocatedCallStack) { cs = Environment.GetStackTrace(null, false); } this.mdaHelper = new MdaHelper(this, cs); } }
public HttpResponseStreamWriter(Stream stream, Encoding encoding, int bufferSize) { if (stream == null) { throw new ArgumentNullException(nameof(stream)); } if (!stream.CanWrite) { throw new ArgumentException(Resources.HttpResponseStreamWriter_StreamNotWritable, nameof(stream)); } if (encoding == null) { throw new ArgumentNullException(nameof(encoding)); } _stream = stream; Encoding = encoding; _charBufferSize = bufferSize; if (bufferSize < MinBufferSize) { bufferSize = MinBufferSize; } _encoder = encoding.GetEncoder(); _byteBuffer = new byte[encoding.GetMaxByteCount(bufferSize)]; _charBuffer = new char[bufferSize]; }
/// <summary> /// Encodes the specified <see cref="ReadOnlySpan{Char}"/> to <see langword="byte"/>s using the specified <see cref="Encoding"/> /// and writes the result to <paramref name="writer"/>. /// </summary> /// <param name="encoding">The <see cref="Encoding"/> which represents how the data in <paramref name="chars"/> should be encoded.</param> /// <param name="chars">The <see cref="ReadOnlySequence{Char}"/> to encode to <see langword="byte"/>s.</param> /// <param name="writer">The buffer to which the encoded bytes will be written.</param> /// <exception cref="EncoderFallbackException">Thrown if <paramref name="chars"/> contains data that cannot be encoded and <paramref name="encoding"/> is configured /// to throw an exception when such data is seen.</exception> public static long GetBytes(this Encoding encoding, ReadOnlySpan <char> chars, IBufferWriter <byte> writer) { if (encoding is null) { throw new ArgumentNullException(nameof(encoding)); } if (writer is null) { throw new ArgumentNullException(nameof(writer)); } if (chars.Length <= MaxInputElementsPerIteration) { // The input span is small enough where we can one-shot this. int byteCount = encoding.GetByteCount(chars); Span <byte> scratchBuffer = writer.GetSpan(byteCount); int actualBytesWritten = encoding.GetBytes(chars, scratchBuffer); writer.Advance(actualBytesWritten); return(actualBytesWritten); } else { // Allocate a stateful Encoder instance and chunk this. Convert(encoding.GetEncoder(), chars, writer, flush: true, out long totalBytesWritten, out bool completed); return(totalBytesWritten); } }
// Protected default constructor that sets the output stream // to a null stream (a bit bucket). protected EndianWriter() { OutStream = Stream.Null; buffer = new byte[16]; Encoding = new UTF8Encoding(false, true); encoder = Encoding.GetEncoder(); }
public HttpResponseStreamWriter(Stream stream, Encoding encoding, int bufferSize) { _stream = stream; Encoding = encoding; _encoder = encoding.GetEncoder(); _charBufferSize = bufferSize; _charBuffer = new ArraySegment<char>(new char[bufferSize]); _byteBuffer = new ArraySegment<byte>(new byte[encoding.GetMaxByteCount(bufferSize)]); }
public TextReaderStream(TextReader textReader, Encoding encoding, int bufferSize = 4096) { _textReader = textReader; _encoding = encoding; _maxByteCountPerChar = _encoding.GetMaxByteCount(1); _encoder = encoding.GetEncoder(); if (bufferSize <= 0) throw new ArgumentOutOfRangeException("bufferSize", "zero or negative"); _charBuffer = new char[bufferSize]; }
public HttpResponseStreamWriter(Stream stream, Encoding encoding, LeasedArraySegment<char> leasedCharBuffer, LeasedArraySegment<byte> leasedByteBuffer) { _stream = stream; Encoding = encoding; _encoder = encoding.GetEncoder(); _charBufferSize = leasedCharBuffer.Data.Count; _charBuffer = leasedCharBuffer.Data; _byteBuffer = leasedByteBuffer.Data; _leasedCharBuffer = leasedCharBuffer; _leasedByteBuffer = leasedByteBuffer; }
private TranscodingStream(Stream stream, Encoding outEncoding) { if (stream == null) throw new ArgumentNullException("stream"); if (outEncoding == null) throw new ArgumentNullException("outEncoding"); this._stream = stream; this._encoder = outEncoding.GetEncoder(); }
public static IntPtr StringToPtr(string value, Encoding encoding) { var encoder = encoding.GetEncoder(); var length = encoding.GetByteCount(value); // The encoded value is null terminated that's the reason for the '+1'. var encodedValue = new byte[length + 1]; encoding.GetBytes(value, 0, value.Length, encodedValue, 0); var handle = Marshal.AllocHGlobal(new IntPtr(encodedValue.Length)); Marshal.Copy(encodedValue, 0, handle, encodedValue.Length); return handle; }
/// <summary> /// /// </summary> /// <param name="input"></param> /// <param name="encoding"></param> public SHA1TextReader(TextReader input, Encoding encoding) : base() { this.input = input; this.encoder = encoding.GetEncoder(); this.sha1 = SHA1.Create(); this.ibuf = new char[bufferSize]; this.obuf = new byte[bufferSize]; this.ibufIndex = 0; }
public AsyncStreamWriter(Stream stream, Encoding encoding, int initialBufferCapacity = DefaultIniBufferCapacity, int maxBufferCapacity = DefaultMaxBufferCapacity) { if (initialBufferCapacity < 4) throw new ArgumentOutOfRangeException("initialBufferCapacity"); if (initialBufferCapacity > maxBufferCapacity) maxBufferCapacity = initialBufferCapacity; _stream = stream; _buffer = new byte[initialBufferCapacity]; _maxBufferCapacity = maxBufferCapacity; _encoder = encoding.GetEncoder(); }
public unsafe BufferedBinaryWriter(Stream output, Encoding encoding) : base(output, encoding) { this.memoryPage = Pool.GetPage(); this.basePtr = this.memoryPage.BasePtr; this.endPtr = this.memoryPage.EndPtr; this.ptr = this.basePtr; this.encoding = encoding; this.encoder = encoding.GetEncoder(); this.charMaxByteCount = encoding.IsSingleByte ? 1 : encoding.GetMaxByteCount(1); }
static public int GetEncoder(IntPtr l) { try { System.Text.Encoding self = (System.Text.Encoding)checkSelf(l); var ret = self.GetEncoder(); pushValue(l, true); pushValue(l, ret); return(2); } catch (Exception e) { return(error(l, e)); } }
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); }
public BinaryWriter(Stream output, Encoding encoding) { if (output==null) throw new ArgumentNullException("output"); if (encoding==null) throw new ArgumentNullException("encoding"); if (!output.CanWrite) throw new ArgumentException(Environment.GetResourceString("Argument_StreamNotWritable")); OutStream = output; _buffer = new byte[16]; _encoding = encoding; _encoder = _encoding.GetEncoder(); }
public SourceTextStream(SourceText source, int bufferSize = 2048, bool useDefaultEncodingIfNull = false) { Debug.Assert(source.Encoding != null || useDefaultEncodingIfNull); _source = source; _encoding = source.Encoding ?? s_utf8EncodingWithNoBOM; _encoder = _encoding.GetEncoder(); _minimumTargetBufferCount = _encoding.GetMaxByteCount(1); _sourceOffset = 0; _position = 0; _charBuffer = new char[Math.Min(bufferSize, _source.Length)]; _bufferOffset = 0; _bufferUnreadChars = 0; _preambleWritten = false; }
public BinaryWriter(Stream output, Encoding encoding, bool leaveOpen) { if (output==null) throw new ArgumentNullException(nameof(output)); if (encoding==null) throw new ArgumentNullException(nameof(encoding)); if (!output.CanWrite) throw new ArgumentException(Environment.GetResourceString("Argument_StreamNotWritable")); Contract.EndContractBlock(); OutStream = output; _buffer = new byte[16]; _encoding = encoding; _encoder = _encoding.GetEncoder(); _leaveOpen = leaveOpen; }
public BinaryWriter(Stream output, Encoding encoding, bool leaveOpen) { if (output == null) throw new ArgumentNullException("output"); if (encoding == null) throw new ArgumentNullException("encoding"); if (!output.CanWrite) throw new ArgumentException("StreamNotWritable"); OutStream = output; _buffer = new byte[16]; _encoding = encoding; _encoder = _encoding.GetEncoder(); _leaveOpen = leaveOpen; }
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]; }
public object GetRealObject(StreamingContext context) { Encoder encoder = _encoding.GetEncoder(); if (_fallback != null) { encoder.Fallback = _fallback; if (_charLeftOver != default(char)) { EncoderNLS encoderNls = encoder as EncoderNLS; if (encoderNls != null) { encoderNls.charLeftOver = _charLeftOver; } } } return(encoder); }
public EndianWriter(Stream output, Endianness endianess, Encoding encoding, bool leaveOpen) { if (output == null) throw new ArgumentNullException(nameof(output)); if (encoding == null) throw new ArgumentNullException(nameof(encoding)); if (!output.CanWrite) throw new ArgumentException("Can't write to the output stream", nameof(output)); Contract.EndContractBlock(); OutStream = output; buffer = new byte[16]; Encoding = encoding; encoder = Encoding.GetEncoder(); this.leaveOpen = leaveOpen; Endianess = endianess; resolvedEndianess = EndianessHelper.Resolve(endianess); }
public HttpResponseStreamWriter(Stream stream, Encoding encoding, int bufferSize) { if (stream == null) { throw new ArgumentNullException(nameof(stream)); } if (encoding == null) { throw new ArgumentNullException(nameof(encoding)); } _stream = stream; Encoding = encoding; _charBufferSize = bufferSize; _encoder = encoding.GetEncoder(); _byteBuffer = new ArraySegment<byte>(new byte[encoding.GetMaxByteCount(bufferSize)]); _charBuffer = new ArraySegment<char>(new char[bufferSize]); }
public BinaryWriter(Stream output, Encoding encoding) { if(output == null) { throw new ArgumentNullException("output"); } if(encoding == null) { throw new ArgumentNullException("encoding"); } if(!output.CanWrite) { throw new ArgumentException(S._("IO_NotSupp_Write")); } OutStream = output; this.encoding = encoding; encoder = encoding.GetEncoder(); smallBuffer = new byte [16]; smallCharBuffer = new char [1]; }
public BinaryWriter(Stream output, Encoding encoding, bool leaveOpen) { if (output == null) { throw new ArgumentNullException(nameof(output)); } if (encoding == null) { throw new ArgumentNullException(nameof(encoding)); } if (!output.CanWrite) { throw new ArgumentException(SR.Argument_StreamNotWritable); } OutStream = output; _buffer = new byte[16]; _encoding = encoding; _encoder = _encoding.GetEncoder(); _leaveOpen = leaveOpen; }
public StreamWriter(Stream stream, Encoding encoding, int bufferSize) { // Validate the parameters. if(stream == null) { throw new ArgumentNullException("stream"); } if(encoding == null) { throw new ArgumentNullException("encoding"); } if(!stream.CanWrite) { throw new ArgumentException(_("IO_NotSupp_Write")); } if(bufferSize <= 0) { throw new ArgumentOutOfRangeException ("bufferSize", _("ArgRange_BufferSize")); } if(bufferSize < MIN_BUFSIZ) { bufferSize = MIN_BUFSIZ; } // Initialize this object. this.stream = stream; this.encoding = encoding; this.encoder = encoding.GetEncoder(); this.bufferSize = bufferSize; this.inBuffer = new char [bufferSize]; this.inBufferLen = 0; this.outBuffer = new byte [encoding.GetMaxByteCount(bufferSize)]; this.autoFlush = false; this.streamOwner = false; // Write the encoding's preamble. WritePreamble(); }
/// <summary> /// Encodes a char sequence and writes it to a span /// </summary> /// <param name="encoding">The encoding to use</param> /// <param name="input">The ReadOnlySequence to read from</param> /// <param name="output">The span to write to</param> /// <returns>The number of bytes written to <paramref name="output"/></returns> public static int GetBytes(this Encoding encoding, ReadOnlySequence <char> input, Span <byte> output) { var RemainingChars = input.Length; var BytesUsed = 0; var Encoder = encoding.GetEncoder(); foreach (var MySegment in input) { var InBuffer = MySegment.Span; bool IsCompleted; do { // Encode the chars into our byte array Encoder.Convert( InBuffer, output, RemainingChars == InBuffer.Length, out var CharsRead, out var WrittenBytes, out IsCompleted ); BytesUsed += WrittenBytes; output = output.Slice(WrittenBytes); if (output.IsEmpty) { return(BytesUsed); } RemainingChars -= CharsRead; InBuffer = InBuffer.Slice(CharsRead); // Loop while there are more chars unread, or there are no chars left but there's still data to flush }while (!InBuffer.IsEmpty || (RemainingChars == 0 && !IsCompleted)); } return(BytesUsed); }
private void Init(Stream streamArg, Encoding encodingArg, int bufferSize, bool shouldLeaveOpen) { _stream = streamArg; _encoding = encodingArg; _encoder = _encoding.GetEncoder(); if (bufferSize < MinBufferSize) { bufferSize = MinBufferSize; } _charBuffer = new char[bufferSize]; _byteBuffer = new byte[_encoding.GetMaxByteCount(bufferSize)]; _charLen = bufferSize; // If we're appending to a Stream that already has data, don't write // the preamble. if (_stream.CanSeek && _stream.Position > 0) { _haveWrittenPreamble = true; } _closable = !shouldLeaveOpen; }
/// <summary> /// Reads the variable-length string that is terminated either by zero-char or end of stream. /// After reading, the position will be set after zero-char terminator. /// </summary> /// <param name="encoding">The encoding.</param> /// <returns>String</returns> public string ReadVariableString(Encoding encoding) { // This method reads a block of bytes to "bytes" from stream, then it decodes it into "chars" by using // the decoder of specified "encoding". This ensures correct decoding of single- or multi-byte strings. // Then "chars" are scanned for zero-char (terminator) or end of stream. Non-zero chars are added to the // output "text" list and then returned as string. // If zero-char is found then the stream postion is returned back to first byte after zero-char. // This seems to be better than using some complex zero-char locator method and then calling // Encoding.GetString() method. var text = new List<char>(_SIZE); var bytes = GetBytesBuffer(); var chars = GetCharsBuffer(); var decoder = encoding.GetDecoder(); var encoder = encoding.GetEncoder(); var isTerminated = false; decoder.Reset(); while (!isTerminated) { var byteCount = _stream.Read(bytes, 0, _SIZE); var charCount = encoding.GetChars(bytes, 0, byteCount, chars, 0); for (int i = 0; i < charCount; i++) { var c = chars[i]; if (c == Char.MinValue) { var offset = encoder.GetByteCount(chars, 0, i + 1, true) - byteCount; _stream.Seek(offset, SeekOrigin.Current); isTerminated = true; break; } text.Add(c); } if (!isTerminated && byteCount < _SIZE) { isTerminated = true; } } return new String(text.ToArray()); }
// Construct an instance of this class that serializes to a Stream interface. public XmlEncodedRawTextWriter( Stream stream, XmlWriterSettings settings ) : this( settings ) { Debug.Assert( stream != null && settings != null ); this.stream = stream; this.encoding = settings.Encoding; // the buffer is allocated will OVERFLOW in order to reduce checks when writing out constant size markup if (settings.Async) { bufLen = ASYNCBUFSIZE; } bufChars = new char[bufLen + OVERFLOW]; bufBytes = new byte[ bufChars.Length ]; bufBytesUsed = 0; // Init escaping of characters not fitting into the target encoding trackTextContent = true; inTextContent = false; lastMarkPos = 0; textContentMarks = new int[INIT_MARKS_COUNT]; textContentMarks[0] = 1; charEntityFallback = new CharEntityEncoderFallback(); this.encoding = (Encoding)settings.Encoding.Clone(); encoding.EncoderFallback = charEntityFallback; encoder = encoding.GetEncoder(); if ( !stream.CanSeek || stream.Position == 0 ) { byte[] bom = encoding.GetPreamble(); if ( bom.Length != 0 ) { this.stream.Write( bom, 0, bom.Length ); } } // Write the xml declaration if ( settings.AutoXmlDeclaration ) { WriteXmlDeclaration( standalone ); autoXmlDeclaration = true; } }
public HttpResponseStreamWriter( Stream stream, Encoding encoding, int bufferSize, LeasedArraySegment<byte> leasedByteBuffer, LeasedArraySegment<char> leasedCharBuffer) { if (stream == null) { throw new ArgumentNullException(nameof(stream)); } if (encoding == null) { throw new ArgumentNullException(nameof(encoding)); } if (leasedByteBuffer == null) { throw new ArgumentNullException(nameof(leasedByteBuffer)); } if (leasedCharBuffer == null) { throw new ArgumentNullException(nameof(leasedCharBuffer)); } var requiredLength = encoding.GetMaxByteCount(bufferSize); if (requiredLength > leasedByteBuffer.Data.Count) { var message = Resources.FormatHttpResponseStreamWriter_InvalidBufferSize( requiredLength, bufferSize, encoding.EncodingName, typeof(Encoding).FullName, nameof(Encoding.GetMaxByteCount)); throw new ArgumentException(message, nameof(leasedByteBuffer)); } _stream = stream; Encoding = encoding; _charBufferSize = bufferSize; _leasedByteBuffer = leasedByteBuffer; _leasedCharBuffer = leasedCharBuffer; _encoder = encoding.GetEncoder(); _byteBuffer = leasedByteBuffer.Data; _charBuffer = leasedCharBuffer.Data; }
public static int WriteString(byte[] buffer, int offset, int numBytes, bool pad, String str, Encoding enc) { Encoder encoder = enc.GetEncoder(); string paddedString = pad ? str + new string(' ', numBytes) : str; // Assumption: never less than one byte per character int charsUsed; int bytesUsed; bool completed; encoder.Convert(paddedString.ToCharArray(), 0, paddedString.Length, buffer, offset, numBytes, false, out charsUsed, out bytesUsed, out completed); if (charsUsed < str.Length) { throw new IOException("Failed to write entire string"); } return bytesUsed; }
private void Init(Stream streamArg, Encoding encodingArg, int bufferSize, bool shouldLeaveOpen) { this.stream = streamArg; this.encoding = encodingArg; this.encoder = encoding.GetEncoder(); if (bufferSize < MinBufferSize) bufferSize = MinBufferSize; charBuffer = new char[bufferSize]; byteBuffer = new byte[encoding.GetMaxByteCount(bufferSize)]; charLen = bufferSize; // If we're appending to a Stream that already has data, don't write // the preamble. if (stream.CanSeek && stream.Position > 0) haveWrittenPreamble = true; closable = !shouldLeaveOpen; #if MDA_SUPPORTED if (Mda.StreamWriterBufferedDataLost.Enabled) { String callstack = null; if (Mda.StreamWriterBufferedDataLost.CaptureAllocatedCallStack) callstack = Environment.GetStackTrace(null, false); mdaHelper = new MdaHelper(this, callstack); } #endif }
// Writing constructor public EncodingStreamWrapper(Stream stream, Encoding encoding, bool emitBOM) { _isReading = false; _encoding = encoding; _stream = stream; // Set the encoding code _encodingCode = GetSupportedEncoding(encoding); if (_encodingCode != SupportedEncoding.UTF8) { EnsureBuffers(); _dec = s_validatingUTF8.GetDecoder(); _enc = _encoding.GetEncoder(); // Emit BOM if (emitBOM) { byte[] bom = _encoding.GetPreamble(); if (bom.Length > 0) _stream.Write(bom, 0, bom.Length); } } }
private void InitForWriting(Stream outputStream, Encoding writeEncoding) { _encoding = writeEncoding; //this.stream = new BufferedStream(outputStream); _stream = outputStream; // Set the encoding code _encodingCode = GetSupportedEncoding(writeEncoding); if (_encodingCode != SupportedEncoding.UTF8) { EnsureBuffers(); _dec = s_validatingUTF8.GetDecoder(); _enc = _encoding.GetEncoder(); } }