本文整理汇总了C#中System.Text.Encoding.GetMaxCharCount方法的典型用法代码示例。如果您正苦于以下问题:C# Encoding.GetMaxCharCount方法的具体用法?C# Encoding.GetMaxCharCount怎么用?C# Encoding.GetMaxCharCount使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Text.Encoding
的用法示例。
在下文中一共展示了Encoding.GetMaxCharCount方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Init
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;
}
示例2: BinaryReader
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);
}
示例3: EndianReader
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");
}
示例4: StdInStreamReader
private readonly StringBuilder _readLineSB; // SB that holds readLine output
internal StdInStreamReader(Stream stream, Encoding encoding, int bufferSize) : base(stream: stream, encoding: encoding, detectEncodingFromByteOrderMarks: false, bufferSize: bufferSize, leaveOpen: true)
{
_unprocessedBufferToBeRead = new char[encoding.GetMaxCharCount(BytesToBeRead)];
_startIndex = 0;
_endIndex = 0;
_readLineSB = new StringBuilder();
}
示例5: HttpRequestStreamReader
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];
}
示例6: BinaryReader
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");
}
示例7: StdInReader
private int _endIndex; // Index after last unprocessed index in the buffer;
internal StdInReader(Encoding encoding, int bufferSize)
{
_encoding = encoding;
_unprocessedBufferToBeRead = new char[encoding.GetMaxCharCount(BytesToBeRead)];
_startIndex = 0;
_endIndex = 0;
_readLineSB = new StringBuilder();
}
示例8: CancellableStreamReader
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;
}
示例9: MyBinaryReader
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;
}
}
示例10: ReusableTextReader
/// <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)];
}
示例11: Decode
internal static SourceText Decode(Stream stream, Encoding encoding, SourceHashAlgorithm checksumAlgorithm, bool throwIfBinaryDetected)
{
stream.Seek(0, SeekOrigin.Begin);
int length = (int)stream.Length;
if (length == 0)
{
return SourceText.From(string.Empty, encoding, checksumAlgorithm);
}
var maxCharRemainingGuess = encoding.GetMaxCharCount(length);
using (var reader = new StreamReader(stream, encoding, detectEncodingFromByteOrderMarks: true, bufferSize: Math.Min(length, 4096), leaveOpen: true))
{
ArrayBuilder<char[]> chunks = ArrayBuilder<char[]>.GetInstance(1 + maxCharRemainingGuess / ChunkSize);
while (!reader.EndOfStream)
{
var nextChunkSize = ChunkSize;
if (maxCharRemainingGuess < ChunkSize)
{
// maxCharRemainingGuess typically overestimates a little
// so we will first fill a slightly smaller (maxCharRemainingGuess - 64) chunk
// and then use 64 char tail, which is likley to be resized.
nextChunkSize = Math.Max(maxCharRemainingGuess - 64, 64);
}
char[] chunk = new char[nextChunkSize];
int charsRead = reader.ReadBlock(chunk, 0, chunk.Length);
if (charsRead == 0)
{
break;
}
maxCharRemainingGuess -= charsRead;
if (charsRead < chunk.Length)
{
Array.Resize(ref chunk, charsRead);
}
// Check for binary files
if (throwIfBinaryDetected && IsBinary(chunk))
{
throw new InvalidDataException();
}
chunks.Add(chunk);
}
var checksum = CalculateChecksum(stream, checksumAlgorithm);
return new LargeText(chunks.ToImmutableAndFree(), reader.CurrentEncoding, checksum, checksumAlgorithm);
}
}
示例12: Init
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;
}
示例13: MultipartFormDataReader
public MultipartFormDataReader(string contentType, Encoding contentEncoding, Stream inputStream, int bufferSize)
{
this.contentType = contentType;
this.contentEncoding = contentEncoding;
this.inputStream = inputStream;
mode = ParsingMode.Headers;
ParseContentType();
mem = new MemoryStream(bufferSize);
text = new char[
contentEncoding.GetMaxCharCount(mem.Capacity)
];
}
示例14: TcpBinaryReader
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);
}
示例15: AsyncStreamReader
// 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>();
}