Package io.grpc

Class Metadata


  • @NotThreadSafe
    public final class Metadata
    extends java.lang.Object
    Provides access to read and write metadata values to be exchanged during a call.

    Keys are allowed to be associated with more than one value.

    This class is not thread safe, implementations should ensure that header reads and writes do not occur in multiple threads concurrently.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static interface  Metadata.AsciiMarshaller<T>
      Marshaller for metadata values that are serialized into ASCII strings.
      static interface  Metadata.BinaryMarshaller<T>
      Marshaller for metadata values that are serialized into raw binary.
      static interface  Metadata.BinaryStreamMarshaller<T>
      Marshaller for metadata values that are serialized to an InputStream.
      static class  Metadata.Key<T>
      Key for metadata entries.
    • Constructor Summary

      Constructors 
      Constructor Description
      Metadata()
      Constructor called by the application layer when it wants to send metadata.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean containsKey​(Metadata.Key<?> key)
      Returns true if a value is defined for the given key.
      <T> void discardAll​(Metadata.Key<T> key)
      Remove all values for the given key without returning them.
      <T> T get​(Metadata.Key<T> key)
      Returns the last metadata entry added with the name 'name' parsed as T.
      <T> java.lang.Iterable<T> getAll​(Metadata.Key<T> key)
      Returns all the metadata entries named 'name', in the order they were received, parsed as T, or null if there are none.
      java.util.Set<java.lang.String> keys()
      Returns set of all keys in store.
      void merge​(Metadata other)
      Perform a simple merge of two sets of metadata.
      void merge​(Metadata other, java.util.Set<Metadata.Key<?>> keys)
      Merge values from the given set of keys into this set of metadata.
      <T> void put​(Metadata.Key<T> key, T value)
      Adds the key, value pair.
      <T> boolean remove​(Metadata.Key<T> key, T value)
      Removes the first occurrence of value for key.
      <T> java.lang.Iterable<T> removeAll​(Metadata.Key<T> key)
      Remove all values for the given key.
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • BINARY_HEADER_SUFFIX

        public static final java.lang.String BINARY_HEADER_SUFFIX
        All binary headers should have this suffix in their names. Vice versa.

        Its value is "-bin". An ASCII header's name must not end with this.

        See Also:
        Constant Field Values
      • BINARY_BYTE_MARSHALLER

        public static final Metadata.BinaryMarshaller<byte[]> BINARY_BYTE_MARSHALLER
        Simple metadata marshaller that encodes bytes as is.

        This should be used when raw bytes are favored over un-serialized version of object. Can be helpful in situations where more processing to bytes is needed on application side, avoids double encoding/decoding.

        Both Metadata.BinaryMarshaller.toBytes(T) and Metadata.BinaryMarshaller.parseBytes(byte[]) methods do not return a copy of the byte array. Do _not_ modify the byte arrays of either the arguments or return values.

      • ASCII_STRING_MARSHALLER

        public static final Metadata.AsciiMarshaller<java.lang.String> ASCII_STRING_MARSHALLER
        Simple metadata marshaller that encodes strings as is.

        This should be used with ASCII strings that only contain the characters listed in the class comment of Metadata.AsciiMarshaller. Otherwise the output may be considered invalid and discarded by the transport, or the call may fail.

    • Constructor Detail

      • Metadata

        public Metadata()
        Constructor called by the application layer when it wants to send metadata.
    • Method Detail

      • get

        @Nullable
        public <T> T get​(Metadata.Key<T> key)
        Returns the last metadata entry added with the name 'name' parsed as T.
        Returns:
        the parsed metadata entry or null if there are none.
      • getAll

        @Nullable
        public <T> java.lang.Iterable<T> getAll​(Metadata.Key<T> key)
        Returns all the metadata entries named 'name', in the order they were received, parsed as T, or null if there are none. The iterator is not guaranteed to be "live." It may or may not be accurate if Metadata is mutated.
      • keys

        public java.util.Set<java.lang.String> keys()
        Returns set of all keys in store.
        Returns:
        unmodifiable Set of keys
      • put

        public <T> void put​(Metadata.Key<T> key,
                            T value)
        Adds the key, value pair. If key already has values, value is added to the end. Duplicate values for the same key are permitted.
        Throws:
        java.lang.NullPointerException - if key or value is null
      • remove

        public <T> boolean remove​(Metadata.Key<T> key,
                                  T value)
        Removes the first occurrence of value for key.
        Parameters:
        key - key for value
        value - value
        Returns:
        true if value removed; false if value was not present
        Throws:
        java.lang.NullPointerException - if key or value is null
      • removeAll

        public <T> java.lang.Iterable<T> removeAll​(Metadata.Key<T> key)
        Remove all values for the given key. If there were no values, null is returned.
      • discardAll

        @ExperimentalApi("https://github.com/grpc/grpc-java/issues/4691")
        public <T> void discardAll​(Metadata.Key<T> key)
        Remove all values for the given key without returning them. This is a minor performance optimization if you do not need the previous values.
      • merge

        public void merge​(Metadata other)
        Perform a simple merge of two sets of metadata.

        This is a purely additive operation, because a single key can be associated with multiple values.

      • merge

        public void merge​(Metadata other,
                          java.util.Set<Metadata.Key<?>> keys)
        Merge values from the given set of keys into this set of metadata. If a key is present in keys, then all of the associated values will be copied over.
        Parameters:
        other - The source of the new key values.
        keys - The subset of matching key we want to copy, if they exist in the source.
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object