Backend API. Server-side code: this runs in a Codename One backend, not in the app on the device.

public final class GrpcMetadata

  1. Object
  2. GrpcMetadata

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.

Fields

public static final String BINARY_SUFFIX = "-bin"The suffix that marks a header as binary metadata.

Methods

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.
public static String formatTimeoutMillis(long millis)Formats a timeout for grpc-timeout, choosing the finest unit that keeps the value within eight digits.
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.
public static String decodeMessage(String value)Decodes a grpc-message.
public static boolean isBinary(String name)Whether a header name carries binary metadata.
public static String encodeBinary(byte[] value)Encodes binary metadata.
public static byte[] decodeBinary(String value)Decodes binary metadata, padded or not.

Inherited methods

Field details

BINARY_SUFFIX

public static final String BINARY_SUFFIX = "-bin"
The suffix that marks a header as binary metadata.

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 String
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 long
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 String
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 String
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 String
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 byte[]
the bytes

Returns

unpadded base64

decodeBinary

public static byte[] decodeBinary(String value)
Decodes binary metadata, padded or not.

Parameters

value String
the header value

Returns

the bytes, or null when the value is not base64