Ejemplo n.º 1
0
        internal static ImmutableArray <byte> CreateBlob(ArraySegment <byte> bytes)
        {
            RoslynDebug.Assert(bytes.Array != null);

            if (bytes.Count < CompressionThreshold)
            {
                using (var builder = Cci.PooledBlobBuilder.GetInstance())
                {
                    builder.WriteInt32(0);
                    builder.WriteBytes(bytes.Array, bytes.Offset, bytes.Count);
                    return(builder.ToImmutableArray());
                }
            }
            else
            {
                using (var builder = BlobBuildingStream.GetInstance())
                {
                    builder.WriteInt32(bytes.Count);

                    using (
                        var deflater = new CountingDeflateStream(
                            builder,
                            CompressionLevel.Optimal,
                            leaveOpen: true
                            )
                        )
                    {
                        deflater.Write(bytes.Array, bytes.Offset, bytes.Count);
                    }

                    return(builder.ToImmutableArray());
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates the blob to be saved to the PDB.
        /// </summary>
        internal static ImmutableArray <byte> CreateBlob(Stream stream)
        {
            RoslynDebug.Assert(stream != null);
            RoslynDebug.Assert(stream.CanRead);
            RoslynDebug.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 (var builder = Cci.PooledBlobBuilder.GetInstance())
                {
                    builder.WriteInt32(0);
                    int bytesWritten = builder.TryWriteBytes(stream, length);

                    if (length != bytesWritten)
                    {
                        throw new EndOfStreamException();
                    }

                    return(builder.ToImmutableArray());
                }
            }
            else
            {
                using (var builder = BlobBuildingStream.GetInstance())
                {
                    builder.WriteInt32(length);

                    using (
                        var deflater = new CountingDeflateStream(
                            builder,
                            CompressionLevel.Optimal,
                            leaveOpen: true
                            )
                        )
                    {
                        stream.CopyTo(deflater);

                        if (length != deflater.BytesWritten)
                        {
                            throw new EndOfStreamException();
                        }
                    }

                    return(builder.ToImmutableArray());
                }
            }
        }
Ejemplo n.º 3
0
        private static ImmutableArray <byte> CreateBlob(SourceText text)
        {
            RoslynDebug.Assert(text != null);
            RoslynDebug.Assert(text.CanBeEmbedded);
            RoslynDebug.Assert(text.Encoding != null);
            RoslynDebug.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 (var builder = BlobBuildingStream.GetInstance())
            {
                if (maxByteCount < CompressionThreshold)
                {
                    builder.WriteInt32(0);

                    using (var writer = new StreamWriter(builder, text.Encoding, bufferSize: Math.Max(1, text.Length), leaveOpen: true))
                    {
                        text.Write(writer);
                    }
                }
                else
                {
                    Blob reserved = builder.ReserveBytes(4);

                    using (var deflater = new CountingDeflateStream(builder, CompressionLevel.Optimal, leaveOpen: true))
                    {
                        using (var writer = new StreamWriter(deflater, text.Encoding, bufferSize: 1024, leaveOpen: true))
                        {
                            text.Write(writer);
                        }

                        new BlobWriter(reserved).WriteInt32(deflater.BytesWritten);
                    }
                }

                return(builder.ToImmutableArray());
            }
        }
Ejemplo n.º 4
0
 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());
     }
 }
Ejemplo n.º 5
0
        /// <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());
            }
        }