Refine search
private static long contentLength(Object msg) { if (msg instanceof HttpContent) { return ((HttpContent) msg).content().readableBytes(); } if (msg instanceof ByteBuf) { return ((ByteBuf) msg).readableBytes(); } if (msg instanceof FileRegion) { return ((FileRegion) msg).count(); } throw new IllegalStateException("unexpected message type: " + StringUtil.simpleClassName(msg)); }
/** * Converts the specified byte array into a hexadecimal value and appends it to the specified buffer. */ public static <T extends Appendable> T toHexString(T dst, byte[] src) { return toHexString(dst, src, 0, src.length); }
/** * Decode a 2-digit hex byte from within a string. */ public static byte decodeHexByte(CharSequence s, int pos) { return StringUtil.decodeHexByte(s, pos); }
while (in.isReadable()) { int outSize = out.size(); if (ctx.isRemoved()) { break; int oldInputLength = in.readableBytes(); decodeRemovalReentryProtection(ctx, in, out); if (ctx.isRemoved()) { break; if (oldInputLength == in.readableBytes()) { break; } else { if (oldInputLength == in.readableBytes()) { throw new DecoderException( StringUtil.simpleClassName(getClass()) + ".decode() did not read anything but decoded a message.");
if (msg instanceof HttpMessage) { if (state != ST_INIT) { throw new IllegalStateException("unexpected message type: " + StringUtil.simpleClassName(msg) + ", state: " + state); H m = (H) msg; buf = ctx.alloc().buffer((int) headersEncodedSizeAccumulator); ByteBufUtil.writeShortBE(buf, CRLF_SHORT); headersEncodedSizeAccumulator = HEADERS_WEIGHT_NEW * padSizeForAccumulation(buf.readableBytes()) + HEADERS_WEIGHT_HISTORICAL * headersEncodedSizeAccumulator; if (!potentialEmptyBuf.isReadable()) { out.add(potentialEmptyBuf.retain()); return; switch (state) { case ST_INIT: throw new IllegalStateException("unexpected message type: " + StringUtil.simpleClassName(msg)); case ST_CONTENT_NON_CHUNK: final long contentLength = contentLength(msg); if (buf != null && buf.writableBytes() >= contentLength && msg instanceof HttpContent) {
@Override protected void doWrite(ChannelOutboundBuffer in) throws Exception { for (;;) { Object msg = in.current(); if (msg == null) { // nothing left to write break; } if (msg instanceof ByteBuf) { ByteBuf buf = (ByteBuf) msg; int readableBytes = buf.readableBytes(); while (readableBytes > 0) { doWriteBytes(buf); int newReadableBytes = buf.readableBytes(); in.progress(readableBytes - newReadableBytes); readableBytes = newReadableBytes; } in.remove(); } else if (msg instanceof FileRegion) { FileRegion region = (FileRegion) msg; long transferred = region.transferred(); doWriteFileRegion(region); in.progress(region.transferred() - transferred); in.remove(); } else { in.remove(new UnsupportedOperationException( "unsupported message type: " + StringUtil.simpleClassName(msg))); } } }
/** * Determine the content length of the given object. * * @param msg the object to determine the length of. * @return the determined content length. */ private static int contentLength(Object msg) { if (msg instanceof MemcacheContent) { return ((MemcacheContent) msg).content().readableBytes(); } if (msg instanceof ByteBuf) { return ((ByteBuf) msg).readableBytes(); } if (msg instanceof FileRegion) { return (int) ((FileRegion) msg).count(); } throw new IllegalStateException("unexpected message type: " + StringUtil.simpleClassName(msg)); }
@Override public String toString() { final StringBuilder buf = new StringBuilder(64).append(StringUtil.simpleClassName(this)).append('('); final DnsRecordType type = type(); if (type != DnsRecordType.OPT) { buf.append(name().isEmpty()? "<root>" : name()) .append(' ') .append(timeToLive()) .append(' '); DnsMessageUtil.appendRecordClass(buf, dnsClass()) .append(' ') .append(type.name()); } else { buf.append("OPT flags:") .append(timeToLive()) .append(" udp:") .append(dnsClass()); } buf.append(' ') .append(content().readableBytes()) .append("B)"); return buf.toString(); } }
private static Object encodeAndRetain(Object msg) { if (msg instanceof ByteBuf) { return ((ByteBuf) msg).retain(); } if (msg instanceof HttpContent) { return ((HttpContent) msg).content().retain(); } if (msg instanceof FileRegion) { return ((FileRegion) msg).retain(); } throw new IllegalStateException("unexpected message type: " + StringUtil.simpleClassName(msg)); }
@Override public String toString() { return StringUtil.simpleClassName(this) + '(' + buf.toString() + ')'; }
/** * Encode the content, depending on the object type. * * @param msg the object to encode. * @return the encoded object. */ private static Object encodeAndRetain(Object msg) { if (msg instanceof ByteBuf) { return ((ByteBuf) msg).retain(); } if (msg instanceof MemcacheContent) { return ((MemcacheContent) msg).content().retain(); } if (msg instanceof FileRegion) { return ((FileRegion) msg).retain(); } throw new IllegalStateException("unexpected message type: " + StringUtil.simpleClassName(msg)); }
@Override protected final Object filterOutboundMessage(Object msg) throws Exception { if (msg instanceof SctpMessage) { SctpMessage m = (SctpMessage) msg; ByteBuf buf = m.content(); if (buf.isDirect() && buf.nioBufferCount() == 1) { return m; } return new SctpMessage(m.protocolIdentifier(), m.streamIdentifier(), m.isUnordered(), newDirectBuffer(m, buf)); } throw new UnsupportedOperationException( "unsupported message type: " + StringUtil.simpleClassName(msg) + " (expected: " + StringUtil.simpleClassName(SctpMessage.class)); }
@Override protected final Object filterOutboundMessage(Object msg) { if (msg instanceof ByteBuf) { ByteBuf buf = (ByteBuf) msg; if (buf.isDirect()) { return msg; } return newDirectBuffer(buf); } if (msg instanceof FileRegion) { return msg; } throw new UnsupportedOperationException( "unsupported message type: " + StringUtil.simpleClassName(msg) + EXPECTED_TYPES); }
@Override public String toString() { return StringUtil.simpleClassName(this) + "(default: " + defaultValue + ", map: " + map + ')'; } }
/** * Converts String to Double. * * @param value * The String to be converted. * @return The converted Double value. */ public static Double stringToDouble(String val) { String v = trim(val); if (StringUtil.isNullOrEmpty(v)) { return null; } else { return Double.valueOf(v); } }
/** * Generate a HTTP/2 {code :path} from a URI in accordance with * <a href="https://tools.ietf.org/html/rfc7230#section-5.3">rfc7230, 5.3</a>. */ private static String toHttp2Path(URI uri) { final StringBuilder pathBuilder = new StringBuilder( length(uri.getRawPath()) + length(uri.getRawQuery()) + length(uri.getRawFragment()) + 2); if (!isNullOrEmpty(uri.getRawPath())) { pathBuilder.append(uri.getRawPath()); } if (!isNullOrEmpty(uri.getRawQuery())) { pathBuilder.append('?'); pathBuilder.append(uri.getRawQuery()); } if (!isNullOrEmpty(uri.getRawFragment())) { pathBuilder.append('#'); pathBuilder.append(uri.getRawFragment()); } return pathBuilder.length() != 0 ? pathBuilder.toString() : EMPTY_REQUEST_PATH; }
/** * Decodes part of a string generated by {@link #hexDump(byte[])} */ public static byte[] decodeHexDump(CharSequence hexDump, int fromIndex, int length) { return StringUtil.decodeHexDump(hexDump, fromIndex, length); }
/** * Decode a 2-digit hex byte from within a string. */ public static byte decodeHexByte(CharSequence s, int pos) { int hi = decodeHexNibble(s.charAt(pos)); int lo = decodeHexNibble(s.charAt(pos + 1)); if (hi == -1 || lo == -1) { throw new IllegalArgumentException(String.format( "invalid hex byte '%s' at index %d of '%s'", s.subSequence(pos, pos + 2), pos, s)); } return (byte) ((hi << 4) + lo); }
if (ctx.isRemoved()) { break; int oldInputLength = in.readableBytes(); try { decodeRemovalReentryProtection(ctx, replayable, out); if (ctx.isRemoved()) { break; if (oldInputLength == in.readableBytes() && oldState == state) { throw new DecoderException( StringUtil.simpleClassName(getClass()) + ".decode() must consume the inbound " + "data or change its state if it did not decode anything."); } else { if (ctx.isRemoved()) { break; if (oldReaderIndex == in.readerIndex() && oldState == state) { throw new DecoderException( StringUtil.simpleClassName(getClass()) + ".decode() method must consume the inbound data " + "or change its state if it decoded something.");
@Override protected void doWrite(ChannelOutboundBuffer in) throws Exception { for (;;) { Object msg = in.current(); if (msg == null) { // nothing left to write break; } if (msg instanceof ByteBuf) { ByteBuf buf = (ByteBuf) msg; int readableBytes = buf.readableBytes(); while (readableBytes > 0) { doWriteBytes(buf); int newReadableBytes = buf.readableBytes(); in.progress(readableBytes - newReadableBytes); readableBytes = newReadableBytes; } in.remove(); } else if (msg instanceof FileRegion) { FileRegion region = (FileRegion) msg; long transferred = region.transferred(); doWriteFileRegion(region); in.progress(region.transferred() - transferred); in.remove(); } else { in.remove(new UnsupportedOperationException( "unsupported message type: " + StringUtil.simpleClassName(msg))); } } }