Java Code Examples for io.netty.util.internal.AppendableCharSequence#length()

The following examples show how to use io.netty.util.internal.AppendableCharSequence#length() . 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: HttpObjectDecoder.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
private void splitHeader(AppendableCharSequence sb) {
    final int length = sb.length();
    int nameStart;
    int nameEnd;
    int colonEnd;
    int valueStart;
    int valueEnd;

    nameStart = findNonWhitespace(sb, 0);
    for (nameEnd = nameStart; nameEnd < length; nameEnd ++) {
        char ch = sb.charAt(nameEnd);
        if (ch == ':' || Character.isWhitespace(ch)) {
            break;
        }
    }

    for (colonEnd = nameEnd; colonEnd < length; colonEnd ++) {
        if (sb.charAt(colonEnd) == ':') {
            colonEnd ++;
            break;
        }
    }

    name = sb.subStringUnsafe(nameStart, nameEnd);
    valueStart = findNonWhitespace(sb, colonEnd);
    if (valueStart == length) {
        value = EMPTY_VALUE;
    } else {
        valueEnd = findEndOfString(sb);
        value = sb.subStringUnsafe(valueStart, valueEnd);
    }
}
 
Example 2
Source File: HttpObjectDecoder.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
private static int findNonWhitespace(AppendableCharSequence sb, int offset) {
    for (int result = offset; result < sb.length(); ++result) {
        if (!Character.isWhitespace(sb.charAtUnsafe(result))) {
            return result;
        }
    }
    return sb.length();
}
 
Example 3
Source File: HttpObjectDecoder.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
private static int findWhitespace(AppendableCharSequence sb, int offset) {
    for (int result = offset; result < sb.length(); ++result) {
        if (Character.isWhitespace(sb.charAtUnsafe(result))) {
            return result;
        }
    }
    return sb.length();
}
 
Example 4
Source File: HttpObjectDecoder.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
private static int findEndOfString(AppendableCharSequence sb) {
    for (int result = sb.length() - 1; result > 0; --result) {
        if (!Character.isWhitespace(sb.charAtUnsafe(result))) {
            return result + 1;
        }
    }
    return 0;
}
 
Example 5
Source File: MqttOverWebsocketProtocol.java    From joyqueue with Apache License 2.0 5 votes vote down vote up
private static int findNonWhitespace(AppendableCharSequence sb, int offset) {
    for (int result = offset; result < sb.length(); ++result) {
        if (!Character.isWhitespace(sb.charAtUnsafe(result))) {
            return result;
        }
    }
    return sb.length();
}
 
Example 6
Source File: MqttOverWebsocketProtocol.java    From joyqueue with Apache License 2.0 5 votes vote down vote up
private static int findWhitespace(AppendableCharSequence sb, int offset) {
    for (int result = offset; result < sb.length(); ++result) {
        if (Character.isWhitespace(sb.charAtUnsafe(result))) {
            return result;
        }
    }
    return sb.length();
}
 
Example 7
Source File: MqttOverWebsocketProtocol.java    From joyqueue with Apache License 2.0 5 votes vote down vote up
private static int findEndOfString(AppendableCharSequence sb) {
    for (int result = sb.length() - 1; result > 0; --result) {
        if (!Character.isWhitespace(sb.charAtUnsafe(result))) {
            return result + 1;
        }
    }
    return 0;
}
 
Example 8
Source File: HttpObjectDecoder.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
private void splitHeader(AppendableCharSequence sb) {
    final int length = sb.length();
    int nameStart;
    int nameEnd;
    int colonEnd;
    int valueStart;
    int valueEnd;

    nameStart = findNonWhitespace(sb, 0);
    for (nameEnd = nameStart; nameEnd < length; nameEnd ++) {
        char ch = sb.charAt(nameEnd);
        if (ch == ':' || Character.isWhitespace(ch)) {
            break;
        }
    }

    for (colonEnd = nameEnd; colonEnd < length; colonEnd ++) {
        if (sb.charAt(colonEnd) == ':') {
            colonEnd ++;
            break;
        }
    }

    name = sb.substring(nameStart, nameEnd);
    valueStart = findNonWhitespace(sb, colonEnd);
    if (valueStart == length) {
        value = EMPTY_VALUE;
    } else {
        valueEnd = findEndOfString(sb);
        value = sb.substring(valueStart, valueEnd);
    }
}
 
