소스 검색

Adde 'Unsafe' static type in ByteString to allow direct buffer manipulation without
copying bytes. Should be used very cautiously as modifications to buffers may result
in unexpected behavior.

csharptest 12 년 전
부모
커밋
fdb4cc7ea5
1개의 변경된 파일24개의 추가작업 그리고 0개의 파일을 삭제
  1. 24 0
      src/ProtocolBuffers/ByteString.cs

+ 24 - 0
src/ProtocolBuffers/ByteString.cs

@@ -52,6 +52,30 @@ namespace Google.ProtocolBuffers
 
         private readonly byte[] bytes;
 
+        /// <summary>
+        /// Unsafe operations that can cause IO Failure and/or other catestrophic side-effects.
+        /// </summary>
+        public static class Unsafe
+        {
+            /// <summary>
+            /// Constructs a new ByteString from the given byte array. The array is
+            /// *not* copied, and must not be modified after this constructor is called.
+            /// </summary>
+            public static ByteString FromBytes(byte[] bytes)
+            {
+                return new ByteString(bytes);
+            }
+
+            /// <summary>
+            /// Provides direct, unrestricted access to the bytes contained in this instance.
+            /// You must not modify or resize the byte array returned by this method.
+            /// </summary>
+            public static byte[] GetBuffer(ByteString bytes)
+            {
+                return bytes.bytes;
+            }
+        }
+
         /// <summary>
         /// Internal use only.  Ensure that the provided array is not mutated and belongs to this instance.
         /// </summary>