diff options
author | Jon Skeet <skeet@pobox.com> | 2008-08-14 20:33:30 +0100 |
---|---|---|
committer | Jon Skeet <skeet@pobox.com> | 2008-08-14 20:33:30 +0100 |
commit | fe7bb26214916d0eec25e7690ff3580477ede510 (patch) | |
tree | 814fad30050df141a0a38df71adc5b3d8326ff14 /csharp/ProtocolBuffers/ByteString.cs | |
parent | c26b43d8cd182cfb5e0317b79c90f1f3f89759ba (diff) | |
download | protobuf-fe7bb26214916d0eec25e7690ff3580477ede510.tar.gz protobuf-fe7bb26214916d0eec25e7690ff3580477ede510.tar.bz2 protobuf-fe7bb26214916d0eec25e7690ff3580477ede510.zip |
Implemented AbstractMethod and split the descriptors into a new package
Diffstat (limited to 'csharp/ProtocolBuffers/ByteString.cs')
-rw-r--r-- | csharp/ProtocolBuffers/ByteString.cs | 31 |
1 files changed, 30 insertions, 1 deletions
diff --git a/csharp/ProtocolBuffers/ByteString.cs b/csharp/ProtocolBuffers/ByteString.cs index ba3e6512..34aea2df 100644 --- a/csharp/ProtocolBuffers/ByteString.cs +++ b/csharp/ProtocolBuffers/ByteString.cs @@ -106,6 +106,35 @@ namespace Google.ProtocolBuffers { } // TODO(jonskeet): CopyTo, Equals, GetHashCode if they turn out to be required - // TODO(jonskeet): Input/Output stuff + + /// <summary> + /// Builder for ByteStrings which allows them to be created without extra + /// copying being involved. This has to be a nested type in order to have access + /// to the private ByteString constructor. + /// </summary> + internal class CodedBuilder { + private readonly CodedOutputStream output; + private readonly byte[] buffer; + + internal CodedBuilder(int size) { + buffer = new byte[size]; + output = CodedOutputStream.CreateInstance(buffer); + } + + public ByteString Build() { + output.CheckNoSpaceLeft(); + + // We can be confident that the CodedOutputStream will not modify the + // underlying bytes anymore because it already wrote all of them. So, + // no need to make a copy. + return new ByteString(buffer); + } + + public CodedOutputStream CodedOutput { + get { + return output; + } + } + } } } |