TransportFrameUtil { public static byte[][] toHttp2Headers(Metadata headers) { byte[][] serializedHeaders = headers.serialize(); int k = 0; for (int i = 0; i < serializedHeaders.length; i += 2) { byte[] key = serializedHeaders[i]; byte[] value = serializedHeaders[i + 1]; if (endsWith(key, binaryHeaderSuffixBytes)) { serializedHeaders[k] = key; serializedHeaders[k + 1] = BaseEncoding.base64().encode(value).getBytes(US_ASCII); k += 2; } else { if (isSpecCompliantAscii(value)) { serializedHeaders[k] = key; serializedHeaders[k + 1] = value; k += 2; } else { String keyString = new String(key, US_ASCII); logger.warning("Metadata key=" + keyString + ", value=" + Arrays.toString(value) + " contains invalid ASCII characters"); } } } if (k == serializedHeaders.length) { return serializedHeaders; } return Arrays.copyOfRange(serializedHeaders, 0, k); } private  TransportFrameUtil(); static byte[][] toHttp2Headers(Metadata headers); static byte[][] toRawSerializedHeaders(byte[][] http2Headers);  }
@Test public void testToHttp2Headers() { Metadata headers = new Metadata(); headers.put(PLAIN_STRING, COMPLIANT_ASCII_STRING); headers.put(BINARY_STRING, NONCOMPLIANT_ASCII_STRING); headers.put(BINARY_STRING_WITHOUT_SUFFIX, NONCOMPLIANT_ASCII_STRING); byte[][] http2Headers = TransportFrameUtil.toHttp2Headers(headers); byte[][] answer = new byte[][] { "plainstring".getBytes(US_ASCII), COMPLIANT_ASCII_STRING.getBytes(US_ASCII), "string-bin".getBytes(US_ASCII), base64Encode(NONCOMPLIANT_ASCII_STRING.getBytes(US_ASCII))}; assertEquals(answer.length, http2Headers.length); for (int i = 0; i < answer.length; i += 2) { assertContains(http2Headers, answer[i], answer[i + 1]); } }