Class GrpcMetadata

java.lang.Object
com.codename1.backend.grpc.GrpcMetadata

public final class GrpcMetadata extends Object

The encodings gRPC layers on top of HTTP/2 headers: the grpc-timeout deadline, the percent-encoded grpc-message, and binary metadata in headers whose name ends in -bin.

Each is small and each has been gotten wrong by somebody: a timeout whose unit letter is case sensitive (M minutes, m milliseconds), a status message that must survive any UTF-8 a handler puts in it, and base64 that a sender may or may not pad.

  • Field Details

    • BINARY_SUFFIX

      public static final String BINARY_SUFFIX
      The suffix that marks a header as binary metadata.
      See Also:
  • Method Details

    • parseTimeoutNanos

      public static long parseTimeoutNanos(String value)
      Parses grpc-timeout: at most eight ASCII digits and a unit, one of H M S m u n. The unit is case sensitive.
      Parameters:
      value - the header value, may be null
      Returns:
      the timeout in nanoseconds, or -1 when the header is absent or malformed (a malformed timeout is ignored, as grpc-java does)
    • formatTimeoutMillis

      public static String formatTimeoutMillis(long millis)
      Formats a timeout for grpc-timeout, choosing the finest unit that keeps the value within eight digits. A coarser unit rounds up, so the advertised deadline is never earlier than the real one.
      Parameters:
      millis - the remaining time in milliseconds, at least 1
      Returns:
      the header value
    • encodeMessage

      public static String encodeMessage(String message)
      Percent-encodes a status message for grpc-message: every byte of its UTF-8 form outside printable ASCII, and % itself, becomes %XX.
      Parameters:
      message - the message, may be null
      Returns:
      the encoded form, empty for null
    • decodeMessage

      public static String decodeMessage(String value)
      Decodes a grpc-message. A malformed escape is kept as it is rather than failing: the message is for people, and losing it helps nobody.
      Parameters:
      value - the header value, may be null
      Returns:
      the message, or null for null
    • isBinary

      public static boolean isBinary(String name)
      Whether a header name carries binary metadata.
      Parameters:
      name - a header name
      Returns:
      true for a name ending in -bin, compared without case
    • encodeBinary

      public static String encodeBinary(byte[] value)
      Encodes binary metadata. gRPC senders omit the padding; receivers accept either.
      Parameters:
      value - the bytes
      Returns:
      unpadded base64
    • decodeBinary

      public static byte[] decodeBinary(String value)
      Decodes binary metadata, padded or not.
      Parameters:
      value - the header value
      Returns:
      the bytes, or null when the value is not base64