internal P4FormRecordSet(string FormCommand, System.Text.Encoding encoding) { // clone this so we don't hold a reference to another object's encoding object // preventing it from Garbage collecting. _encoding = (System.Text.Encoding)encoding.Clone(); _FormCommand = FormCommand; }
private static System.Text.Encoding GetEncodingWithFallback(System.Text.Encoding encoding) { System.Text.Encoding encoding2 = (System.Text.Encoding)encoding.Clone(); encoding2.EncoderFallback = EncoderFallback.ReplacementFallback; encoding2.DecoderFallback = DecoderFallback.ReplacementFallback; return(encoding2); }
internal P4Form(string FormCommand, string specDef, Dictionary <string, string> S, System.Text.Encoding encoding) : base(S) { _specdef = specDef; // clone this so we don't hold a reference to another object's encoding object // preventing it from Garbage collecting. _encoding = (System.Text.Encoding)encoding.Clone(); _spec = new p4dn.Spec(specDef, encoding); _formCommand = FormCommand; }
/// <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); }
static public int Clone(IntPtr l) { try { System.Text.Encoding self = (System.Text.Encoding)checkSelf(l); var ret = self.Clone(); pushValue(l, true); pushValue(l, ret); return(2); } catch (Exception e) { return(error(l, e)); } }
public object GetRealObject(StreamingContext context) { if (this.realObject == null) { Encoding encoding = Encoding.GetEncoding(this.codePage); if (!this.isReadOnly) { encoding = (Encoding)encoding.Clone(); encoding.EncoderFallback = this.encoderFallback; encoding.DecoderFallback = this.decoderFallback; } this.realObject = encoding; } return(this.realObject); }
protected Encoding(System.Text.Encoding encoding, int minBytesPerChar) { if (encoding.IsReadOnly) { this.encoding = encoding.Clone() as System.Text.Encoding; } else { this.encoding = encoding; } this.encoding.DecoderFallback = new DecoderExceptionFallback(); this.encoding.EncoderFallback = new EncoderExceptionFallback(); this.minBytesPerChar = minBytesPerChar; }
/// <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; }
// 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; }
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); }
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; }
/// <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 if (BEncodingSettings.ParserMode == BEncodingParserMode.Loose) { if(!dictionary.ContainsKey(key)) dictionary.Add(key, value); // Discard duplicate key in loose mode } else { dictionary.Add(key, value); } } catch (ArgumentException e) { // Decoding the value has failed throw BEncodedFormatDecodeException.CreateTraced("Failed to decode dictionary value", e, reader.BaseStream); } // Preview next character in the stream peekChar = (char)reader.PeekChar(); } // Stream position is currently at the BEncoded.DictionaryEnd field, seek past the field in order to complete decoding reader.ReadChar(); // Return the decoded dictionary return dictionary; } else { // The stream does not contain a valid BEncodedDictionary at the current position throw BEncodedFormatDecodeException.CreateTraced("Expected dictionary start token", reader.BaseStream); } }
private static Encoding GetEncodingWithFallback(Encoding encoding) { Encoding copy = (Encoding)encoding.Clone(); copy.EncoderFallback = EncoderFallback.ReplacementFallback; copy.DecoderFallback = DecoderFallback.ReplacementFallback; return copy; }
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; }
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); } }
internal void SetEncoding(Encoding encoding) { _encoding = (Encoding)encoding.Clone(); }