public void WriteFromDict <TKey, TValue> (IDictionary <TKey, TValue> val) { long origPos = stream.Position; // Pre-write array length field, we overwrite it at the end with the correct value Write((uint)0); WritePad(8); long startPos = stream.Position; TypeWriter <TKey> keyWriter = TypeImplementer.GetTypeWriter <TKey> (); TypeWriter <TValue> valueWriter = TypeImplementer.GetTypeWriter <TValue> (); foreach (KeyValuePair <TKey, TValue> entry in val) { WritePad(8); keyWriter(this, entry.Key); valueWriter(this, entry.Value); } long endPos = stream.Position; uint ln = (uint)(endPos - startPos); stream.Position = origPos; if (ln > ProtocolInformation.MaxArrayLength) { throw new Exception("Dict length " + ln + " exceeds maximum allowed " + ProtocolInformation.MaxArrayLength + " bytes"); } Write(ln); stream.Position = endPos; }
//this requires a seekable stream for now public void WriteArray <T> (T[] val) { Type elemType = typeof(T); if (elemType == typeof(byte)) { if (val.Length > ProtocolInformation.MaxArrayLength) { ThrowArrayLengthException((uint)val.Length); } Write((uint)val.Length); stream.Write((byte[])(object)val, 0, val.Length); return; } if (elemType.IsEnum) { elemType = Enum.GetUnderlyingType(elemType); } Signature sigElem = Signature.GetSig(elemType); int fixedSize = 0; if (endianness == Connection.NativeEndianness && elemType.IsValueType && !sigElem.IsStruct && elemType != typeof(bool) && sigElem.GetFixedSize(ref fixedSize)) { int byteLength = fixedSize * val.Length; if (byteLength > ProtocolInformation.MaxArrayLength) { ThrowArrayLengthException((uint)byteLength); } Write((uint)byteLength); WritePad(sigElem.Alignment); byte[] data = new byte[byteLength]; Buffer.BlockCopy(val, 0, data, 0, data.Length); stream.Write(data, 0, data.Length); return; } long origPos = stream.Position; Write((uint)0); //advance to the alignment of the element WritePad(sigElem.Alignment); long startPos = stream.Position; TypeWriter <T> tWriter = TypeImplementer.GetTypeWriter <T> (); foreach (T elem in val) { tWriter(this, elem); } long endPos = stream.Position; uint ln = (uint)(endPos - startPos); stream.Position = origPos; if (ln > ProtocolInformation.MaxArrayLength) { ThrowArrayLengthException(ln); } Write(ln); stream.Position = endPos; }