Java Code Examples for io.netty.util.AsciiString#INDEX_NOT_FOUND

The following examples show how to use io.netty.util.AsciiString#INDEX_NOT_FOUND . 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: GrpcHttp2HeadersUtils.java    From grpc-nebula-java with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("BetaApi") // BaseEncoding is stable in Guava 20.0
protected Http2Headers add(AsciiString name, AsciiString value) {
  byte[] nameBytes = bytes(name);
  byte[] valueBytes;
  if (!name.endsWith(binaryHeaderSuffix)) {
    valueBytes = bytes(value);
    addHeader(value, nameBytes, valueBytes);
    return this;
  }
  int startPos = 0;
  int endPos = -1;
  while (endPos < value.length()) {
    int indexOfComma = value.indexOf(',', startPos);
    endPos = indexOfComma == AsciiString.INDEX_NOT_FOUND ? value.length() : indexOfComma;
    AsciiString curVal = value.subSequence(startPos, endPos, false);
    valueBytes = BaseEncoding.base64().decode(curVal);
    startPos = indexOfComma + 1;
    addHeader(curVal, nameBytes, valueBytes);
  }
  return this;
}
 
Example 2
Source File: GrpcHttp2HeadersUtils.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
protected Http2Headers add(AsciiString name, AsciiString value) {
  byte[] nameBytes = bytes(name);
  byte[] valueBytes;
  if (!name.endsWith(binaryHeaderSuffix)) {
    valueBytes = bytes(value);
    addHeader(value, nameBytes, valueBytes);
    return this;
  }
  int startPos = 0;
  int endPos = -1;
  while (endPos < value.length()) {
    int indexOfComma = value.indexOf(',', startPos);
    endPos = indexOfComma == AsciiString.INDEX_NOT_FOUND ? value.length() : indexOfComma;
    AsciiString curVal = value.subSequence(startPos, endPos, false);
    valueBytes = BaseEncoding.base64().decode(curVal);
    startPos = indexOfComma + 1;
    addHeader(curVal, nameBytes, valueBytes);
  }
  return this;
}
 
Example 3
Source File: HttpUtil.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
/**
 * Fetch charset from Content-Type header value as a char sequence.
 *
 * A lot of sites/possibly clients have charset="CHARSET", for example charset="utf-8". Or "utf8" instead of "utf-8"
 * This is not according to standard, but this method provide an ability to catch desired mistakes manually in code
 *
 * @param contentTypeValue Content-Type header value to parse
 * @return the {@code CharSequence} with charset from message's Content-Type header
 * or {@code null} if charset is not presented
 * @throws NullPointerException in case if {@code contentTypeValue == null}
 * 将charset从Content-Type头值中提取为char序列。很多站点/可能客户都有charset=" charset ",例如charset="utf-8"。或者“utf8”而不是“utf-8”这不是标准的,但是这种方法提供了在代码中手动捕获所需错误的能力
 */
public static CharSequence getCharsetAsSequence(CharSequence contentTypeValue) {
    if (contentTypeValue == null) {
        throw new NullPointerException("contentTypeValue");
    }
    int indexOfCharset = AsciiString.indexOfIgnoreCaseAscii(contentTypeValue, CHARSET_EQUALS, 0);
    if (indexOfCharset != AsciiString.INDEX_NOT_FOUND) {
        int indexOfEncoding = indexOfCharset + CHARSET_EQUALS.length();
        if (indexOfEncoding < contentTypeValue.length()) {
            return contentTypeValue.subSequence(indexOfEncoding, contentTypeValue.length());
        }
    }
    return null;
}
 
Example 4
Source File: HttpUtil.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
/**
 * Fetch MIME type part from Content-Type header value as a char sequence.从Content-Type头值中提取MIME类型部分作为char序列。
 *
 * @param contentTypeValue Content-Type header value to parse
 * @return the MIME type as a {@code CharSequence} from message's Content-Type header
 * or {@code null} if content-type header or MIME type part of this header are not presented
 * <p/>
 * "content-type: text/html; charset=utf-8" - "text/html" will be returned <br/>
 * "content-type: text/html" - "text/html" will be returned <br/>
 * "content-type: empty header - {@code null} we be returned
 * @throws NullPointerException in case if {@code contentTypeValue == null}
 */
public static CharSequence getMimeType(CharSequence contentTypeValue) {
    if (contentTypeValue == null) {
        throw new NullPointerException("contentTypeValue");
    }

    int indexOfSemicolon = AsciiString.indexOfIgnoreCaseAscii(contentTypeValue, SEMICOLON, 0);
    if (indexOfSemicolon != AsciiString.INDEX_NOT_FOUND) {
        return contentTypeValue.subSequence(0, indexOfSemicolon);
    } else {
        return contentTypeValue.length() > 0 ? contentTypeValue : null;
    }
}