本文整理汇总了C#中System.Text.Encoding.Clone方法的典型用法代码示例。如果您正苦于以下问题:C# Encoding.Clone方法的具体用法?C# Encoding.Clone怎么用?C# Encoding.Clone使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Text.Encoding
的用法示例。
在下文中一共展示了Encoding.Clone方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TextFileLineReader
/// <summary>
/// Initializes a new instance of the <see cref="TextFileLineReader"/> class.
/// </summary>
/// <param name="filePath">The path to text file.</param>
/// <param name="encoding">The encoding of text file.</param>
public TextFileLineReader(string filePath, Encoding encoding)
{
if(!File.Exists(filePath))
throw new FileNotFoundException("File (" + filePath + ") is not found.");
var safeEncoding = (Encoding)encoding.Clone();
safeEncoding.DecoderFallback = new DecoderReplacementFallback("");
_fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
_length = _fileStream.Length;
_binReader = new BinaryReader(_fileStream, safeEncoding);
}
示例2: GetEncodingWithFallback
/// <summary>
/// Gets the encoding with fallback to default encoding.
/// </summary>
/// <param name="encoding">Encoding to use for output</param>
/// <returns></returns>
private static Encoding GetEncodingWithFallback(Encoding encoding)
{
Encoding encoding2 = (Encoding)encoding.Clone();
encoding2.EncoderFallback = EncoderFallback.ReplacementFallback;
encoding2.DecoderFallback = DecoderFallback.ReplacementFallback;
return encoding2;
}
示例3: GetEncodingWithFallback
// This is defined in TWTL as well, we should look into refactoring/relayering at a later time
private static Encoding GetEncodingWithFallback(Encoding encoding)
{
// Clone it and set the "?" replacement fallback
Encoding fallbackEncoding = (Encoding)encoding.Clone();
fallbackEncoding.EncoderFallback = EncoderFallback.ReplacementFallback;
fallbackEncoding.DecoderFallback = DecoderFallback.ReplacementFallback;
return fallbackEncoding;
}
示例4: DoEncode
private static PythonTuple DoEncode(Encoding encoding, object input, string errors, bool includePreamble) {
// input should be some Unicode object
string res;
if (Converter.TryConvertToString(input, out res)) {
StringBuilder sb = new StringBuilder();
encoding = (Encoding)encoding.Clone();
#if !SILVERLIGHT // EncoderFallback
encoding.EncoderFallback = EncoderFallback.ExceptionFallback;
#endif
if (includePreamble) {
byte[] preamble = encoding.GetPreamble();
for (int i = 0; i < preamble.Length; i++) {
sb.Append((char)preamble[i]);
}
}
byte[] bytes = encoding.GetBytes(res);
for (int i = 0; i < bytes.Length; i++) {
sb.Append((char)bytes[i]);
}
return PythonTuple.MakeTuple(sb.ToString(), res.Length);
}
throw PythonOps.TypeErrorForBadInstance("cannot decode {0}", input);
}
示例5: DoDecode
private static PythonTuple DoDecode(Encoding encoding, object input, string errors, bool fAlwaysThrow) {
// input should be character buffer of some form...
string res;
if (!Converter.TryConvertToString(input, out res)) {
Bytes bs = input as Bytes;
if (bs == null) {
throw PythonOps.TypeErrorForBadInstance("argument 1 must be string, got {0}", input);
} else {
res = bs.ToString();
}
}
int preOffset = CheckPreamble(encoding, res);
byte[] bytes = new byte[res.Length - preOffset];
for (int i = 0; i < bytes.Length; i++) {
bytes[i] = (byte)res[i + preOffset];
}
#if !SILVERLIGHT // DecoderFallback
encoding = (Encoding)encoding.Clone();
ExceptionFallBack fallback = null;
if (fAlwaysThrow) {
encoding.DecoderFallback = DecoderFallback.ExceptionFallback;
} else {
fallback = new ExceptionFallBack(bytes);
encoding.DecoderFallback = fallback;
}
#endif
string decoded = encoding.GetString(bytes, 0, bytes.Length);
int badByteCount = 0;
#if !SILVERLIGHT // DecoderFallback
if (!fAlwaysThrow) {
byte[] badBytes = fallback.buffer.badBytes;
if (badBytes != null) {
badByteCount = badBytes.Length;
}
}
#endif
PythonTuple tuple = PythonTuple.MakeTuple(decoded, bytes.Length - badByteCount);
return tuple;
}
示例6: Decode
/// <summary>
/// Decodes the specified reader.
/// </summary>
/// <param name="reader">The reader.</param>
/// <param name="encoding">The encoding.</param>
/// <returns></returns>
internal static BEncodedDictionary Decode(BinaryReader reader, Encoding encoding)
{
if (encoding == null) throw new ArgumentNullException("encoding");
if (!reader.BaseStream.CanSeek) throw new NotSupportedException("Cannot decode non-seekable streams");
// Decoded key
BEncodedString key;
// Buffer for key decoding
BEncodedString keyBuffer;
// Decoded Value
IBEncodedValue value;
// BEncodedDictionary containing KeyValueParis
BEncodedDictionary dictionary = new BEncodedDictionary(encoding);
// Preview next character in the stream
int peek = reader.PeekChar();
if (peek == -1)
throw BEncodedFormatDecodeException.CreateTraced("Unexpected end of dictionary", reader.BaseStream);
char peekChar = (char)peek;
// Ensure the current stream position represents a BEncodedDictionary
if (peekChar == BEncodingSettings.DictionaryStart)
{
// Seek past the BEncoded.DictionaryStart field
reader.ReadChar();
// Preview the next character in the stream, this should represent a constituent IBEncodedValue
peekChar = (char)reader.PeekChar();
// Set the key to null before the first key value pair is decoded, necessary for lexographical comparison
key = null;
// Decode all constituent IBEncodedValues until the BEncoded.DictionaryEnd field is reached
while (peekChar != BEncodingSettings.DictionaryEnd)
{
try
{
// Try to parse a BEncodedString key
keyBuffer = BEncodedString.Decode(reader, encoding);
}
catch (BEncodedFormatDecodeException e)
{
throw BEncodedFormatDecodeException.CreateTraced("Failed to read dictionary key", e, reader.BaseStream);
}
// Check whether the key has appeared in lexographical order by comparing keyBuffer with the previous key, if present
if (key == null || keyBuffer.Value.CompareTo(key.Value) >= 0)
{
// keyBuffer has appeared in valid lexical order
key = keyBuffer;
}
else if (BEncodingSettings.ParserMode == BEncodingParserMode.Loose)
{
// The key, keyBuffer is not in lexographical order, ie. belongs somewhere before the former key
key = keyBuffer;
}
else
{
throw BEncodedFormatDecodeException.CreateTraced("Dictionary keys must be ordered lexographically in strict mode", reader.BaseStream);
}
// Preview next character in the stream, this should represent an IBEncodedValue following the key
peekChar = (char)reader.PeekChar();
// Attempt to decode the value
try
{
switch (peekChar)
{
case BEncodingSettings.DictionaryStart:
value = BEncodedDictionary.Decode(reader, (Encoding)encoding.Clone());
break;
case BEncodingSettings.ListStart:
value = BEncodedList.Decode(reader, (Encoding)encoding.Clone());
break;
case BEncodingSettings.IntegerStart:
value = BEncodedInteger.Decode(reader);
break;
default:
// Capture BEncodedStrigns beginning with numeric characters
if (Array.IndexOf(BEncodingSettings.NumericMask, peekChar) != -1)
{
value = BEncodedString.Decode(reader, (Encoding)encoding.Clone());
}
else
{
// Unrecognised token encountered
throw BEncodedFormatDecodeException.CreateTraced("Unrecognised dictionary element encountered", reader.BaseStream);
}
break;
}
// Add key and value to the dictionary
//.........这里部分代码省略.........
示例7: GetEncodingWithFallback
private static Encoding GetEncodingWithFallback(Encoding encoding)
{
Encoding copy = (Encoding)encoding.Clone();
copy.EncoderFallback = EncoderFallback.ReplacementFallback;
copy.DecoderFallback = DecoderFallback.ReplacementFallback;
return copy;
}
示例8: DoDecode
private static PythonTuple DoDecode(Encoding encoding, object input, string errors, bool fAlwaysThrow) {
// input should be character buffer of some form...
string res;
if (!Converter.TryConvertToString(input, out res)) {
Bytes tempBytes = input as Bytes;
if (tempBytes == null) {
throw PythonOps.TypeErrorForBadInstance("argument 1 must be string, got {0}", input);
} else {
res = tempBytes.ToString();
}
}
int preOffset = CheckPreamble(encoding, res);
byte[] bytes = new byte[res.Length - preOffset];
for (int i = 0; i < bytes.Length; i++) {
bytes[i] = (byte)res[i + preOffset];
}
#if FEATURE_ENCODING // DecoderFallback
encoding = (Encoding)encoding.Clone();
ExceptionFallBack fallback = null;
if (fAlwaysThrow) {
encoding.DecoderFallback = DecoderFallback.ExceptionFallback;
} else {
fallback = (encoding is UTF8Encoding && DotNet) ?
// This is a workaround for a bug, see ExceptionFallbackBufferUtf8DotNet
// for more details.
new ExceptionFallBackUtf8DotNet(bytes):
new ExceptionFallBack(bytes);
encoding.DecoderFallback = fallback;
}
#endif
string decoded = encoding.GetString(bytes, 0, bytes.Length);
int badByteCount = 0;
#if FEATURE_ENCODING // DecoderFallback
if (!fAlwaysThrow) {
byte[] badBytes = fallback.buffer.badBytes;
if (badBytes != null) {
badByteCount = badBytes.Length;
}
}
#endif
PythonTuple tuple = PythonTuple.MakeTuple(decoded, bytes.Length - badByteCount);
return tuple;
}
示例9: Decode
internal static BEncodedList Decode(BinaryReader reader, Encoding encoding)
{
if (encoding == null) throw new ArgumentNullException("encoding");
if (!reader.BaseStream.CanSeek) throw new NotSupportedException("Cannot decode non-seekable streams");
IBEncodedValue value;
BEncodedList list = new BEncodedList(encoding);
char peekChar = (char)reader.PeekChar();
if (peekChar == BEncodingSettings.ListStart)
{
reader.ReadChar();
peekChar = (char)reader.PeekChar();
while (peekChar != BEncodingSettings.ListEnd)
{
try
{
switch (peekChar)
{
case BEncodingSettings.DictionaryStart:
value = BEncodedDictionary.Decode(reader, (Encoding)encoding.Clone());
break;
case BEncodingSettings.ListStart:
value = BEncodedList.Decode(reader, (Encoding)encoding.Clone());
break;
case BEncodingSettings.IntegerStart:
value = BEncodedInteger.Decode(reader);
break;
default:
if (Array.IndexOf(BEncodingSettings.NumericMask, peekChar) != -1)
{
value = BEncodedString.Decode(reader, (Encoding)encoding.Clone());
}
else
{
throw BEncodedFormatDecodeException.CreateTraced("Expected integer value", reader.BaseStream);
}
break;
}
}
catch (Exception e)
{
throw e;
}
list.Add(value);
peekChar = (char)reader.PeekChar();
}
reader.ReadChar();
return list;
}
else
{
throw BEncodedFormatDecodeException.CreateTraced("Expected list start token", reader.BaseStream);
}
}
示例10: SetEncoding
internal void SetEncoding(Encoding encoding)
{
_encoding = (Encoding)encoding.Clone();
}