public ByteBuf encode()

in src/main/java/com/twitter/http2/HttpHeaderBlockEncoder.java [78:128]


    public ByteBuf encode(ChannelHandlerContext ctx, HttpHeaderBlockFrame frame) throws IOException {
        ByteBuf buf = Unpooled.buffer();
        ByteBufOutputStream out = new ByteBufOutputStream(buf);

        // The current allowable max header table size is the
        // minimum of the encoder and decoder allowable sizes
        int allowableHeaderTableSize = Math.min(encoderMaxHeaderTableSize, decoderMaxHeaderTableSize);

        // maxHeaderTableSize will hold the smallest size seen the
        // last call to encode. This might be smaller than the
        // current allowable max header table size
        if (maxHeaderTableSize < allowableHeaderTableSize) {
            encoder.setMaxHeaderTableSize(out, maxHeaderTableSize);
        }

        // Check if the current allowable size is equal to the encoder's
        // capacity and set the new size if necessary
        if (allowableHeaderTableSize != encoder.getMaxHeaderTableSize()) {
            encoder.setMaxHeaderTableSize(out, allowableHeaderTableSize);
        }

        // Store the current allowable size for the next call
        maxHeaderTableSize = allowableHeaderTableSize;

        // Now we can encode headers
        for (String name : frame.headers().names()) {
            if ("cookie".equalsIgnoreCase(name)) {
                // Sec. 8.1.3.4. Cookie Header Field
                for (String value : frame.headers().getAll(name)) {
                    for (String crumb : value.split(";")) {
                        byte[] valueBytes = crumb.trim().getBytes(StandardCharsets.UTF_8);
                        encoder.encodeHeader(out, COOKIE, valueBytes, true);
                    }
                }
            } else {
                byte[] nameBytes = name.toLowerCase(Locale.ENGLISH).getBytes(StandardCharsets.UTF_8);
                // Sec. 8.1.3.3. Header Field Ordering
                List<String> values = frame.headers().getAll(name);
                if (values.size() == 0) {
                    encoder.encodeHeader(out, nameBytes, EMPTY, false);
                } else {
                    for (String value : values) {
                        byte[] valueBytes = value.getBytes(StandardCharsets.UTF_8);
                        encoder.encodeHeader(out, nameBytes, valueBytes, false);
                    }
                }
            }
        }

        return buf;
    }