1 /* 2 * Copyright 2016 The gRPC Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package io.grpc; 18 19 import com.google.common.io.BaseEncoding; 20 import io.grpc.Metadata.AsciiMarshaller; 21 import io.grpc.Metadata.BinaryStreamMarshaller; 22 import java.io.InputStream; 23 import java.nio.charset.Charset; 24 25 /** 26 * Internal {@link Metadata} accessor. This is intended for use by io.grpc.internal, and the 27 * specifically supported transport packages. If you *really* think you need to use this, contact 28 * the gRPC team first. 29 */ 30 @Internal 31 public final class InternalMetadata { 32 33 /** 34 * A specialized plain ASCII marshaller. Both input and output are assumed to be valid header 35 * ASCII. 36 * 37 * <p>Extended here to break the dependency. 38 */ 39 @Internal 40 public interface TrustedAsciiMarshaller<T> extends Metadata.TrustedAsciiMarshaller<T> {} 41 42 /** 43 * Copy of StandardCharsets, which is only available on Java 1.7 and above. 44 */ 45 @Internal 46 public static final Charset US_ASCII = Charset.forName("US-ASCII"); 47 48 /** 49 * An instance of base64 encoder that omits padding. 50 */ 51 @Internal 52 public static final BaseEncoding BASE64_ENCODING_OMIT_PADDING 53 = Metadata.BASE64_ENCODING_OMIT_PADDING; 54 55 @Internal keyOf(String name, TrustedAsciiMarshaller<T> marshaller)56 public static <T> Metadata.Key<T> keyOf(String name, TrustedAsciiMarshaller<T> marshaller) { 57 boolean isPseudo = name != null && !name.isEmpty() && name.charAt(0) == ':'; 58 return Metadata.Key.of(name, isPseudo, marshaller); 59 } 60 61 @Internal keyOf(String name, AsciiMarshaller<T> marshaller)62 public static <T> Metadata.Key<T> keyOf(String name, AsciiMarshaller<T> marshaller) { 63 boolean isPseudo = name != null && !name.isEmpty() && name.charAt(0) == ':'; 64 return Metadata.Key.of(name, isPseudo, marshaller); 65 } 66 67 @Internal newMetadata(byte[]... binaryValues)68 public static Metadata newMetadata(byte[]... binaryValues) { 69 return new Metadata(binaryValues); 70 } 71 72 @Internal newMetadata(int usedNames, byte[]... binaryValues)73 public static Metadata newMetadata(int usedNames, byte[]... binaryValues) { 74 return new Metadata(usedNames, binaryValues); 75 } 76 77 @Internal serialize(Metadata md)78 public static byte[][] serialize(Metadata md) { 79 return md.serialize(); 80 } 81 82 @Internal headerCount(Metadata md)83 public static int headerCount(Metadata md) { 84 return md.headerCount(); 85 } 86 87 /** 88 * Serializes all metadata entries, leaving some values as {@link InputStream}s. 89 * 90 * <p>Produces serialized names and values interleaved. result[i*2] are names, while 91 * result[i*2+1] are values. 92 * 93 * <p>Names are byte arrays as described according to the {@link Metadata#serialize} 94 * method. Values are either byte arrays or {@link InputStream}s. 95 */ 96 @Internal serializePartial(Metadata md)97 public static Object[] serializePartial(Metadata md) { 98 return md.serializePartial(); 99 } 100 101 /** 102 * Creates a holder for a pre-parsed value read by the transport. 103 * 104 * @param marshaller The {@link Metadata.BinaryStreamMarshaller} associated with this value. 105 * @param value The value to store. 106 * @return an object holding the pre-parsed value for this key. 107 */ 108 @Internal parsedValue(BinaryStreamMarshaller<T> marshaller, T value)109 public static <T> Object parsedValue(BinaryStreamMarshaller<T> marshaller, T value) { 110 return new Metadata.LazyValue<>(marshaller, value); 111 } 112 113 /** 114 * Creates a new {@link Metadata} instance from serialized data, 115 * with some values pre-parsed. Metadata will mutate the passed in array. 116 * 117 * @param usedNames The number of names used. 118 * @param namesAndValues An array of interleaved names and values, 119 * with each name (at even indices) represented as a byte array, 120 * and each value (at odd indices) represented as either a byte 121 * array or an object returned by the {@link #parsedValue} 122 * method. 123 */ 124 @Internal newMetadataWithParsedValues(int usedNames, Object[] namesAndValues)125 public static Metadata newMetadataWithParsedValues(int usedNames, Object[] namesAndValues) { 126 return new Metadata(usedNames, namesAndValues); 127 } 128 } 129