1 #region Copyright notice and license 2 // Protocol Buffers - Google's data interchange format 3 // Copyright 2008 Google Inc. All rights reserved. 4 // 5 // Use of this source code is governed by a BSD-style 6 // license that can be found in the LICENSE file or at 7 // https://developers.google.com/open-source/licenses/bsd 8 #endregion 9 10 using System; 11 using System.Security; 12 13 namespace Google.Protobuf 14 { 15 /// <summary> 16 /// Provides a number of unsafe byte operations to be used by advanced applications with high performance 17 /// requirements. These methods are referred to as "unsafe" due to the fact that they potentially expose 18 /// the backing buffer of a <see cref="ByteString"/> to the application. 19 /// </summary> 20 /// <remarks> 21 /// <para> 22 /// The methods in this class should only be called if it is guaranteed that the buffer backing the 23 /// <see cref="ByteString"/> will never change! Mutation of a <see cref="ByteString"/> can lead to unexpected 24 /// and undesirable consequences in your application, and will likely be difficult to debug. Proceed with caution! 25 /// </para> 26 /// <para> 27 /// This can have a number of significant side affects that have spooky-action-at-a-distance-like behavior. In 28 /// particular, if the bytes value changes out from under a Protocol Buffer: 29 /// </para> 30 /// <list type="bullet"> 31 /// <item> 32 /// <description>serialization may throw</description> 33 /// </item> 34 /// <item> 35 /// <description>serialization may succeed but the wrong bytes may be written out</description> 36 /// </item> 37 /// <item> 38 /// <description>objects that are normally immutable (such as ByteString) are no longer immutable</description> 39 /// </item> 40 /// <item> 41 /// <description>hashCode may be incorrect</description> 42 /// </item> 43 /// </list> 44 /// </remarks> 45 [SecuritySafeCritical] 46 public static class UnsafeByteOperations 47 { 48 /// <summary> 49 /// Constructs a new <see cref="ByteString" /> from the given bytes. The bytes are not copied, 50 /// and must not be modified while the <see cref="ByteString" /> is in use. 51 /// This API is experimental and subject to change. 52 /// </summary> UnsafeWrap(ReadOnlyMemory<byte> bytes)53 public static ByteString UnsafeWrap(ReadOnlyMemory<byte> bytes) 54 { 55 return ByteString.AttachBytes(bytes); 56 } 57 } 58 } 59