public abstract GetMaxByteCount ( int charCount ) : int | ||
charCount | int | |
return | int |
public void TestBaseEncodings() { Assert.True(Enc.ASCII.IsSingleByte); Assert.True(latin1.IsSingleByte); Assert.False(Enc.BigEndianUnicode.IsSingleByte); Assert.False(Enc.Unicode.IsSingleByte); Assert.False(Enc.UTF7.IsSingleByte); Assert.False(Enc.UTF8.IsSingleByte); Assert.False(Enc.UTF32.IsSingleByte); // Why??? if (Platform.GetOS() == Platform.OS.Windows) { Assert.AreEqual(2, Enc.ASCII.GetMaxByteCount(1)); Assert.AreEqual(2, latin1.GetMaxByteCount(1)); Assert.AreEqual(4, Enc.BigEndianUnicode.GetMaxByteCount(1)); Assert.AreEqual(4, Enc.Unicode.GetMaxByteCount(1)); Assert.AreEqual(5, Enc.UTF7.GetMaxByteCount(1)); Assert.AreEqual(6, Enc.UTF8.GetMaxByteCount(1)); Assert.AreEqual(8, Enc.UTF32.GetMaxByteCount(1)); } else { Assert.AreEqual(1, Enc.ASCII.GetMaxByteCount(1)); Assert.AreEqual(1, latin1.GetMaxByteCount(1)); Assert.AreEqual(2, Enc.BigEndianUnicode.GetMaxByteCount(1)); Assert.AreEqual(2, Enc.Unicode.GetMaxByteCount(1)); Assert.AreEqual(5, Enc.UTF7.GetMaxByteCount(1)); Assert.AreEqual(4, Enc.UTF8.GetMaxByteCount(1)); Assert.AreEqual(4, Enc.UTF32.GetMaxByteCount(1)); } }
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 static string UrlEncode(string s, Encoding Enc) { if (s == null) { return null; } if (s == "") { return ""; } bool flag = false; int length = s.Length; for (int i = 0; i < length; i++) { char c = s[i]; if (((((c < '0') || ((c < 'A') && (c > '9'))) || ((c > 'Z') && (c < 'a'))) || (c > 'z')) && !NotEncoded(c)) { flag = true; break; } } if (!flag) { return s; } byte[] bytes = new byte[Enc.GetMaxByteCount(s.Length)]; int count = Enc.GetBytes(s, 0, s.Length, bytes, 0); return Encoding.ASCII.GetString(UrlEncodeToBytes(bytes, 0, count)); }
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"); }
public static string UrlEncode(string s, Encoding Enc) { if (s == null) return null; if (s == String.Empty) return String.Empty; bool needEncode = false; int len = s.Length; for (int i = 0; i < len; i++) { char c = s[i]; if ((c < '0') || (c < 'A' && c > '9') || (c > 'Z' && c < 'a') || (c > 'z')) { if (HttpEncoder.NotEncoded(c)) continue; needEncode = true; break; } } if (!needEncode) return s; // avoided GetByteCount call byte[] bytes = new byte[Enc.GetMaxByteCount(s.Length)]; int realLen = Enc.GetBytes(s, 0, s.Length, bytes, 0); byte[] t2 = UrlEncodeToBytes(bytes, 0, realLen); return Encoding.UTF8.GetString(t2, 0, t2.Length); }
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"); }
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]; }
private RubyEncoding(Encoding/*!*/ encoding, Encoding/*!*/ strictEncoding, int ordinal) { Assert.NotNull(encoding, strictEncoding); _ordinal = ordinal; _encoding = encoding; _strictEncoding = strictEncoding; _maxBytesPerChar = strictEncoding.GetMaxByteCount(1); _isAsciiIdentity = AsciiIdentity(encoding); }
protected InternalReaderBase(Encoding encoding, int bufferSize) { encoding_ = encoding; isSingleByte_ = encoding_.GetMaxByteCount(1) == 1; buffer_ = new byte[bufferSize]; handle_ = GCHandle.Alloc(buffer_, GCHandleType.Pinned); basePointer_ = handle_.AddrOfPinnedObject(); }
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, 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)]); }
private void TestEncoding(Encoding enc, int byteCount, int maxByteCount, byte[] bytes) { Assert.Equal(byteCount, enc.GetByteCount(s_myChars)); Assert.Equal(maxByteCount, enc.GetMaxByteCount(s_myChars.Length)); Assert.Equal(enc.GetBytes(s_myChars), bytes); Assert.Equal(enc.GetCharCount(bytes), s_myChars.Length); Assert.Equal(enc.GetChars(bytes), s_myChars); Assert.Equal(enc.GetString(bytes, 0, bytes.Length), s_myString); Assert.NotEqual(0, enc.GetHashCode()); }
/// <summary> /// Creates a new stream reader with a custom encoding. /// </summary> /// <param name="input">underlying input stream</param> /// <param name="encoding">custom encoding</param> public BinaryNBOReader(Stream input, Encoding encoding) : base(input, encoding) { int maxByteCount = encoding.GetMaxByteCount(1); if (maxByteCount < 0x10) { maxByteCount = 0x10; } buffer = new byte[maxByteCount]; }
/// <summary> /// Modifies a single column value in a modified record to be inserted or to /// update the current record. /// </summary> /// <param name="sesid">The session to use.</param> /// <param name="tableid">The cursor to update. An update should be prepared.</param> /// <param name="columnid">The columnid to set.</param> /// <param name="data">The data to set.</param> /// <param name="encoding">The encoding used to convert the string.</param> /// <param name="grbit">SetColumn options.</param> public static void SetColumn( JET_SESID sesid, JET_TABLEID tableid, JET_COLUMNID columnid, string data, Encoding encoding, SetColumnGrbit grbit) { CheckEncodingIsValid(encoding); if (null == data) { JetSetColumn(sesid, tableid, columnid, null, 0, grbit, null); } else if (0 == data.Length) { JetSetColumn(sesid, tableid, columnid, null, 0, grbit | SetColumnGrbit.ZeroLength, null); } else if (Encoding.Unicode == encoding) { // Optimization for setting Unicode strings. unsafe { fixed (char* buffer = data) { JetSetColumn( sesid, tableid, columnid, new IntPtr(buffer), checked(data.Length * sizeof(char)), grbit, null); } } } else if (encoding.GetMaxByteCount(data.Length) <= Caches.ColumnCache.BufferSize) { // The encoding output will fix in a cached buffer. Get one to avoid // more memory allocations. byte[] buffer = Caches.ColumnCache.Allocate(); unsafe { fixed (char* chars = data) fixed (byte* bytes = buffer) { int dataSize = encoding.GetBytes(chars, data.Length, bytes, buffer.Length); JetSetColumn(sesid, tableid, columnid, new IntPtr(bytes), dataSize, grbit, null); } } Caches.ColumnCache.Free(ref buffer); } else { byte[] bytes = encoding.GetBytes(data); JetSetColumn(sesid, tableid, columnid, bytes, bytes.Length, grbit, null); } }
internal void Initialize(Encoding encoding, int bufferSize) { internalEncoding = encoding; decode_pos = byte_pos = 0; int BufferSize = Math.Max(bufferSize, MinimumBufferSize); decode_buf = new char [BufferSize]; byte_buf = new byte [encoding.GetMaxByteCount (BufferSize)]; // Fixes bug http://bugzilla.ximian.com/show_bug.cgi?id=74513 if (internalStream.CanSeek && internalStream.Position > 0) preamble_done = true; }
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; } }
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); }
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))]; }
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; }
static public int GetMaxByteCount(IntPtr l) { try { System.Text.Encoding self = (System.Text.Encoding)checkSelf(l); System.Int32 a1; checkType(l, 2, out a1); var ret = self.GetMaxByteCount(a1); pushValue(l, true); pushValue(l, ret); return(2); } catch (Exception e) { return(error(l, e)); } }
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); }
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]); }
/// <inheritdoc /> public TextWriter CreateWriter(Stream stream, Encoding encoding) { if (stream == null) { throw new ArgumentNullException(nameof(stream)); } if (encoding == null) { throw new ArgumentNullException(nameof(encoding)); } LeasedArraySegment<byte> bytes = null; LeasedArraySegment<char> chars = null; try { chars = _charPool.Lease(DefaultBufferSize); // We need to compute the minimum size of the byte buffer based on the size of the char buffer, // so that we have enough room to encode the buffer in one shot. var minimumSize = encoding.GetMaxByteCount(DefaultBufferSize); bytes = _bytePool.Lease(minimumSize); return new HttpResponseStreamWriter(stream, encoding, DefaultBufferSize, bytes, chars); } catch { if (bytes != null) { bytes.Owner.Return(bytes); } if (chars != null) { chars.Owner.Return(chars); } throw; } }
public BinaryReader(Stream input, Encoding encoding, bool leaveOpen) { if (input == null) { throw new ArgumentNullException("input"); } if (encoding == null) { throw new ArgumentNullException("encoding"); } if (!input.CanRead) { throw new ArgumentException(SR.Argument_StreamNotReadable); } _stream = input; _decoder = encoding.GetDecoder(); _maxCharsSize = encoding.GetMaxCharCount(MaxCharBytesSize); int minBufferSize = encoding.GetMaxByteCount(1); // max bytes per one char if (minBufferSize < 16) { minBufferSize = 16; } _buffer = new byte[minBufferSize]; // _charBuffer and _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. _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 _isMemoryStream = (_stream.GetType() == typeof(MemoryStream)); _leaveOpen = leaveOpen; Debug.Assert(_decoder != null, "[BinaryReader.ctor]_decoder!=null"); }
/// <include file='doc\BinaryReader.uex' path='docs/doc[@for="BinaryReader.BinaryReader1"]/*' /> 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")); m_stream = input; m_decoder = encoding.GetDecoder(); int minBufferSize = encoding.GetMaxByteCount(1); // max bytes per one char if (minBufferSize < 16) minBufferSize = 16; m_buffer = new byte[minBufferSize]; m_charBuffer = null; m_charBytes = null; // Performance hack - 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; BCLDebug.Assert(m_decoder!=null, "[BinaryReader.ctor]m_decoder!=null"); }
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; }
private Task WriteEncodingChar(string s, int numChars, int offset, Encoding encoding, TdsParserStateObject stateObj, bool canAccumulate = true) { char[] charData; byte[] byteData; // if hitting 7.0 server, encoding will be null in metadata for columns or return values since // 7.0 has no support for multiple code pages in data - single code page support only if (encoding == null) encoding = _defaultEncoding; charData = s.ToCharArray(offset, numChars); // Optimization: if the entire string fits in the current buffer, then copy it directly int bytesLeft = stateObj._outBuff.Length - stateObj._outBytesUsed; if ((numChars <= bytesLeft) && (encoding.GetMaxByteCount(charData.Length) <= bytesLeft)) { int bytesWritten = encoding.GetBytes(charData, 0, charData.Length, stateObj._outBuff, stateObj._outBytesUsed); stateObj._outBytesUsed += bytesWritten; return null; } else { byteData = encoding.GetBytes(charData, 0, numChars); Debug.Assert(byteData != null, "no data from encoding"); return stateObj.WriteByteArray(byteData, byteData.Length, 0, canAccumulate); } }
internal static int getBytesPerChar( Encoding encoding ) { return encoding.GetMaxByteCount( 1 ); }
public static string UrlEncode(string s, Encoding encoding) { int len; if (s == null || (len = s.Length) == 0) return s; var needEncode = false; foreach (var c in s) { if ((c < '0') || (c < 'A' && c > '9') || (c > 'Z' && c < 'a') || (c > 'z')) { if (notEncoded (c)) continue; needEncode = true; break; } } if (!needEncode) return s; if (encoding == null) encoding = Encoding.UTF8; // Avoided GetByteCount call. var bytes = new byte[encoding.GetMaxByteCount (len)]; var realLen = encoding.GetBytes (s, 0, len, bytes, 0); return Encoding.ASCII.GetString (InternalUrlEncodeToBytes (bytes, 0, realLen)); }
public override int GetMaxByteCount(int charCount) { return(defaultEncoding.GetMaxByteCount(charCount)); }
private static string Encode(string str, MimeEncodingMethod encoding, Encoding charset, bool doFold, int foldingLimit, int foldingOffset, string foldingString) { if (str == null) throw new ArgumentNullException("str"); if (charset == null) throw new ArgumentNullException("charset"); if (doFold) { if (foldingLimit < 1) throw new ArgumentOutOfRangeException("foldingLimit", foldingLimit, "must be greater than 1"); if (foldingOffset < 0) throw new ArgumentOutOfRangeException("foldingOffset", foldingOffset, "must be greater than zero"); if (foldingLimit <= foldingOffset) throw new ArgumentOutOfRangeException("foldingOffset", foldingOffset, "must be less than foldingLimit"); if (foldingString == null) throw new ArgumentNullException("foldingString"); } ICryptoTransform transform; char encodingChar; switch (encoding) { case MimeEncodingMethod.Base64: transform = new ToBase64Transform(); encodingChar = 'b'; break; case MimeEncodingMethod.QuotedPrintable: transform = new ToQuotedPrintableTransform(); encodingChar = 'q'; break; default: throw new System.ComponentModel.InvalidEnumArgumentException("encoding", (int)encoding, typeof(MimeEncodingMethod)); } var preambleText = string.Format("=?{0}?{1}?", charset.BodyName, encodingChar); if (!doFold) return preambleText + transform.TransformStringTo(str, charset) + "?="; // folding var ret = new StringBuilder(); var preamble = Encoding.ASCII.GetBytes(preambleText); var firstLine = true; var inputCharBuffer = str.ToCharArray(); var inputCharOffset = 0; var outputBuffer = new byte[foldingLimit]; var ambleLength = preamble.Length + mimeEncodingPostamble.Length; var outputLimit = foldingLimit - (foldingOffset + ambleLength); if (outputLimit <= 0) throw new ArgumentOutOfRangeException("foldingLimit", foldingLimit, "too short"); // copy preamble to buffer Buffer.BlockCopy(preamble, 0, outputBuffer, 0, preamble.Length); for (;;) { var inputBlockSizeLimit = (outputLimit * transform.InputBlockSize) / transform.OutputBlockSize - 1; var transformCharCount = 0; var outputCount = preamble.Length; // decide char count to transform for (transformCharCount = inputBlockSizeLimit / charset.GetMaxByteCount(1);; transformCharCount++) { if (inputCharBuffer.Length <= inputCharOffset + transformCharCount) { transformCharCount = inputCharBuffer.Length - inputCharOffset; break; } if (inputBlockSizeLimit <= charset.GetByteCount(inputCharBuffer, inputCharOffset, transformCharCount + 1)) break; } // transform chars byte[] transformed = null; for (;;) { var t = transform.TransformBytes(charset.GetBytes(inputCharBuffer, inputCharOffset, transformCharCount)); if (transformed == null || t.Length <= outputLimit) { transformed = t; if (inputCharBuffer.Length <= inputCharOffset + transformCharCount + 1) break; transformCharCount++; continue; } else { transformCharCount--; break; } } if (outputBuffer.Length < ambleLength + transformed.Length) throw new ArgumentOutOfRangeException("foldingLimit", foldingLimit, string.Format("too short, at least {0} is required", ambleLength + transformed.Length)); // copy transformed chars to buffer Buffer.BlockCopy(transformed, 0, outputBuffer, outputCount, transformed.Length); outputCount += transformed.Length; // copy postanble to buffer Buffer.BlockCopy(mimeEncodingPostamble, 0, outputBuffer, outputCount, mimeEncodingPostamble.Length); outputCount += mimeEncodingPostamble.Length; ret.Append(Encoding.ASCII.GetString(outputBuffer, 0, outputCount)); inputCharOffset += transformCharCount; if (inputCharOffset < inputCharBuffer.Length) { ret.Append(foldingString); if (firstLine) { outputLimit = foldingLimit - ambleLength; firstLine = false; } } else { break; } } return ret.ToString(); }
public static string UrlEncode (string s, Encoding Enc) { if (string.IsNullOrEmpty (s)) return s; bool needEncode = false; int len = s.Length; for (int i = 0; i < len; i++) { char c = s [i]; if ((c < '0') || (c < 'A' && c > '9') || (c > 'Z' && c < 'a') || (c > 'z')) { if (NotEncoded (c)) continue; needEncode = true; break; } } if (!needEncode) return s; // avoided GetByteCount call byte [] bytes = new byte[Enc.GetMaxByteCount(s.Length)]; int realLen = Enc.GetBytes (s, 0, s.Length, bytes, 0); return Encoding.ASCII.GetString (UrlEncodeToBytes (bytes, 0, realLen)); }
private unsafe void WritePrefixLengthString(int characterStart, int characterCount, Encoding encoding, char* ptr) { char* curr = &ptr[characterStart]; int totalByteCount = encoding.GetByteCount(curr, characterCount); WriteVariant((uint)totalByteCount); // May not be optimal byte* bytes = stackalloc byte[256]; int maxChars = 256 / encoding.GetMaxByteCount(1); while (characterCount > 0) { int readChars = Math.Min(maxChars, characterCount); int byteCount = encoding.GetBytes(curr, readChars, bytes, 256); WriteMemory(bytes, byteCount * 8); curr += readChars; characterCount -= readChars; } }