public void WriteData(BlobBuilder resourceWriter) { if (_fileReference == null) { try { using (Stream stream = _streamProvider()) { if (stream == null) { throw new InvalidOperationException(CodeAnalysisResources.ResourceStreamProviderShouldReturnNonNullStream); } var count = (int)(stream.Length - stream.Position); resourceWriter.WriteInt32(count); int bytesWritten = resourceWriter.TryWriteBytes(stream, count); if (bytesWritten != count) { throw new EndOfStreamException( string.Format(CultureInfo.CurrentUICulture, CodeAnalysisResources.ResourceStreamEndedUnexpectedly, bytesWritten, count)); } resourceWriter.Align(8); } } catch (Exception e) { throw new ResourceException(_name, e); } } }
public void WriteData(BlobBuilder resourceWriter) { if (_fileReference == null) { try { #nullable disable // Can '_streamProvider' be null? https://github.com/dotnet/roslyn/issues/39166 using (Stream stream = _streamProvider()) #nullable enable { if (stream == null) { throw new InvalidOperationException(CodeAnalysisResources.ResourceStreamProviderShouldReturnNonNullStream); } var count = (int)(stream.Length - stream.Position); resourceWriter.WriteInt32(count); int bytesWritten = resourceWriter.TryWriteBytes(stream, count); if (bytesWritten != count) { throw new EndOfStreamException( string.Format(CultureInfo.CurrentUICulture, CodeAnalysisResources.ResourceStreamEndedUnexpectedly, bytesWritten, count)); } resourceWriter.Align(8); } }
public unsafe void Write_Errors() { var builder = new BlobBuilder(16); Assert.Throws<ArgumentNullException>(() => builder.WriteUTF16((char[])null)); Assert.Throws<ArgumentNullException>(() => builder.WriteUTF16((string)null)); Assert.Throws<ArgumentNullException>(() => builder.WriteUTF8(null, allowUnpairedSurrogates: true)); Assert.Throws<ArgumentNullException>(() => builder.WriteUTF8(null, allowUnpairedSurrogates: true)); Assert.Throws<ArgumentNullException>(() => builder.TryWriteBytes((Stream)null, 0)); Assert.Throws<ArgumentNullException>(() => builder.WriteBytes(null)); Assert.Throws<ArgumentNullException>(() => builder.WriteBytes(null, 0, 0)); Assert.Throws<ArgumentNullException>(() => builder.WriteBytes((byte*)null, 0)); Assert.Throws<ArgumentNullException>(() => builder.WriteBytes(default(ImmutableArray<byte>))); Assert.Throws<ArgumentNullException>(() => builder.WriteBytes(default(ImmutableArray<byte>), 0, 0)); var bw = default(BlobWriter); Assert.Throws<ArgumentNullException>(() => builder.WriteContentTo(ref bw)); Assert.Throws<ArgumentNullException>(() => builder.WriteContentTo((Stream)null)); Assert.Throws<ArgumentNullException>(() => builder.WriteContentTo((BlobBuilder)null)); Assert.Throws<ArgumentOutOfRangeException>(() => builder.TryWriteBytes(new MemoryStream(), -1)); Assert.Throws<ArgumentOutOfRangeException>(() => builder.WriteBytes(0, -1)); Assert.Throws<ArgumentOutOfRangeException>(() => builder.WriteBytes(new byte[] { }, 1, 0)); Assert.Throws<ArgumentOutOfRangeException>(() => builder.WriteBytes(new byte[] { }, 0, 1)); Assert.Throws<ArgumentOutOfRangeException>(() => builder.WriteBytes(new byte[] { }, 0, -1)); Assert.Throws<ArgumentOutOfRangeException>(() => builder.WriteBytes(ImmutableArray<byte>.Empty, 1, 0)); Assert.Throws<ArgumentOutOfRangeException>(() => builder.WriteBytes(ImmutableArray<byte>.Empty, 0, 1)); Assert.Throws<ArgumentOutOfRangeException>(() => builder.WriteBytes(ImmutableArray<byte>.Empty, 1, -1)); }