Java Code Examples for io.netty.util.AsciiString#forEachByte()

The following examples show how to use io.netty.util.AsciiString#forEachByte() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HpackHuffmanEncoder.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
/**
 * Compresses the input string literal using the Huffman coding.
 *
 * @param out the output stream for the compressed data
 * @param data the string literal to be Huffman encoded
 */
public void encode(ByteBuf out, CharSequence data) {
    ObjectUtil.checkNotNull(out, "out");
    if (data instanceof AsciiString) {
        AsciiString string = (AsciiString) data;
        try {
            encodeProcessor.out = out;
            string.forEachByte(encodeProcessor);
        } catch (Exception e) {
            PlatformDependent.throwException(e);
        } finally {
            encodeProcessor.end();
        }
    } else {
        encodeSlowPath(out, data);
    }
}
 
Example 2
Source File: HpackHuffmanEncoder.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the number of bytes required to Huffman encode the input string literal.
 *
 * @param data the string literal to be Huffman encoded
 * @return the number of bytes required to Huffman encode {@code data}
 */
int getEncodedLength(CharSequence data) {
    if (data instanceof AsciiString) {
        AsciiString string = (AsciiString) data;
        try {
            encodedLengthProcessor.reset();
            string.forEachByte(encodedLengthProcessor);
            return encodedLengthProcessor.length();
        } catch (Exception e) {
            PlatformDependent.throwException(e);
            return -1;
        }
    } else {
        return getEncodedLengthSlowPath(data);
    }
}
 
Example 3
Source File: HttpConversionUtil.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
private static CharSequenceMap<AsciiString> toLowercaseMap(Iterator<? extends CharSequence> valuesIter,
                                                           int arraySizeHint) {
    UnsupportedValueConverter<AsciiString> valueConverter = UnsupportedValueConverter.<AsciiString>instance();
    CharSequenceMap<AsciiString> result = new CharSequenceMap<AsciiString>(true, valueConverter, arraySizeHint);

    while (valuesIter.hasNext()) {
        AsciiString lowerCased = AsciiString.of(valuesIter.next()).toLowerCase();
        try {
            int index = lowerCased.forEachByte(FIND_COMMA);
            if (index != -1) {
                int start = 0;
                do {
                    result.add(lowerCased.subSequence(start, index, false).trim(), EMPTY_STRING);
                    start = index + 1;
                } while (start < lowerCased.length() &&
                         (index = lowerCased.forEachByte(start, lowerCased.length() - start, FIND_COMMA)) != -1);
                result.add(lowerCased.subSequence(start, lowerCased.length(), false).trim(), EMPTY_STRING);
            } else {
                result.add(lowerCased.trim(), EMPTY_STRING);
            }
        } catch (Exception e) {
            // This is not expect to happen because FIND_COMMA never throws but must be caught
            // because of the ByteProcessor interface.
            throw new IllegalStateException(e);
        }
    }
    return result;
}
 
Example 4
Source File: ArmeriaHttpUtil.java    From armeria with Apache License 2.0 5 votes vote down vote up
private static CharSequenceMap toLowercaseMap(Iterator<? extends CharSequence> valuesIter,
                                              int arraySizeHint) {
    final CharSequenceMap result = new CharSequenceMap(arraySizeHint);

    while (valuesIter.hasNext()) {
        final AsciiString lowerCased = AsciiString.of(valuesIter.next()).toLowerCase();
        try {
            int index = lowerCased.forEachByte(FIND_COMMA);
            if (index != -1) {
                int start = 0;
                do {
                    result.add(lowerCased.subSequence(start, index, false).trim(), EMPTY_STRING);
                    start = index + 1;
                } while (start < lowerCased.length() &&
                         (index = lowerCased.forEachByte(start,
                                                         lowerCased.length() - start, FIND_COMMA)) != -1);
                result.add(lowerCased.subSequence(start, lowerCased.length(), false).trim(), EMPTY_STRING);
            } else {
                result.add(lowerCased.trim(), EMPTY_STRING);
            }
        } catch (Exception e) {
            // This is not expect to happen because FIND_COMMA never throws but must be caught
            // because of the ByteProcessor interface.
            throw new IllegalStateException(e);
        }
    }
    return result;
}
 
