| 1 | // This is partially copied source from referencesource/mscorlib/system/text/encoding.cs, modifying a bit. |
| 2 | |
| 3 | // ==++== |
| 4 | // |
| 5 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 6 | // |
| 7 | // ==--== |
| 8 | |
| 9 | |
| 10 | using System; |
| 11 | using System.Diagnostics.CodeAnalysis; |
| 12 | using System.Diagnostics.Contracts; |
| 13 | using System.Runtime.InteropServices; |
| 14 | using System.Runtime.Serialization; |
| 15 | using System.Text; |
| 16 | |
| 17 | namespace I18N.Common |
| 18 | { |
| 19 | [Serializable] |
| 20 | public class ReferenceSourceDefaultEncoder : Encoder, IObjectReference |
| 21 | { |
| 22 | private Encoding m_encoding; |
| 23 | [NonSerialized] private bool m_hasInitializedEncoding; |
| 24 | |
| 25 | [NonSerialized] internal char charLeftOver; |
| 26 | |
| 27 | public ReferenceSourceDefaultEncoder(Encoding encoding) |
| 28 | { |
| 29 | m_encoding = encoding; |
| 30 | m_hasInitializedEncoding = true; |
| 31 | } |
| 32 | |
| 33 | // Constructor called by serialization, have to handle deserializing from Everett |
| 34 | internal ReferenceSourceDefaultEncoder(SerializationInfo info, StreamingContext context) |
| 35 | { |
| 36 | if (info==null) throw new ArgumentNullException("info"); |
| 37 | Contract.EndContractBlock(); |
| 38 | |
| 39 | // All we have is our encoding |
| 40 | this.m_encoding = (Encoding)info.GetValue("encoding", typeof(Encoding)); |
| 41 | |
| 42 | try |
| 43 | { |
| 44 | //this.m_fallback = (EncoderFallback) info.GetValue("m_fallback", typeof(EncoderFallback)); |
| 45 | this.charLeftOver = (Char) info.GetValue("charLeftOver", typeof(Char)); |
| 46 | } |
| 47 | catch (SerializationException) |
| 48 | { |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | // Just get it from GetEncoding |
| 53 | [System.Security.SecurityCritical] // auto-generated |
| 54 | public Object GetRealObject(StreamingContext context) |
| 55 | { |
| 56 | // upon deserialization since the DefaultEncoder implement IObjectReference the |
| 57 | // serialization code tries to do the fixup. The fixup returns another |
| 58 | // IObjectReference (the DefaultEncoder) class and hence so on and on. |
| 59 | // Finally the deserialization logics fails after following maximum references |
| 60 | // unless we short circuit with the following |
| 61 | if (m_hasInitializedEncoding) |
| 62 | { |
| 63 | return this; |
| 64 | } |
| 65 | |
| 66 | Encoder encoder = m_encoding.GetEncoder(); |
| 67 | /* |
| 68 | if (m_fallback != null) |
| 69 | encoder.m_fallback = m_fallback; |
| 70 | if (charLeftOver != (char) 0) |
| 71 | { |
| 72 | EncoderNLS encoderNls = encoder as EncoderNLS; |
| 73 | if (encoderNls != null) |
| 74 | encoderNls.charLeftOver = charLeftOver; |
| 75 | } |
| 76 | */ |
| 77 | return encoder; |
| 78 | } |
| 79 | |
| 80 | #if FEATURE_SERIALIZATION |
| 81 | // ISerializable implementation, get data for this object |
| 82 | [System.Security.SecurityCritical] // auto-generated_required |
| 83 | void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context) |
| 84 | { |
| 85 | // Any info? |
| 86 | if (info==null) throw new ArgumentNullException("info"); |
| 87 | Contract.EndContractBlock(); |
| 88 | |
| 89 | // All we have is our encoding |
| 90 | info.AddValue("encoding", this.m_encoding); |
| 91 | } |
| 92 | #endif |
| 93 | |
| 94 | // Returns the number of bytes the next call to GetBytes will |
| 95 | // produce if presented with the given range of characters and the given |
| 96 | // value of the flush parameter. The returned value takes into |
| 97 | // account the state in which the encoder was left following the last call |
| 98 | // to GetBytes. The state of the encoder is not affected by a call |
| 99 | // to this method. |
| 100 | // |
| 101 | |
| 102 | public override int GetByteCount(char[] chars, int index, int count, bool flush) |
| 103 | { |
| 104 | return m_encoding.GetByteCount(chars, index, count); |
| 105 | } |
| 106 | |
| 107 | [System.Security.SecurityCritical] // auto-generated |
| 108 | [SuppressMessage("Microsoft.Contracts", "CC1055")] // Skip extra error checking to avoid *potential* AppCompat problems. |
| 109 | public unsafe override int GetByteCount(char* chars, int count, bool flush) |
| 110 | { |
| 111 | return m_encoding.GetByteCount(chars, count); |
| 112 | } |
| 113 | |
| 114 | // Encodes a range of characters in a character array into a range of bytes |
| 115 | // in a byte array. The method encodes charCount characters from |
| 116 | // chars starting at index charIndex, storing the resulting |
| 117 | // bytes in bytes starting at index byteIndex. The encoding |
| 118 | // takes into account the state in which the encoder was left following the |
| 119 | // last call to this method. The flush parameter indicates whether |
| 120 | // the encoder should flush any shift-states and partial characters at the |
| 121 | // end of the conversion. To ensure correct termination of a sequence of |
| 122 | // blocks of encoded bytes, the last call to GetBytes should specify |
| 123 | // a value of true for the flush parameter. |
| 124 | // |
| 125 | // An exception occurs if the byte array is not large enough to hold the |
| 126 | // complete encoding of the characters. The GetByteCount method can |
| 127 | // be used to determine the exact number of bytes that will be produced for |
| 128 | // a given range of characters. Alternatively, the GetMaxByteCount |
| 129 | // method of the Encoding that produced this encoder can be used to |
| 130 | // determine the maximum number of bytes that will be produced for a given |
| 131 | // number of characters, regardless of the actual character values. |
| 132 | // |
| 133 | |
| 134 | public override int GetBytes(char[] chars, int charIndex, int charCount, |
| 135 | byte[] bytes, int byteIndex, bool flush) |
| 136 | { |
| 137 | return m_encoding.GetBytes(chars, charIndex, charCount, bytes, byteIndex); |
| 138 | } |
| 139 | |
| 140 | [System.Security.SecurityCritical] // auto-generated |
| 141 | [SuppressMessage("Microsoft.Contracts", "CC1055")] // Skip extra error checking to avoid *potential* AppCompat problems. |
| 142 | public unsafe override int GetBytes(char* chars, int charCount, |
| 143 | byte* bytes, int byteCount, bool flush) |
| 144 | { |
| 145 | return m_encoding.GetBytes(chars, charCount, bytes, byteCount); |
| 146 | } |
| 147 | } |
| 148 | } |