CheckNotNull() 공개 정적인 메소드

Throws an ArgumentNullException if the given value is null, otherwise return the value to the caller.
public static CheckNotNull ( value, string name ) : T
name string
리턴 T
예제 #1
0
        /// <summary>
        /// Constructs a <see cref="ByteString"/> from data in the given stream, synchronously.
        /// </summary>
        /// <remarks>If successful, <paramref name="stream"/> will be read completely, from the position
        /// at the start of the call.</remarks>
        /// <param name="stream">The stream to copy into a ByteString.</param>
        /// <returns>A ByteString with content read from the given stream.</returns>
        public static ByteString FromStream(Stream stream)
        {
            ProtoPreconditions.CheckNotNull(stream, "stream");
            int capacity     = stream.CanSeek ? checked ((int)(stream.Length - stream.Position)) : 0;
            var memoryStream = new MemoryStream(capacity);

            stream.CopyTo(memoryStream);

            // Avoid an extra copy if we can.
            byte[] bytes = memoryStream.Length == memoryStream.Capacity ? memoryStream.GetBuffer() : memoryStream.ToArray();

            return(AttachBytes(bytes));
        }
예제 #2
0
        internal static void MergeFrom(this IMessage message, byte[] data, int offset, int length, bool discardUnknownFields, ExtensionRegistry registry)
        {
            ProtoPreconditions.CheckNotNull(message, nameof(message));
            ProtoPreconditions.CheckNotNull(data, nameof(data));
            CodedInputStream input = new CodedInputStream(data, offset, length)
            {
                DiscardUnknownFields = discardUnknownFields,
                ExtensionRegistry    = registry
            };

            message.MergeFrom(input);
            input.CheckReadEndOfStreamTag();
        }
예제 #3
0
        internal static void MergeFrom(this IMessage message, Stream input, bool discardUnknownFields, ExtensionRegistry registry)
        {
            ProtoPreconditions.CheckNotNull(message, nameof(message));
            ProtoPreconditions.CheckNotNull(input, nameof(input));
            CodedInputStream codedInput = new CodedInputStream(input)
            {
                DiscardUnknownFields = discardUnknownFields,
                ExtensionRegistry    = registry
            };

            message.MergeFrom(codedInput);
            codedInput.CheckReadEndOfStreamTag();
        }
예제 #4
0
        /// <summary>
        /// Formats the specified message as JSON.
        /// </summary>
        /// <param name="message">The message to format.</param>
        /// <param name="writer">The TextWriter to write the formatted message to.</param>
        /// <returns>The formatted message.</returns>
        public void Format(IMessage message, TextWriter writer)
        {
            ProtoPreconditions.CheckNotNull(message, "message");
            ProtoPreconditions.CheckNotNull(writer, "writer");

            if (message.Descriptor.IsWellKnownType)
            {
                WriteWellKnownTypeValue(writer, message.Descriptor, message);
            }
            else
            {
                WriteMessage(writer, message);
            }
        }
예제 #5
0
        /// <summary>
        /// Constructs a <see cref="ByteString"/> from data in the given stream, synchronously.
        /// </summary>
        /// <remarks>If successful, <paramref name="stream"/> will be read completely, from the position
        /// at the start of the call.</remarks>
        /// <param name="stream">The stream to copy into a ByteString.</param>
        /// <returns>A ByteString with content read from the given stream.</returns>
        public static ByteString FromStream(Stream stream)
        {
            ProtoPreconditions.CheckNotNull(stream, nameof(stream));
            int capacity     = stream.CanSeek ? checked ((int)(stream.Length - stream.Position)) : 0;
            var memoryStream = new MemoryStream(capacity);

            stream.CopyTo(memoryStream);
#if NETSTANDARD1_0 || WINDOWS_UWP
            byte[] bytes = memoryStream.ToArray();
#else
            // Avoid an extra copy if we can.
            byte[] bytes = memoryStream.Length == memoryStream.Capacity ? memoryStream.GetBuffer() : memoryStream.ToArray();
#endif
            return(AttachBytes(bytes));
        }
        /// <summary>
        /// Formats the specified message as JSON.
        /// </summary>
        /// <param name="message">The message to format.</param>
        /// <returns>The formatted message.</returns>
        public string Format(IMessage message)
        {
            ProtoPreconditions.CheckNotNull(message, nameof(message));
            StringBuilder builder = new StringBuilder();

            if (message.Descriptor.IsWellKnownType)
            {
                WriteWellKnownTypeValue(builder, message.Descriptor, message);
            }
            else
            {
                WriteMessage(builder, message);
            }
            return(builder.ToString());
        }