Example 5
Source File: HttpHeaderNames.java    From armeria with Apache License 2.0 5 votes vote down vote up
private static AsciiString validate(AsciiString name) {
    if (name.isEmpty()) {
        throw new IllegalArgumentException("malformed header name: <EMPTY>");
    }

    if (!Flags.validateHeaders()) {
        return name;
    }

    final int lastIndex;
    try {
        lastIndex = name.forEachByte(value -> {
            if (value > LAST_PROHIBITED_NAME_CHAR) {
                // Definitely valid.
                return true;
            }

            return !PROHIBITED_NAME_CHARS.get(value);
        });
    } catch (Exception e) {
        throw new Error(e);
    }

    if (lastIndex >= 0) {
        throw new IllegalArgumentException(malformedHeaderNameMessage(name));
    }

    return name;
}
 
Example 6
Source File: HttpConversionUtil.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
public static void toHttp2Headers(HttpHeaders inHeaders, Http2Headers out) {
    Iterator<Entry<CharSequence, CharSequence>> iter = inHeaders.iteratorCharSequence();
    // Choose 8 as a default size because it is unlikely we will see more than 4 Connection headers values, but
    // still allowing for "enough" space in the map to reduce the chance of hash code collision.
    CharSequenceMap<AsciiString> connectionBlacklist =
        toLowercaseMap(inHeaders.valueCharSequenceIterator(CONNECTION), 8);
    while (iter.hasNext()) {
        Entry<CharSequence, CharSequence> entry = iter.next();
        final AsciiString aName = AsciiString.of(entry.getKey()).toLowerCase();
        if (!HTTP_TO_HTTP2_HEADER_BLACKLIST.contains(aName) && !connectionBlacklist.contains(aName)) {
            // https://tools.ietf.org/html/rfc7540#section-8.1.2.2 makes a special exception for TE
            if (aName.contentEqualsIgnoreCase(TE)) {
                toHttp2HeadersFilterTE(entry, out);
            } else if (aName.contentEqualsIgnoreCase(COOKIE)) {
                AsciiString value = AsciiString.of(entry.getValue());
                // split up cookies to allow for better compression
                // https://tools.ietf.org/html/rfc7540#section-8.1.2.5
                try {
                    int index = value.forEachByte(FIND_SEMI_COLON);
                    if (index != -1) {
                        int start = 0;
                        do {
                            out.add(COOKIE, value.subSequence(start, index, false));
                            // skip 2 characters "; " (see https://tools.ietf.org/html/rfc6265#section-4.2.1)
                            start = index + 2;
                        } while (start < value.length() &&
                                (index = value.forEachByte(start, value.length() - start, FIND_SEMI_COLON)) != -1);
                        if (start >= value.length()) {
                            throw new IllegalArgumentException("cookie value is of unexpected format: " + value);
                        }
                        out.add(COOKIE, value.subSequence(start, value.length(), false));
                    } else {
                        out.add(COOKIE, value);
                    }
                } catch (Exception e) {
                    // This is not expect to happen because FIND_SEMI_COLON never throws but must be caught
                    // because of the ByteProcessor interface.
                    throw new IllegalStateException(e);
                }
            } else {
                out.add(aName, entry.getValue());
            }
        }
    }
}
 
Example 7
Source File: HttpResponseStatus.java    From netty-4.1.22 with Apache License 2.0 3 votes vote down vote up
/**
 * Parses the specified HTTP status line into a {@link HttpResponseStatus}. The expected formats of the line are:
 * <ul>
 * <li>{@code statusCode} (e.g. 200)</li>
 * <li>{@code statusCode} {@code reasonPhrase} (e.g. 404 Not Found)</li>
 * </ul>
 *
 * @throws IllegalArgumentException if the specified status line is malformed
 * 将指定的HTTP状态行解析为HttpResponseStatus。预期的行格式如下:
statusCode(例如:200)
statusCode推理短语(如404 Not Found)
 */
public static HttpResponseStatus parseLine(AsciiString line) {
    try {
        int space = line.forEachByte(FIND_ASCII_SPACE);
        return space == -1 ? valueOf(line.parseInt()) : valueOf(line.parseInt(0, space), line.toString(space + 1));
    } catch (Exception e) {
        throw new IllegalArgumentException("malformed status line: " + line, e);
    }
}