Example 9
Source File: HttpObjectDecoder.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
private State readHeaders(ByteBuf buffer) {
        final HttpMessage message = this.message;
        final HttpHeaders headers = message.headers();

        AppendableCharSequence line = headerParser.parse(buffer);
        if (line == null) {
            return null;
        }
        if (line.length() > 0) {
            do {
                char firstChar = line.charAt(0);
                if (name != null && (firstChar == ' ' || firstChar == '\t')) {
                    //please do not make one line from below code
                    //as it breaks +XX:OptimizeStringConcat optimization//请不要从下面的代码中写一行
//当它中断时+XX:OptimizeStringConcat优化
                    String trimmedLine = line.toString().trim();
                    String valueStr = String.valueOf(value);
                    value = valueStr + ' ' + trimmedLine;
                } else {
                    if (name != null) {
                        headers.add(name, value);
                    }
                    splitHeader(line);
                }

                line = headerParser.parse(buffer);
                if (line == null) {
                    return null;
                }
            } while (line.length() > 0);
        }

        // Add the last header.
        if (name != null) {
            headers.add(name, value);
        }
        // reset name and value fields
        name = null;
        value = null;

        State nextState;

        if (isContentAlwaysEmpty(message)) {
            HttpUtil.setTransferEncodingChunked(message, false);
            nextState = State.SKIP_CONTROL_CHARS;
        } else if (HttpUtil.isTransferEncodingChunked(message)) {
            nextState = State.READ_CHUNK_SIZE;
        } else if (contentLength() >= 0) {
            nextState = State.READ_FIXED_LENGTH_CONTENT;
        } else {
            nextState = State.READ_VARIABLE_LENGTH_CONTENT;
        }
        return nextState;
    }
 
Example 10
Source File: HttpObjectDecoder.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
private LastHttpContent readTrailingHeaders(ByteBuf buffer) {
        AppendableCharSequence line = headerParser.parse(buffer);
        if (line == null) {
            return null;
        }
        CharSequence lastHeader = null;
        if (line.length() > 0) {
            LastHttpContent trailer = this.trailer;
            if (trailer == null) {
                trailer = this.trailer = new DefaultLastHttpContent(Unpooled.EMPTY_BUFFER, validateHeaders);
            }
            do {
                char firstChar = line.charAt(0);
                if (lastHeader != null && (firstChar == ' ' || firstChar == '\t')) {
                    List<String> current = trailer.trailingHeaders().getAll(lastHeader);
                    if (!current.isEmpty()) {
                        int lastPos = current.size() - 1;
                        //please do not make one line from below code
                        //as it breaks +XX:OptimizeStringConcat optimization//请不要从下面的代码中写一行
//当它中断时+XX:OptimizeStringConcat优化
                        String lineTrimmed = line.toString().trim();
                        String currentLastPos = current.get(lastPos);
                        current.set(lastPos, currentLastPos + lineTrimmed);
                    }
                } else {
                    splitHeader(line);
                    CharSequence headerName = name;
                    if (!HttpHeaderNames.CONTENT_LENGTH.contentEqualsIgnoreCase(headerName) &&
                        !HttpHeaderNames.TRANSFER_ENCODING.contentEqualsIgnoreCase(headerName) &&
                        !HttpHeaderNames.TRAILER.contentEqualsIgnoreCase(headerName)) {
                        trailer.trailingHeaders().add(headerName, value);
                    }
                    lastHeader = name;
                    // reset name and value fields
                    name = null;
                    value = null;
                }

                line = headerParser.parse(buffer);
                if (line == null) {
                    return null;
                }
            } while (line.length() > 0);

            this.trailer = null;
            return trailer;
        }

        return LastHttpContent.EMPTY_LAST_CONTENT;
    }
 
