MetadataUtil { public static Metadata copyFromHeaders(HttpHeaders headers) { if (headers.isEmpty()) { return new Metadata(); } int numHeaders = 0; for (Entry<AsciiString, String> entry : headers) { final AsciiString name = entry.getKey(); final String value = entry.getValue(); if (STRIPPED_HEADERS.contains(name)) { continue; } if (isBinary(name)) { numHeaders += COMMA_MATCHER.countIn(value) + 1; } else { numHeaders += 1; } } final byte[][] metadata = new byte[numHeaders * 2][]; int i = 0; for (Entry<AsciiString, String> entry : headers) { final AsciiString name = entry.getKey(); final String value = entry.getValue(); if (STRIPPED_HEADERS.contains(name)) { continue; } final byte[] nameBytes = name.isEntireArrayUsed() ? name.array() : name.toByteArray(); if (isBinary(name)) { int commaIndex = COMMA_MATCHER.indexIn(value); if (commaIndex == -1) { metadata[i++] = nameBytes; metadata[i++] = Base64.getDecoder().decode(value); } else { int substringStartIndex = 0; while (commaIndex != -1) { final String substring = value.substring(substringStartIndex, commaIndex); metadata[i++] = nameBytes; metadata[i++] = Base64.getDecoder().decode(substring); substringStartIndex = commaIndex + 1; commaIndex = COMMA_MATCHER.indexIn(value, commaIndex + 1); } final String substring = value.substring(substringStartIndex); metadata[i++] = nameBytes; metadata[i++] = Base64.getDecoder().decode(substring); } } else { metadata[i++] = nameBytes; metadata[i++] = value.getBytes(StandardCharsets.UTF_8); } } return InternalMetadata.newMetadata(metadata); } private  MetadataUtil(); static void fillHeaders(Metadata metadata, HttpHeadersBuilder builder); static Metadata copyFromHeaders(HttpHeaders headers);  }
@Test void copyFromHeadersTest() { final HttpHeaders trailers = ResponseHeaders.builder() .endOfStream(true) .add(HttpHeaderNames.STATUS, HttpStatus.OK.codeAsText()) .add(HttpHeaderNames.CONTENT_TYPE, "application/grpc+proto") .add(GrpcHeaderNames.GRPC_STATUS, "3") .add(GrpcHeaderNames.GRPC_MESSAGE, "test_grpc_message") .add(TEST_ASCII_KEY.originalName(), "test_message") .add(GrpcHeaderNames.ARMERIA_GRPC_THROWABLEPROTO_BIN, Base64.getEncoder().encodeToString(THROWABLE_PROTO.toByteArray())) .build(); final Metadata metadata = MetadataUtil.copyFromHeaders(trailers); assertThat(metadata.get(TEST_ASCII_KEY)).isEqualTo("test_message"); assertThat(metadata.get(STATUS_KEY)).isNull(); assertThat(metadata.get(InternalStatus.CODE_KEY)).isNull(); assertThat(metadata.get(InternalStatus.MESSAGE_KEY)).isNull(); assertThat(metadata.get(THROWABLE_PROTO_METADATA_KEY)).isNull(); }