public void ProperStreamRead() { var firstRead = true; var sourceArray = new byte[] { 1, 2, 3, 4 }; int sourceOffset = 0; var stream = new TestStream(readFunc: (buf, offset, count) => { if (firstRead) { count = count / 2; firstRead = false; } Array.Copy(sourceArray, sourceOffset, buf, offset, count); sourceOffset += count; return(count); }); var builder = PooledBlobBuilder.GetInstance(sourceArray.Length); Assert.Equal(sourceArray.Length, builder.TryWriteBytes(stream, sourceArray.Length)); Assert.Equal(sourceArray, builder.ToArray()); builder.Free(); }
/// <summary> /// Encodes a string using UTF8 encoding to a blob and adds it to the Blob heap, if it's not there already. /// </summary> /// <param name="value">Constant value.</param> /// <param name="allowUnpairedSurrogates"> /// True to encode unpaired surrogates as specified, otherwise replace them with U+FFFD character. /// </param> /// <returns>Handle to the added or existing blob.</returns> /// <exception cref="ArgumentNullException"><paramref name="value"/> is null.</exception> public BlobHandle GetOrAddBlobUTF8(string value, bool allowUnpairedSurrogates = true) { var builder = PooledBlobBuilder.GetInstance(); builder.WriteUTF8(value, allowUnpairedSurrogates); var handle = GetOrAddBlob(builder); builder.Free(); return(handle); }
/// <summary> /// Encodes a string using UTF16 encoding to a blob and adds it to the Blob heap, if it's not there already. /// </summary> /// <param name="value">String.</param> /// <returns>Handle to the added or existing blob.</returns> /// <exception cref="ArgumentNullException"><paramref name="value"/> is null.</exception> public BlobHandle GetOrAddBlobUTF16(string value) { var builder = PooledBlobBuilder.GetInstance(); builder.WriteUTF16(value); var handle = GetOrAddBlob(builder); builder.Free(); return(handle); }
public void PrematureEndOfStream() { var sourceArray = new byte[] { 1, 2, 3, 4 }; var stream = new MemoryStream(sourceArray); var destArray = new byte[6]; var builder = PooledBlobBuilder.GetInstance(destArray.Length); // Try to write more bytes than exist in the stream Assert.Equal(4, builder.TryWriteBytes(stream, 6)); Assert.Equal(sourceArray, builder.ToArray()); builder.Free(); }
/// <summary> /// Encodes a constant value to a blob and adds it to the Blob heap, if it's not there already. /// Uses UTF16 to encode string constants. /// </summary> /// <param name="value">Constant value.</param> /// <returns>Handle to the added or existing blob.</returns> public unsafe BlobHandle GetOrAddConstantBlob(object?value) { if (value is string str) { return(GetOrAddBlobUTF16(str)); } var builder = PooledBlobBuilder.GetInstance(); builder.WriteConstant(value); var result = GetOrAddBlob(builder); builder.Free(); return(result); }
public void Pooled() { var builder1 = PooledBlobBuilder.GetInstance(); var builder2 = PooledBlobBuilder.GetInstance(); var builder3 = new BlobBuilder(); builder1.WriteByte(1); builder2.WriteByte(2); builder3.WriteByte(3); // mix pooled with non-pooled builder1.LinkPrefix(builder3); builder1.Free(); builder2.Free(); }
/// <summary> /// Encodes a debug document name and adds it to the Blob heap, if it's not there already. /// </summary> /// <param name="value">Document name.</param> /// <returns> /// Handle to the added or existing document name blob /// (see https://github.com/dotnet/corefx/blob/master/src/System.Reflection.Metadata/specs/PortablePdb-Metadata.md#DocumentNameBlob). /// </returns> /// <exception cref="ArgumentNullException"><paramref name="value"/> is null.</exception> public BlobHandle GetOrAddDocumentName(string value) { if (value == null) { Throw.ArgumentNull(nameof(value)); } char separator = ChooseSeparator(value); var resultBuilder = PooledBlobBuilder.GetInstance(); resultBuilder.WriteByte((byte)separator); var partBuilder = PooledBlobBuilder.GetInstance(); int i = 0; while (true) { int next = value.IndexOf(separator, i); partBuilder.WriteUTF8(value, i, (next >= 0 ? next : value.Length) - i, allowUnpairedSurrogates: true, prependSize: false); resultBuilder.WriteCompressedInteger(GetOrAddBlob(partBuilder).GetHeapOffset()); if (next == -1) { break; } if (next == value.Length - 1) { // trailing separator: resultBuilder.WriteByte(0); break; } partBuilder.Clear(); i = next + 1; } partBuilder.Free(); var resultHandle = GetOrAddBlob(resultBuilder); resultBuilder.Free(); return(resultHandle); }
public BlobHandle GetOrAddConstantBlob(object value) { string str = value as string; if (str != null) { return(GetOrAddBlob(str)); } var builder = PooledBlobBuilder.GetInstance(); builder.WriteConstant(value); var result = GetOrAddBlob(builder); builder.Free(); return(result); }