Example 11
Source File: HttpObjectDecoder.java    From netty4.0.27Learn with Apache License 2.0 4 votes vote down vote up
private State readHeaders(ByteBuf buffer) {
    final HttpMessage message = this.message;
    final HttpHeaders headers = message.headers();

    AppendableCharSequence line = headerParser.parse(buffer);
    if (line == null) {
        return null;
    }
    if (line.length() > 0) {
        do {
            char firstChar = line.charAt(0);
            if (name != null && (firstChar == ' ' || firstChar == '\t')) {
                StringBuilder buf = new StringBuilder(value.length() + line.length() + 1);
                buf.append(value)
                   .append(' ')
                   .append(line.toString().trim());
                value = buf.toString();
            } else {
                if (name != null) {
                    headers.add(name, value);
                }
                splitHeader(line);
            }

            line = headerParser.parse(buffer);
            if (line == null) {
                return null;
            }
        } while (line.length() > 0);
    }

    // Add the last header.
    if (name != null) {
        headers.add(name, value);
    }
    // reset name and value fields
    name = null;
    value = null;

    State nextState;

    if (isContentAlwaysEmpty(message)) {
        HttpHeaders.removeTransferEncodingChunked(message);
        nextState = State.SKIP_CONTROL_CHARS;
    } else if (HttpHeaders.isTransferEncodingChunked(message)) {
        nextState = State.READ_CHUNK_SIZE;
    } else if (contentLength() >= 0) {
        nextState = State.READ_FIXED_LENGTH_CONTENT;
    } else {
        nextState = State.READ_VARIABLE_LENGTH_CONTENT;
    }
    return nextState;
}
 
Example 12
Source File: HttpObjectDecoder.java    From netty4.0.27Learn with Apache License 2.0 4 votes vote down vote up
private LastHttpContent readTrailingHeaders(ByteBuf buffer) {
    AppendableCharSequence line = headerParser.parse(buffer);
    if (line == null) {
        return null;
    }
    CharSequence lastHeader = null;
    if (line.length() > 0) {
        LastHttpContent trailer = this.trailer;
        if (trailer == null) {
            trailer = this.trailer = new DefaultLastHttpContent(Unpooled.EMPTY_BUFFER, validateHeaders);
        }
        do {
            char firstChar = line.charAt(0);
            if (lastHeader != null && (firstChar == ' ' || firstChar == '\t')) {
                List<String> current = trailer.trailingHeaders().getAll(lastHeader);
                if (!current.isEmpty()) {
                    int lastPos = current.size() - 1;
                    String lineTrimmed = line.toString().trim();
                    CharSequence currentLastPos = current.get(lastPos);
                    StringBuilder b = new StringBuilder(currentLastPos.length() + lineTrimmed.length());
                    b.append(currentLastPos)
                     .append(lineTrimmed);
                    current.set(lastPos, b.toString());
                } else {
                    // Content-Length, Transfer-Encoding, or Trailer
                }
            } else {
                splitHeader(line);
                CharSequence headerName = name;
                if (!HttpHeaders.equalsIgnoreCase(HttpHeaders.Names.CONTENT_LENGTH, headerName) &&
                        !HttpHeaders.equalsIgnoreCase(HttpHeaders.Names.TRANSFER_ENCODING, headerName) &&
                        !HttpHeaders.equalsIgnoreCase(HttpHeaders.Names.TRAILER, headerName)) {
                    trailer.trailingHeaders().add(headerName, value);
                }
                lastHeader = name;
                // reset name and value fields
                name = null;
                value = null;
            }

            line = headerParser.parse(buffer);
            if (line == null) {
                return null;
            }
        } while (line.length() > 0);

        this.trailer = null;
        return trailer;
    }

    return LastHttpContent.EMPTY_LAST_CONTENT;
}