From fdb4cc7ea5f9ad4af9d9a24611b1ad08531f40f6 Mon Sep 17 00:00:00 2001 From: csharptest Date: Tue, 7 May 2013 16:12:45 -0500 Subject: 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. --- src/ProtocolBuffers/ByteString.cs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'src') diff --git a/src/ProtocolBuffers/ByteString.cs b/src/ProtocolBuffers/ByteString.cs index ea17ca1e..8b7bd06c 100644 --- a/src/ProtocolBuffers/ByteString.cs +++ b/src/ProtocolBuffers/ByteString.cs @@ -52,6 +52,30 @@ namespace Google.ProtocolBuffers private readonly byte[] bytes; + /// + /// Unsafe operations that can cause IO Failure and/or other catestrophic side-effects. + /// + public static class Unsafe + { + /// + /// Constructs a new ByteString from the given byte array. The array is + /// *not* copied, and must not be modified after this constructor is called. + /// + public static ByteString FromBytes(byte[] bytes) + { + return new ByteString(bytes); + } + + /// + /// Provides direct, unrestricted access to the bytes contained in this instance. + /// You must not modify or resize the byte array returned by this method. + /// + public static byte[] GetBuffer(ByteString bytes) + { + return bytes.bytes; + } + } + /// /// Internal use only. Ensure that the provided array is not mutated and belongs to this instance. /// -- cgit v1.2.3