예제 #7
0
        /// <summary>
        /// Constructs a <see cref="ByteString"/> from data in the given stream, asynchronously.
        /// </summary>
        /// <remarks>If successful, <paramref name="stream"/> will be read completely, from the position
        /// at the start of the call.</remarks>
        /// <param name="stream">The stream to copy into a ByteString.</param>
        /// <param name="cancellationToken">The cancellation token to use when reading from the stream, if any.</param>
        /// <returns>A ByteString with content read from the given stream.</returns>
        public async static Task <ByteString> FromStreamAsync(Stream stream, CancellationToken cancellationToken = default(CancellationToken))
        {
            ProtoPreconditions.CheckNotNull(stream, nameof(stream));
            int capacity     = stream.CanSeek ? checked ((int)(stream.Length - stream.Position)) : 0;
            var memoryStream = new MemoryStream(capacity);
            // We have to specify the buffer size here, as there's no overload accepting the cancellation token
            // alone. But it's documented to use 81920 by default if not specified.
            await stream.CopyToAsync(memoryStream, 81920, cancellationToken);

#if NETSTANDARD1_0
            byte[] bytes = memoryStream.ToArray();
#else
            // Avoid an extra copy if we can.
            byte[] bytes = memoryStream.Length == memoryStream.Capacity ? memoryStream.GetBuffer() : memoryStream.ToArray();
#endif
            return(AttachBytes(bytes));
        }
예제 #8
0
        /// <summary>
        /// Copies the arrays in the registry set to the specified array at the specified index
        /// </summary>
        /// <param name="array">The array to copy to</param>
        /// <param name="arrayIndex">The array index to start at</param>
        void ICollection <Extension> .CopyTo(Extension[] array, int arrayIndex)
        {
            ProtoPreconditions.CheckNotNull(array, nameof(array));
            if (arrayIndex < 0 || arrayIndex >= array.Length)
            {
                throw new ArgumentOutOfRangeException(nameof(arrayIndex));
            }
            if (array.Length - arrayIndex < Count)
            {
                throw new ArgumentException("The provided array is shorter than the number of elements in the registry");
            }

            for (int i = 0; i < array.Length; i++)
            {
                Extension extension = array[i];
                extensions.Add(new ObjectIntPair <Type>(extension.TargetType, extension.FieldNumber), extension);
            }
        }
예제 #9
0
        /// <summary>
        /// Adds the specified extension to the registry
        /// </summary>
        public void Add(Extension extension)
        {
            ProtoPreconditions.CheckNotNull(extension, nameof(extension));

            extensions.Add(new ObjectIntPair <Type>(extension.TargetType, extension.FieldNumber), extension);
        }
예제 #10
0
 /// <summary>
 /// Constructs a <see cref="ByteString"/> from data in the given stream, asynchronously.
 /// </summary>
 /// <remarks>If successful, <paramref name="stream"/> will be read completely, from the position
 /// at the start of the call.</remarks>
 /// <param name="stream">The stream to copy into a ByteString.</param>
 /// <param name="cancellationToken">The cancellation token to use when reading from the stream, if any.</param>
 /// <returns>A ByteString with content read from the given stream.</returns>
 public static Task <ByteString> FromStreamAsync(Stream stream, CancellationToken cancellationToken = default(CancellationToken))
 {
     ProtoPreconditions.CheckNotNull(stream, nameof(stream));
     return(ByteStringAsync.FromStreamAsyncCore(stream, cancellationToken));
 }
예제 #11
0
 /// <summary>
 /// Creates a new formatted with the given settings.
 /// </summary>
 /// <param name="settings">The settings.</param>
 public JsonFormatter(Settings settings)
 {
     this.settings = ProtoPreconditions.CheckNotNull(settings, nameof(settings));
 }
 /// <summary>
 /// Creates a new CodedInputStream reading data from the given stream.
 /// </summary>
 public CodedInputStream(Stream input) : this(input, new byte[BufferSize], 0, 0)
 {
     ProtoPreconditions.CheckNotNull(input, "input");
 }
예제 #13
0
 /// <summary>
 /// Creates a new <see cref="Settings"/> object with the specified unknown type handling, recursion limit and type registry.
 /// </summary>
 /// <param name="ignoreUnknownTypes">Should an unknown Type be ignored during parsing</param>
 /// <param name="recursionLimit">The maximum depth of messages to parse</param>
 /// <param name="typeRegistry">The type registry used to parse <see cref="Any"/> messages</param>
 public Settings(bool ignoreUnknownTypes, int recursionLimit, TypeRegistry typeRegistry)
 {
     IgnoreUnknownTypes = ignoreUnknownTypes;
     RecursionLimit     = recursionLimit;
     TypeRegistry       = ProtoPreconditions.CheckNotNull(typeRegistry, nameof(typeRegistry));
 }
예제 #14
0
 public void Set(byte[] buffer)
 {
     Set(null, ProtoPreconditions.CheckNotNull(buffer, "buffer"), true);
 }
예제 #15
0
 /// <summary>
 /// Creates a new <see cref="Settings"/> object based on this one, but with the specified type registry.
 /// </summary>
 /// <param name="typeRegistry">The new type registry. Must not be null.</param>
 public Settings WithTypeRegistry(TypeRegistry typeRegistry)
 {
     return(new Settings(RecursionLimit, ProtoPreconditions.CheckNotNull(typeRegistry, "typeRegistry"), IgnoreUnknownFields));
 }
