internal static ImmutableArray <byte> CreateBlob(ArraySegment <byte> bytes) { Debug.Assert(bytes.Array != null); if (bytes.Count < CompressionThreshold) { using (Cci.PooledBlobBuilder builder = Cci.PooledBlobBuilder.GetInstance()) { builder.WriteInt32(0); builder.WriteBytes(bytes.Array, bytes.Offset, bytes.Count); return(builder.ToImmutableArray()); } } else { using (BlobBuildingStream builder = BlobBuildingStream.GetInstance()) { builder.WriteInt32(bytes.Count); using (CountingDeflateStream deflater = new CountingDeflateStream(builder, CompressionLevel.Optimal, leaveOpen: true)) { deflater.Write(bytes.Array, bytes.Offset, bytes.Count); } return(builder.ToImmutableArray()); } } }
/// <summary> /// Creates the blob to be saved to the PDB. /// </summary> internal static ImmutableArray <byte> CreateBlob(Stream stream) { Debug.Assert(stream != null); Debug.Assert(stream.CanRead); Debug.Assert(stream.CanSeek); long longLength = stream.Length; Debug.Assert(longLength >= 0); if (longLength > int.MaxValue) { throw new IOException(CodeAnalysisResources.StreamIsTooLong); } stream.Seek(0, SeekOrigin.Begin); int length = (int)longLength; if (length < CompressionThreshold) { using (Cci.PooledBlobBuilder builder = Cci.PooledBlobBuilder.GetInstance()) { builder.WriteInt32(0); int bytesWritten = builder.TryWriteBytes(stream, length); if (length != bytesWritten) { throw new EndOfStreamException(); } return(builder.ToImmutableArray()); } } else { using (BlobBuildingStream builder = BlobBuildingStream.GetInstance()) { builder.WriteInt32(length); using (CountingDeflateStream deflater = new CountingDeflateStream(builder, CompressionLevel.Optimal, leaveOpen: true)) { stream.CopyTo(deflater); if (length != deflater.BytesWritten) { throw new EndOfStreamException(); } } return(builder.ToImmutableArray()); } } }
private static ImmutableArray <byte> CreateBlob(SourceText text) { Debug.Assert(text != null); Debug.Assert(text.CanBeEmbedded); Debug.Assert(text.Encoding != null); Debug.Assert(text.PrecomputedEmbeddedTextBlob.IsDefault); int maxByteCount; try { maxByteCount = text.Encoding.GetMaxByteCount(text.Length); } catch (ArgumentOutOfRangeException) { // Encoding does not provide a way to predict that max byte count would not // fit in Int32 and we must therefore catch ArgumentOutOfRange to handle that // case. maxByteCount = int.MaxValue; } using (BlobBuildingStream builder = BlobBuildingStream.GetInstance()) { if (maxByteCount < CompressionThreshold) { builder.WriteInt32(0); using (StreamWriter writer = new StreamWriter(builder, text.Encoding, bufferSize: Math.Max(1, text.Length), leaveOpen: true)) { text.Write(writer); } } else { Blob reserved = builder.ReserveBytes(4); using (CountingDeflateStream deflater = new CountingDeflateStream(builder, CompressionLevel.Optimal, leaveOpen: true)) { using (StreamWriter writer = new StreamWriter(deflater, text.Encoding, bufferSize: 1024, leaveOpen: true)) { text.Write(writer); } new BlobWriter(reserved).WriteInt32(deflater.BytesWritten); } } return(builder.ToImmutableArray()); } }
internal static ImmutableArray <byte> CreateBlob(ArraySegment <byte> bytes) { if (bytes.Count < 200) { BlobBuilder pooledBlobBuilder = new BlobBuilder(); pooledBlobBuilder.WriteInt32(0); pooledBlobBuilder.WriteBytes(bytes.Array, bytes.Offset, bytes.Count); return(pooledBlobBuilder.ToImmutableArray()); } using (BlobBuildingStream builder = BlobBuildingStream.GetInstance()) { builder.WriteInt32(bytes.Count); using (CountingDeflateStream deflater = new CountingDeflateStream(builder, CompressionLevel.Optimal, leaveOpen: true)) { deflater.Write(bytes.Array, bytes.Offset, bytes.Count); } return(builder.ToImmutableArray()); } }
/// <summary> /// Creates the blob to be saved to the PDB. /// </summary> internal static ImmutableArray <byte> CreateBlob(Stream stream) { long length2 = stream.Length; if (length2 > int.MaxValue) { throw new IOException("Stream is too long"); } stream.Seek(0L, SeekOrigin.Begin); int length = (int)length2; if (length < 200) { BlobBuilder pooledBlobBuilder = new BlobBuilder(); pooledBlobBuilder.WriteInt32(0); int bytesWritten = pooledBlobBuilder.TryWriteBytes(stream, length); if (length != bytesWritten) { throw new EndOfStreamException(); } return(pooledBlobBuilder.ToImmutableArray()); } using (BlobBuildingStream builder = BlobBuildingStream.GetInstance()) { builder.WriteInt32(length); using (CountingDeflateStream deflater = new CountingDeflateStream(builder, CompressionLevel.Optimal, leaveOpen: true)) { stream.CopyTo(deflater); if (length != deflater.BytesWritten) { throw new EndOfStreamException(); } } return(builder.ToImmutableArray()); } }