Java Code Examples for io.netty.util.AsciiString#length()
The following examples show how to use
io.netty.util.AsciiString#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: GrpcHttp2HeadersUtils.java From grpc-nebula-java with Apache License 2.0 | 6 votes |
@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: HttpHeaderNames.java From armeria with Apache License 2.0 | 6 votes |
private static String malformedHeaderNameMessage(AsciiString name) { final StringBuilder buf = new StringBuilder(IntMath.saturatedAdd(name.length(), 64)); buf.append("malformed header name: "); final int nameLength = name.length(); for (int i = 0; i < nameLength; i++) { final char ch = name.charAt(i); if (PROHIBITED_NAME_CHARS.get(ch)) { buf.append(PROHIBITED_NAME_CHAR_NAMES[ch]); } else { buf.append(ch); } } return buf.toString(); }
Example 3
Source File: GrpcHttp2HeadersUtils.java From grpc-java with Apache License 2.0 | 6 votes |
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 4
Source File: GrpcHttp2HeadersUtils.java From grpc-nebula-java with Apache License 2.0 | 5 votes |
protected AsciiString validateName(AsciiString str) { int offset = str.arrayOffset(); int length = str.length(); final byte[] data = str.array(); for (int i = offset; i < offset + length; i++) { if (isUpperCase(data[i])) { PlatformDependent.throwException(connectionError(PROTOCOL_ERROR, "invalid header name '%s'", str)); } } return str; }
Example 5
Source File: HttpConversionUtil.java From netty-4.1.22 with Apache License 2.0 | 5 votes |
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 6
Source File: Http2Headers.java From netty-4.1.22 with Apache License 2.0 | 5 votes |
/** * Indicates whether the specified header follows the pseudo-header format (begins with ':' character) * * @return {@code true} if the header follow the pseudo-header format */ public static boolean hasPseudoHeaderFormat(CharSequence headerName) { if (headerName instanceof AsciiString) { final AsciiString asciiHeaderName = (AsciiString) headerName; return asciiHeaderName.length() > 0 && asciiHeaderName.byteAt(0) == PSEUDO_HEADER_PREFIX_BYTE; } else { return headerName.length() > 0 && headerName.charAt(0) == PSEUDO_HEADER_PREFIX; } }
Example 7
Source File: ByteBufUtil.java From netty-4.1.22 with Apache License 2.0 | 5 votes |
/** * Copies the content of {@code src} to a {@link ByteBuf} using {@link ByteBuf#writeBytes(byte[], int, int)}.使用ByteBuf将src的内容复制到ByteBuf。writeBytes(byte[],int,int)。 * * @param src the source string to copy * @param srcIdx the starting offset of characters to copy * @param dst the destination buffer * @param length the number of characters to copy */ public static void copy(AsciiString src, int srcIdx, ByteBuf dst, int length) { if (isOutOfBounds(srcIdx, length, src.length())) { throw new IndexOutOfBoundsException("expected: " + "0 <= srcIdx(" + srcIdx + ") <= srcIdx + length(" + length + ") <= srcLen(" + src.length() + ')'); } checkNotNull(dst, "dst").writeBytes(src.array(), srcIdx + src.arrayOffset(), length); }
Example 8
Source File: ArmeriaHttpUtil.java From armeria with Apache License 2.0 | 5 votes |
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 9
Source File: GrpcHttp2HeadersUtils.java From grpc-java with Apache License 2.0 | 5 votes |
protected AsciiString validateName(AsciiString str) { int offset = str.arrayOffset(); int length = str.length(); final byte[] data = str.array(); for (int i = offset; i < offset + length; i++) { if (isUpperCase(data[i])) { PlatformDependent.throwException(connectionError(PROTOCOL_ERROR, "invalid header name '%s'", str)); } } return str; }
Example 10
Source File: HttpConversionUtil.java From netty-4.1.22 with Apache License 2.0 | 4 votes |
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 11
Source File: AbstractProtocolEncoder.java From spring-boot-protocol with Apache License 2.0 | 4 votes |
@Override public void encode(ChannelHandlerContext ctx, T packet, ByteBuf out) throws Exception { int packetLength = fixedLength; //(2 byte Unsigned) mak total length int writerTotalLengthIndex = out.writerIndex(); out.writerIndex(writerTotalLengthIndex + CHAR_LENGTH); //(1 byte Unsigned) packet type out.writeByte(packet.getPacketType()); //(1 byte) packet ack flag out.writeByte(packet.getAck()); //(8 byte) protocol head out.writeBytes(versionBytes); //Fields Map<AsciiString, ByteBuf> fieldMap = packet.getFieldMap(); int fieldSize = fieldMap == null ? 0 : fieldMap.size(); //(1 byte Unsigned) Fields size out.writeByte(fieldSize); if (fieldSize > 0) { packetLength += fieldSize * 3; for (Map.Entry<AsciiString, ByteBuf> entry : fieldMap.entrySet()) { AsciiString key = entry.getKey(); ByteBuf value = entry.getValue(); //(key.length byte Unsigned) Fields size packetLength += key.length(); out.writeByte(key.length()); ByteBufUtil.writeAscii(out,key); //(value.length byte Unsigned) Fields size packetLength += value.readableBytes(); out.writeChar(value.readableBytes()); out.writeBytes(value); } } //Body ByteBuf body = packet.getBody(); if (body.readableBytes() > 0) { packetLength += body.readableBytes(); out.writeBytes(body); } //Fill total length out.setChar(writerTotalLengthIndex, packetLength); //retain // out.retain(); }
Example 12
Source File: ByteBufUtil.java From netty-4.1.22 with Apache License 2.0 | 3 votes |
/** * Copies the content of {@code src} to a {@link ByteBuf} using {@link ByteBuf#setBytes(int, byte[], int, int)}. * Unlike the {@link #copy(AsciiString, ByteBuf)} and {@link #copy(AsciiString, int, ByteBuf, int)} methods, * this method do not increase a {@code writerIndex} of {@code dst} buffer. * * @param src the source string to copy * @param srcIdx the starting offset of characters to copy * @param dst the destination buffer * @param dstIdx the starting offset in the destination buffer * @param length the number of characters to copy * 使用ByteBuf将src的内容复制到ByteBuf。setBytes(int,byte[],int,int)。与复制(AsciiString、ByteBuf)和复制(AsciiString、int、ByteBuf、int)方法不同,该方法不增加dst缓冲区的writerIndex。 */ public static void copy(AsciiString src, int srcIdx, ByteBuf dst, int dstIdx, int length) { if (isOutOfBounds(srcIdx, length, src.length())) { throw new IndexOutOfBoundsException("expected: " + "0 <= srcIdx(" + srcIdx + ") <= srcIdx + length(" + length + ") <= srcLen(" + src.length() + ')'); } checkNotNull(dst, "dst").setBytes(dstIdx, src.array(), srcIdx + src.arrayOffset(), length); }