예제 #16
0
 private Settings(int recursionLimit, TypeRegistry typeRegistry, bool ignoreUnknownFields)
 {
     RecursionLimit      = recursionLimit;
     TypeRegistry        = ProtoPreconditions.CheckNotNull(typeRegistry, nameof(typeRegistry));
     IgnoreUnknownFields = ignoreUnknownFields;
 }
예제 #17
0
 /// <summary>
 /// Converts the given message into a byte string in protobuf encoding.
 /// </summary>
 /// <param name="message">The message to convert.</param>
 /// <returns>The message data as a byte string.</returns>
 public static ByteString ToByteString(this IMessage message)
 {
     ProtoPreconditions.CheckNotNull(message, "message");
     return(ByteString.AttachBytes(message.ToByteArray()));
 }
예제 #18
0
        /// <summary>
        /// Removes the specified extension from the set
        /// </summary>
        /// <param name="item">The extension</param>
        /// <returns><c>true</c> if the extension was removed, otherwise <c>false</c></returns>
        public bool Remove(Extension item)
        {
            ProtoPreconditions.CheckNotNull(item, nameof(item));

            return(extensions.Remove(new ObjectIntPair <Type>(item.TargetType, item.FieldNumber)));
        }
예제 #19
0
 /// <summary>
 /// Creates a new <see cref="Settings"/> object based on this one, but with the specified type registry.
 /// </summary>
 /// <param name="typeRegistry">The new type registry. Must not be null.</param>
 public Settings WithTypeRegistry(TypeRegistry typeRegistry) =>
 new Settings(
     RecursionLimit,
     ProtoPreconditions.CheckNotNull(typeRegistry, nameof(typeRegistry)),
     IgnoreUnknownFields);
예제 #20
0
 /// <summary>
 /// Creates a new <see cref="Settings"/> object with the specified formatting of default values
 /// and type registry.
 /// </summary>
 /// <param name="formatDefaultValues"><c>true</c> if default values (0, empty strings etc) should be formatted; <c>false</c> otherwise.</param>
 /// <param name="typeRegistry">The <see cref="TypeRegistry"/> to use when formatting <see cref="Any"/> messages.</param>
 public Settings(bool formatDefaultValues, TypeRegistry typeRegistry)
 {
     FormatDefaultValues = formatDefaultValues;
     TypeRegistry        = ProtoPreconditions.CheckNotNull(typeRegistry, "typeRegistry");
 }
예제 #21
0
 /// <summary>
 /// Creates a new <see cref="Settings"/> object with the specified recursion limit and type registry.
 /// </summary>
 /// <param name="recursionLimit">The maximum depth of messages to parse</param>
 /// <param name="typeRegistry">The type registry used to parse <see cref="Any"/> messages</param>
 public Settings(int recursionLimit, TypeRegistry typeRegistry)
 {
     RecursionLimit = recursionLimit;
     TypeRegistry   = ProtoPreconditions.CheckNotNull(typeRegistry, nameof(typeRegistry));
 }
예제 #22
0
 /// <summary>
 /// Converts a message to JSON for diagnostic purposes with no extra context.
 /// </summary>
 /// <remarks>
 /// <para>
 /// This differs from calling <see cref="Format(IMessage)"/> on the default JSON
 /// formatter in its handling of <see cref="Any"/>. As no type registry is available
 /// in <see cref="object.ToString"/> calls, the normal way of resolving the type of
 /// an <c>Any</c> message cannot be applied. Instead, a JSON property named <c>@value</c>
 /// is included with the base64 data from the <see cref="Any.Value"/> property of the message.
 /// </para>
 /// <para>The value returned by this method is only designed to be used for diagnostic
 /// purposes. It may not be parsable by <see cref="JsonParser"/>, and may not be parsable
 /// by other Protocol Buffer implementations.</para>
 /// </remarks>
 /// <param name="message">The message to format for diagnostic purposes.</param>
 /// <returns>The diagnostic-only JSON representation of the message</returns>
 public static string ToDiagnosticString(IMessage message)
 {
     ProtoPreconditions.CheckNotNull(message, "message");
     return(diagnosticFormatter.Format(message));
 }
예제 #23
0
        /// <summary>
        /// Adds the specified extensions to the registry
        /// </summary>
        public void Add(params Extension[] newExtensions)
        {
            ProtoPreconditions.CheckNotNull(newExtensions, nameof(newExtensions));

            Add((IEnumerable <Extension>)newExtensions);
        }
예제 #24
0
 /// <summary>
 /// Parses <paramref name="json"/> into a new message.
 /// </summary>
 /// <typeparam name="T">The type of message to create.</typeparam>
 /// <param name="json">The JSON to parse.</param>
 /// <exception cref="InvalidJsonException">The JSON does not comply with RFC 7159</exception>
 /// <exception cref="InvalidProtocolBufferException">The JSON does not represent a Protocol Buffers message correctly</exception>
 public T Parse <T>(string json) where T : IMessage, new()
 {
     ProtoPreconditions.CheckNotNull(json, nameof(json));
     return(Parse <T>(new StringReader(json)));
 }
예제 #25
0
 public void Set(byte[] buffer)
 {
     Set(null, ProtoPreconditions.CheckNotNull(buffer, "buffer"), 0, buffer.Length);
 }