public void SerializeChecksumWithChildren(ChecksumWithChildren checksums, ObjectWriter writer, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); var kind = checksums.GetWellKnownSynchronizationKind(); writer.WriteString(kind); checksums.Checksum.WriteTo(writer); writer.WriteInt32(checksums.Children.Count); foreach (var child in checksums.Children) { var checksum = child as Checksum; if (checksum != null) { writer.WriteByte(ChecksumKind); checksum.WriteTo(writer); continue; } var checksumCollection = child as ChecksumCollection; if (checksumCollection != null) { writer.WriteByte(ChecksumWithChildrenKind); SerializeChecksumWithChildren(checksumCollection, writer, cancellationToken); continue; } throw ExceptionUtilities.UnexpectedValue(child); } }
public async Task SerializeChecksumObjectWithChildrenAsync(ChecksumObjectWithChildren checksumObject, ObjectWriter writer, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); writer.WriteString(checksumObject.Kind); checksumObject.Checksum.WriteTo(writer); writer.WriteInt32(checksumObject.Children.Length); foreach (var child in checksumObject.Children) { var checksum = child as Checksum; if (checksum != null) { writer.WriteByte(ChecksumKind); checksum.WriteTo(writer); continue; } var checksumCollection = child as ChecksumCollection; if (checksumCollection != null) { writer.WriteByte(ChecksumCollectionKind); await SerializeChecksumObjectWithChildrenAsync(checksumCollection, writer, cancellationToken).ConfigureAwait(false); continue; } throw ExceptionUtilities.UnexpectedValue(child); } }
private static void WriteBitArray(ObjectWriter writer, BitArray bitArray) { // Our serialization format doesn't round-trip bit arrays of non-byte lengths Contract.ThrowIfTrue(bitArray.Length % 8 != 0); writer.WriteInt32(bitArray.Length / 8); // This will hold the byte that we will write out after we process every 8 bits. This is // LSB, so we push bits into it from the MSB. byte b = 0; for (var i = 0; i < bitArray.Length; i++) { if (bitArray[i]) { b = (byte)(0x80 | b >> 1); } else { b >>= 1; } if ((i + 1) % 8 == 0) { // End of a byte, write out the byte writer.WriteByte(b); } } }
public override void WriteTo(Encoding encoding, ObjectWriter writer, CancellationToken cancellationToken) { if (encoding == null) { base.WriteTo(encoding, writer, cancellationToken); return; } cancellationToken.ThrowIfCancellationRequested(); writer.WriteByte(EncodingSerialization); byte[] value; if (!s_encodingCache.TryGetValue(encoding, out value)) { // we don't have cache, cache it var formatter = new BinaryFormatter(); using (var stream = SerializableBytes.CreateWritableStream()) { // unfortunately, this is only way to properly clone encoding formatter.Serialize(stream, encoding); value = stream.ToArray(); // add if not already exist. otherwise, noop s_encodingCache.TryAdd(encoding, value); } } // write data out writer.WriteValue(value); }
internal void WriteTo(ObjectWriter writer) { writer.WriteString(Name); writer.WriteString(ContainerDisplayName); writer.WriteString(FullyQualifiedContainerName); writer.WriteByte((byte)Kind); writer.WriteInt32(Span.Start); writer.WriteInt32(Span.Length); writer.WriteUInt16(ParameterCount); writer.WriteUInt16(TypeParameterCount); }
public static void WriteTo(this BitArray bitArray, ObjectWriter writer) { // TODO : think about a way to use pool for byte array. // BitArray will internally allocate another int array. probably need to drop BitArray usage. var bytes = new byte[(bitArray.Length + 7) / 8]; bitArray.CopyTo(bytes, 0); writer.WriteString(SerializationFormat); writer.WriteInt32(bytes.Length); for (var i = 0; i < bytes.Length; i++) { writer.WriteByte(bytes[i]); } }