List of usage examples for io.netty.buffer ByteBuf writerIndex
public abstract int writerIndex();
From source file:com.lambdaworks.redis.codec.StringCodec.java
License:Apache License
/** * Compatibility implementation.// w w w.ja v a2 s .c om * * @param key * @return */ private ByteBuffer encodeAndAllocateBuffer(String key) { if (key == null) { return ByteBuffer.wrap(EMPTY); } CharsetEncoder encoder = CharsetUtil.encoder(charset); ByteBuffer buffer = ByteBuffer.allocate((int) (encoder.maxBytesPerChar() * key.length())); ByteBuf byteBuf = Unpooled.wrappedBuffer(buffer); byteBuf.clear(); encode(key, byteBuf); buffer.limit(byteBuf.writerIndex()); return buffer; }
From source file:com.larskroll.common.RAFileRef.java
License:Open Source License
void copyTo(io.netty.buffer.ByteBuf buffer, long start, int length) { try {/*from ww w .jav a2s .c o m*/ int written = -1; int startx = buffer.writerIndex(); /* Note: there's a weird bug here, causing the buffer to sometimes * read nothing, although there's enough to read. * As a workaround: Keep trying until we get the desired amount of data */ while (written < length) { raf.seek(start); buffer.writerIndex(startx); FileChannel source = raf.getChannel(); written = buffer.writeBytes(source, length); // if (written != length) { // System.err.println("Read from " + raf + " of size " + raf.length() // + " starting at " + start + " leaving " + (raf.length()-start) // + " to read " + length); // throw new RuntimeException("Buffer didn't write required bytes!"); // } } } catch (IOException ex) { throw new RuntimeException(ex); } }
From source file:com.linecorp.armeria.server.grpc.ArmeriaServerCall.java
License:Apache License
private static void encodeHeader(CharSequence name, CharSequence value, ByteBuf buf) { final int nameLen = name.length(); final int valueLen = value.length(); final int entryLen = nameLen + valueLen + 4; buf.ensureWritable(entryLen);/*from ww w.ja va2s.c om*/ int offset = buf.writerIndex(); writeAscii(buf, offset, name, nameLen); offset += nameLen; buf.setByte(offset++, ':'); buf.setByte(offset++, ' '); writeAscii(buf, offset, value, valueLen); offset += valueLen; buf.setByte(offset++, '\r'); buf.setByte(offset++, '\n'); buf.writerIndex(offset); }
From source file:com.mastfrog.acteur.Application.java
License:Open Source License
/** * Create a 404 response/* w w w.ja va 2s. co m*/ * * @param event * @return */ protected HttpResponse createNotFoundResponse(Event<?> event) { ByteBuf buf = Unpooled .copiedBuffer("<html><head>" + "<title>Not Found</title></head><body><h1>Not Found</h1>" + event + " was not found\n<body></html>\n", charset); DefaultFullHttpResponse resp = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.NOT_FOUND, buf); Headers.write(Headers.CONTENT_TYPE, MediaType.HTML_UTF_8.withCharset(charset), resp); Headers.write(Headers.CONTENT_LENGTH, (long) buf.writerIndex(), resp); Headers.write(Headers.CONTENT_LANGUAGE, Locale.ENGLISH, resp); Headers.write(Headers.CACHE_CONTROL, new CacheControl(CacheControlTypes.no_cache), resp); Headers.write(Headers.DATE, new DateTime(), resp); if (debug) { String pth = event instanceof HttpEvent ? ((HttpEvent) event).getPath().toString() : ""; Headers.write(Headers.custom("X-Req-Path"), pth, resp); } return resp; }
From source file:com.necla.simba.server.gateway.server.frontend.FrontendFrameDecoder.java
License:Apache License
private ByteBuf decompress(ChannelHandlerContext ctx, ByteBuf frame) throws Exception { int readableBytes = frame.readableBytes(); if (frame.hasArray()) { inflater.setInput(frame.array(), 0, readableBytes); } else {//from ww w . j a v a2 s . co m byte[] array = new byte[frame.readableBytes()]; frame.getBytes(0, array); inflater.setInput(array); } int totalLength = 0; List<ByteBuf> all = new LinkedList<ByteBuf>(); int multiplier = 2; alldone: while (true) { int maxOutputLength = inflater.getRemaining() * multiplier; // multiplier keeps increasing, so we will keep picking // larger and larger buffers the more times we have to loop // around, i.e., the more we realize that the data was very // heavily compressed, the larger our buffers are going to be. multiplier += 1; ByteBuf decompressed = ctx.alloc().heapBuffer(maxOutputLength); while (!inflater.needsInput()) { byte[] outArray = decompressed.array(); int outIndex = decompressed.arrayOffset() + decompressed.writerIndex(); int length = outArray.length - outIndex; if (length == 0) break; try { //LOG.debug("here1"); int outputLength = inflater.inflate(outArray, outIndex, length); totalLength += outputLength; //LOG.debug("here2"); if (outputLength > 0) decompressed.writerIndex(decompressed.writerIndex() + outputLength); } catch (DataFormatException e) { throw new Exception("Could not inflate" + e.getMessage()); } if (inflater.finished()) { all.add(decompressed); break alldone; } } all.add(decompressed); } inflater.reset(); if (all.size() == 1) return all.get(0); else { ByteBuf allData = ctx.alloc().heapBuffer(totalLength); for (ByteBuf b : all) { //LOG.debug("capacity=" + allData.capacity()); allData.writeBytes(b); b.release(); } return allData; } }
From source file:com.necla.simba.server.gateway.server.frontend.FrontendFrameEncoder.java
License:Apache License
private void compress(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) { LOG.debug("compress here"); byte[] inAry = new byte[msg.readableBytes()]; msg.readBytes(inAry);// ww w.j ava2s .c o m int sizeEstimate = (int) Math.ceil(inAry.length * 1.001) + 12 + 4; LOG.debug("compress here2"); out.ensureWritable(sizeEstimate); int beginIndex = out.writerIndex(); out.writerIndex(beginIndex + 4); try { deflater.setInput(inAry); while (!deflater.needsInput()) { LOG.debug("compress here3333"); int numBytes = deflater.deflate(encodeBuf, 0, encodeBuf.length); LOG.debug("Compressed numBytes=" + numBytes); out.writeBytes(encodeBuf, 0, numBytes); LOG.debug("compress here4"); } deflater.finish(); while (!deflater.finished()) { int numBytes = deflater.deflate(encodeBuf, 0, encodeBuf.length); out.writeBytes(encodeBuf, 0, numBytes); LOG.debug("compress here5"); } deflater.reset(); int len = out.writerIndex() - beginIndex - 4; Stats.sent(out.writerIndex() + beginIndex + 4); LOG.debug("Compressed len=" + len); len |= (1 << 30); out.setInt(beginIndex, len); } catch (Exception e) { LOG.debug("Exception" + e); } }
From source file:com.netflix.iep.http.NetflixJsonObjectDecoder.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { if (state == ST_CORRUPTED) { in.skipBytes(in.readableBytes()); return;/* ww w .j a v a 2 s . c o m*/ } if (LOGGER.isTraceEnabled()) { byte[] bytes = new byte[in.readableBytes()]; in.getBytes(in.readerIndex(), bytes, 0, in.readableBytes()); LOGGER.trace("starting [" + in.readerIndex() + ":" + in.readableBytes() + "]:" + new String(bytes)); } // index of next byte to process. int len = this.len; int wrtIdx = in.writerIndex(); for (; in.readerIndex() + len < wrtIdx; len++) { if (len > maxObjectLength) { // buffer size exceeded maxObjectLength; discarding the complete buffer. in.skipBytes(in.readableBytes()); reset(); throw new TooLongFrameException( "object length exceeds " + maxObjectLength + ": " + len + " bytes discarded"); } byte c = in.getByte(in.readerIndex() + len); if (state == ST_DECODING_NORMAL) { decodeByte(c, in, in.readerIndex() + len); // All opening braces/brackets have been closed. That's enough to conclude // that the JSON object/array is complete. if (openBraces == 0) { ByteBuf json = extractObject(ctx, in, in.readerIndex(), len + 1); if (json != null) { out.add(json); } // The JSON object/array was extracted => discard the bytes from // the input buffer. in.readerIndex(in.readerIndex() + len + 1); len = 0; // Reset the object state to get ready for the next JSON object/text // coming along the byte stream. reset(); break; } } else if (state == ST_DECODING_ARRAY_STREAM) { if (len == 0 && Character.isWhitespace(c)) { in.skipBytes(1); len--; } decodeByte(c, in, in.readerIndex() + len); if (!insideString && (openBraces == 1 && c == ',' || openBraces == 0 && c == ']')) { ByteBuf json = extractObject(ctx, in, in.readerIndex(), len); if (json != null) { out.add(json); } in.readerIndex(in.readerIndex() + len + 1); len = 0; if (c == ']') { reset(); } break; } // JSON object/array detected. Accumulate bytes until all braces/brackets are closed. } else if (c == '{' || c == '[') { initDecoding(c); if (state == ST_DECODING_ARRAY_STREAM) { // Discard the array bracket in.skipBytes(1); len--; } // Discard leading spaces in front of a JSON object/array. } else if (Character.isWhitespace(c)) { in.skipBytes(1); len--; } else { state = ST_CORRUPTED; throw new CorruptedFrameException("invalid JSON received at byte position " + (in.readerIndex() + len) + ": " + ByteBufUtil.hexDump(in)); } } this.len = len; if (LOGGER.isTraceEnabled()) { byte[] bytes = new byte[in.readableBytes()]; in.getBytes(in.readerIndex(), bytes, 0, in.readableBytes()); LOGGER.trace("remainder [" + in.readerIndex() + ":" + in.readableBytes() + "]:" + new String(bytes)); } }
From source file:com.ning.http.client.providers.netty_4.NettyAsyncHttpProvider.java
License:Apache License
private static HttpRequest construct(AsyncHttpClientConfig config, Request request, HttpMethod m, URI uri, ByteBuf buffer, ProxyServer proxyServer) throws IOException { String host = AsyncHttpProviderUtils.getHost(uri); boolean webSocket = isWebSocket(uri); if (request.getVirtualHost() != null) { host = request.getVirtualHost(); }/*from w ww . j ava2 s .co m*/ FullHttpRequest nettyRequest; if (m.equals(HttpMethod.CONNECT)) { nettyRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_0, m, AsyncHttpProviderUtils.getAuthority(uri)); } else { String path = null; if (proxyServer != null && !(isSecure(uri) && config.isUseRelativeURIsWithSSLProxies())) path = uri.toString(); else if (uri.getRawQuery() != null) path = uri.getRawPath() + "?" + uri.getRawQuery(); else path = uri.getRawPath(); nettyRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, m, path); } if (webSocket) { nettyRequest.headers().add(HttpHeaders.Names.UPGRADE, HttpHeaders.Values.WEBSOCKET); nettyRequest.headers().add(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.UPGRADE); nettyRequest.headers().add("Origin", "http://" + uri.getHost() + ":" + (uri.getPort() == -1 ? isSecure(uri.getScheme()) ? 443 : 80 : uri.getPort())); nettyRequest.headers().add(WEBSOCKET_KEY, WebSocketUtil.getKey()); nettyRequest.headers().add("Sec-WebSocket-Version", "13"); } if (host != null) { if (uri.getPort() == -1) { nettyRequest.headers().set(HttpHeaders.Names.HOST, host); } else if (request.getVirtualHost() != null) { nettyRequest.headers().set(HttpHeaders.Names.HOST, host); } else { nettyRequest.headers().set(HttpHeaders.Names.HOST, host + ":" + uri.getPort()); } } else { host = "127.0.0.1"; } if (!m.equals(HttpMethod.CONNECT)) { FluentCaseInsensitiveStringsMap h = request.getHeaders(); if (h != null) { for (String name : h.keySet()) { if (!"host".equalsIgnoreCase(name)) { for (String value : h.get(name)) { nettyRequest.headers().add(name, value); } } } } if (config.isCompressionEnabled()) { nettyRequest.headers().set(HttpHeaders.Names.ACCEPT_ENCODING, HttpHeaders.Values.GZIP); } } else { List<String> auth = request.getHeaders().get(HttpHeaders.Names.PROXY_AUTHORIZATION); if (isNonEmpty(auth) && auth.get(0).startsWith("NTLM")) { nettyRequest.headers().add(HttpHeaders.Names.PROXY_AUTHORIZATION, auth.get(0)); } } Realm realm = request.getRealm() != null ? request.getRealm() : config.getRealm(); if (realm != null && realm.getUsePreemptiveAuth()) { String domain = realm.getNtlmDomain(); if (proxyServer != null && proxyServer.getNtlmDomain() != null) { domain = proxyServer.getNtlmDomain(); } String authHost = realm.getNtlmHost(); if (proxyServer != null && proxyServer.getHost() != null) { host = proxyServer.getHost(); } switch (realm.getAuthScheme()) { case BASIC: nettyRequest.headers().set(HttpHeaders.Names.AUTHORIZATION, AuthenticatorUtils.computeBasicAuthentication(realm)); break; case DIGEST: if (isNonEmpty(realm.getNonce())) { try { nettyRequest.headers().set(HttpHeaders.Names.AUTHORIZATION, AuthenticatorUtils.computeDigestAuthentication(realm)); } catch (NoSuchAlgorithmException e) { throw new SecurityException(e); } } break; case NTLM: try { String msg = ntlmEngine.generateType1Msg("NTLM " + domain, authHost); nettyRequest.headers().set(HttpHeaders.Names.AUTHORIZATION, "NTLM " + msg); } catch (NTLMEngineException e) { IOException ie = new IOException(); ie.initCause(e); throw ie; } break; case KERBEROS: case SPNEGO: String challengeHeader = null; String server = proxyServer == null ? host : proxyServer.getHost(); try { challengeHeader = getSpnegoEngine().generateToken(server); } catch (Throwable e) { IOException ie = new IOException(); ie.initCause(e); throw ie; } nettyRequest.headers().set(HttpHeaders.Names.AUTHORIZATION, "Negotiate " + challengeHeader); break; case NONE: break; default: throw new IllegalStateException("Invalid Authentication " + realm); } } if (!webSocket && !request.getHeaders().containsKey(HttpHeaders.Names.CONNECTION)) { nettyRequest.headers().set(HttpHeaders.Names.CONNECTION, AsyncHttpProviderUtils.keepAliveHeaderValue(config)); } if (proxyServer != null) { if (!request.getHeaders().containsKey("Proxy-Connection")) { nettyRequest.headers().set("Proxy-Connection", AsyncHttpProviderUtils.keepAliveHeaderValue(config)); } if (proxyServer.getPrincipal() != null) { if (isNonEmpty(proxyServer.getNtlmDomain())) { List<String> auth = request.getHeaders().get(HttpHeaders.Names.PROXY_AUTHORIZATION); if (!(isNonEmpty(auth) && auth.get(0).startsWith("NTLM"))) { try { String msg = ntlmEngine.generateType1Msg(proxyServer.getNtlmDomain(), proxyServer.getHost()); nettyRequest.headers().set(HttpHeaders.Names.PROXY_AUTHORIZATION, "NTLM " + msg); } catch (NTLMEngineException e) { IOException ie = new IOException(); ie.initCause(e); throw ie; } } } else { nettyRequest.headers().set(HttpHeaders.Names.PROXY_AUTHORIZATION, AuthenticatorUtils.computeBasicAuthentication(proxyServer)); } } } // Add default accept headers. if (request.getHeaders().getFirstValue("Accept") == null) { nettyRequest.headers().set(HttpHeaders.Names.ACCEPT, "*/*"); } if (request.getHeaders().getFirstValue("User-Agent") != null) { nettyRequest.headers().set("User-Agent", request.getHeaders().getFirstValue("User-Agent")); } else if (config.getUserAgent() != null) { nettyRequest.headers().set("User-Agent", config.getUserAgent()); } else { nettyRequest.headers().set("User-Agent", AsyncHttpProviderUtils.constructUserAgent(NettyAsyncHttpProvider.class, config)); } if (!m.equals(HttpMethod.CONNECT)) { if (isNonEmpty(request.getCookies())) { CookieEncoder httpCookieEncoder = new CookieEncoder(false); Iterator<Cookie> ic = request.getCookies().iterator(); Cookie c; org.jboss.netty.handler.codec.http.Cookie cookie; while (ic.hasNext()) { c = ic.next(); cookie = new DefaultCookie(c.getName(), c.getValue()); cookie.setPath(c.getPath()); cookie.setMaxAge(c.getMaxAge()); cookie.setDomain(c.getDomain()); httpCookieEncoder.addCookie(cookie); } nettyRequest.headers().set(HttpHeaders.Names.COOKIE, httpCookieEncoder.encode()); } String reqType = request.getMethod(); if (!"HEAD".equals(reqType) && !"OPTION".equals(reqType) && !"TRACE".equals(reqType)) { String bodyCharset = request.getBodyEncoding() == null ? DEFAULT_CHARSET : request.getBodyEncoding(); // We already have processed the body. if (buffer != null && buffer.writerIndex() != 0) { nettyRequest.headers().set(HttpHeaders.Names.CONTENT_LENGTH, buffer.writerIndex()); nettyRequest.setContent(buffer); } else if (request.getByteData() != null) { nettyRequest.headers().set(HttpHeaders.Names.CONTENT_LENGTH, String.valueOf(request.getByteData().length)); nettyRequest.setContent(Unpooled.wrappedBuffer(request.getByteData())); } else if (request.getStringData() != null) { nettyRequest.headers().set(HttpHeaders.Names.CONTENT_LENGTH, String.valueOf(request.getStringData().getBytes(bodyCharset).length)); nettyRequest.setContent(Unpooled.wrappedBuffer(request.getStringData().getBytes(bodyCharset))); } else if (request.getStreamData() != null) { int[] lengthWrapper = new int[1]; byte[] bytes = AsyncHttpProviderUtils.readFully(request.getStreamData(), lengthWrapper); int length = lengthWrapper[0]; nettyRequest.headers().set(HttpHeaders.Names.CONTENT_LENGTH, String.valueOf(length)); nettyRequest.setContent(Unpooled.wrappedBuffer(bytes, 0, length)); } else if (isNonEmpty(request.getParams())) { StringBuilder sb = new StringBuilder(); for (final Entry<String, List<String>> paramEntry : request.getParams()) { final String key = paramEntry.getKey(); for (final String value : paramEntry.getValue()) { if (sb.length() > 0) { sb.append("&"); } UTF8UrlEncoder.appendEncoded(sb, key); sb.append("="); UTF8UrlEncoder.appendEncoded(sb, value); } } nettyRequest.headers().set(HttpHeaders.Names.CONTENT_LENGTH, String.valueOf(sb.length())); nettyRequest.setContent(Unpooled.wrappedBuffer(sb.toString().getBytes(bodyCharset))); if (!request.getHeaders().containsKey(HttpHeaders.Names.CONTENT_TYPE)) { nettyRequest.headers().set(HttpHeaders.Names.CONTENT_TYPE, "application/x-www-form-urlencoded"); } } else if (request.getParts() != null) { int lenght = computeAndSetContentLength(request, nettyRequest); if (lenght == -1) { lenght = MAX_BUFFERED_BYTES; } MultipartRequestEntity mre = AsyncHttpProviderUtils .createMultipartRequestEntity(request.getParts(), request.getParams()); nettyRequest.headers().set(HttpHeaders.Names.CONTENT_TYPE, mre.getContentType()); nettyRequest.headers().set(HttpHeaders.Names.CONTENT_LENGTH, String.valueOf(mre.getContentLength())); /** * TODO: AHC-78: SSL + zero copy isn't supported by the MultiPart class and pretty complex to implements. */ if (isSecure(uri)) { ByteBuf b = Unpooled.buffer(lenght); mre.writeRequest(new ByteBufOutputStream(b)); nettyRequest.setContent(b); } } else if (request.getEntityWriter() != null) { int lenght = computeAndSetContentLength(request, nettyRequest); if (lenght == -1) { lenght = MAX_BUFFERED_BYTES; } ByteBuf b = Unpooled.buffer(lenght); request.getEntityWriter().writeEntity(new ByteBufOutputStream(b)); nettyRequest.headers().set(HttpHeaders.Names.CONTENT_LENGTH, b.writerIndex()); nettyRequest.setContent(b); } else if (request.getFile() != null) { File file = request.getFile(); if (!file.isFile()) { throw new IOException( String.format("File %s is not a file or doesn't exist", file.getAbsolutePath())); } nettyRequest.headers().set(HttpHeaders.Names.CONTENT_LENGTH, file.length()); } } } return nettyRequest; }
From source file:com.ogarproject.ogar.server.net.packet.outbound.PacketOutUpdateNodes.java
License:Open Source License
@Override public void writeData(ByteBuf buf) { // Removals by eating int lengthIndex = buf.writerIndex(); int eaten = 0; buf.writerIndex(lengthIndex + 2);/* ww w . j av a 2s . c o m*/ for (EntityImpl entity : removals) { if (entity.getConsumer() > 0) { eaten++; buf.writeInt(entity.getConsumer()); buf.writeInt(entity.getID()); } } buf.markWriterIndex(); buf.writerIndex(lengthIndex); buf.writeShort(eaten); buf.resetWriterIndex(); // Updates for (int id : updates) { EntityImpl entity = world.getEntity(id); if (entity == null) { // TODO - Theoretically this could be ignored, but it might cause other issues, // like having nonexistent entities on the player's screen. Re-evaluate this later? throw new MalformedPacketException("Attempted to update nonexistent entity"); } buf.writeInt(entity.getID()); buf.writeInt((int) entity.getPosition().getX()); buf.writeInt((int) entity.getPosition().getY()); buf.writeShort(entity.getPhysicalSize()); buf.writeByte(entity.getColor().getRed()); buf.writeByte(entity.getColor().getGreen()); buf.writeByte(entity.getColor().getBlue()); buf.writeBoolean(entity.isSpiked()); // buf.skipBytes(18); if (entity instanceof CellImpl) { CellImpl cell = (CellImpl) entity; if (cell.getName() == null) { writeUTF16(buf, ""); } else { writeUTF16(buf, cell.getName()); } } else { writeUTF16(buf, ""); } } buf.writeInt(0); // General removals buf.writeInt(removals.size()); for (EntityImpl entity : removals) { buf.writeInt(entity.getID()); } }
From source file:com.quavo.osrs.network.protocol.codec.game.GamePacketEncoder.java
License:Open Source License
@Override protected void encode(ChannelHandlerContext ctx, GamePacketResponse msg, ByteBuf out) throws Exception { PacketEncoder<PacketContext> packet = msg.getPacket(); PacketType type = packet.getPacket().getType(); ByteBuf buffer = packet.getBuilder().getBuffer(); out.writeByte(packet.getPacket().getId()/* + encoder.nextInt() */); if (type == PacketType.VARIABLE_BYTE) { out.writeByte(buffer.writerIndex()); } else if (type == PacketType.VARIABLE_SHORT) { out.writeShort(buffer.writerIndex()); }/* w ww . j a v a 2s.c o m*/ out.writeBytes(buffer); }