List of usage examples for io.netty.buffer ByteBuf readBytes
public abstract ByteBuf readBytes(ByteBuffer dst);
From source file:com.ibasco.agql.protocols.valve.source.query.SourcePacketBuilder.java
License:Open Source License
/** * Convert a source packet instance to it's byte representation * * @param packet The {@link SourceServerPacket} to convert * * @return Returns the deconstructed packet in byte array form *//*from w w w . ja v a2 s .c om*/ @Override public byte[] deconstruct(SourceServerPacket packet) { if (packet == null) throw new IllegalArgumentException("Invalid packet specified in the arguments."); byte[] payload = packet.getPayload(); byte[] protocolHeader = packet.getProtocolHeader(); byte[] packetHeader = packet.getPacketHeader(); int payloadSize = payload != null ? payload.length : 0; int protocolHeaderSize = protocolHeader != null ? protocolHeader.length : 0; int packetHeaderSize = packetHeader != null ? packetHeader.length : 0; int packetSize = protocolHeaderSize + packetHeaderSize + payloadSize; //Allocate our buffer final ByteBuf buf = createBuffer(packetSize); byte[] data; try { //Include Protocol Header if available if (protocolHeaderSize > 0) buf.writeBytes(protocolHeader); //Include Packet Header if (packetHeaderSize > 0) buf.writeBytes(packetHeader); //Include Payload (if available) if (payloadSize > 0) buf.writeBytes(payload); //Store the buffer into a byte array data = new byte[buf.readableBytes()]; if (data.length > 0) { buf.readBytes(data); } } finally { buf.release(); } log.debug("Constructing '{}' with total of {} bytes of data", packet.getClass().getSimpleName(), data.length); //Return the backing array representation return data; }
From source file:com.ibasco.agql.protocols.valve.source.query.SourceRconPacketBuilder.java
License:Open Source License
@Override public byte[] deconstruct(SourceRconPacket packet) { //1) size = int (4 bytes) //2) id = int (4 bytes) //3) type = int (4 bytes) //4) body = string (length + 1 null byte) //5) trailer = null byte int id = packet.getId(); int type = packet.getType(); final String body = StringUtils.defaultString(packet.getBody()); int packetSize = 10 + body.length(); final ByteBuf buf = createBuffer(packetSize); byte[] data;//from w w w .j a v a 2s. co m try { buf.writeIntLE(packetSize); buf.writeIntLE(id); buf.writeIntLE(type); buf.writeBytes(body.getBytes()); buf.writeByte(0); buf.writeByte(0); data = new byte[buf.readableBytes()]; buf.readBytes(data); } finally { buf.release(); } return data; }
From source file:com.ibasco.agql.protocols.valve.steam.master.MasterServerPacketBuilder.java
License:Open Source License
@Override public <T extends MasterServerPacket> T construct(ByteBuf data) { //Mark Index/*from w ww. j a v a 2 s .c om*/ data.markReaderIndex(); try { //Reset the index data.readerIndex(0); //Verify size if (data.readableBytes() <= 6) throw new IllegalStateException( "Cannot continue processing buffer with less than or equal to 6 bytes"); //Read header byte[] header = new byte[6]; data.readBytes(header); //Read payload byte[] payload = new byte[data.readableBytes()]; data.readBytes(payload); //Verify if packet header is valid MasterServerResponsePacket packet = new MasterServerResponsePacket(); packet.setHeader(header); packet.setPayload(payload); return (T) packet; } finally { data.resetReaderIndex(); } }
From source file:com.ibasco.agql.protocols.valve.steam.master.MasterServerPacketBuilder.java
License:Open Source License
@Override public byte[] deconstruct(MasterServerPacket packet) { if (packet == null) throw new IllegalArgumentException("Invalid packet specified in the arguments."); byte[] payload = packet.getPayload(); byte[] packetHeader = packet.getPacketHeader(); int payloadSize = payload != null ? payload.length : 0; int packetHeaderSize = packetHeader != null ? packetHeader.length : 0; int packetSize = packetHeaderSize + payloadSize; //Allocate our buffer final ByteBuf buf = createBuffer(packetSize); byte[] data;/*from w w w . j a v a 2 s .c o m*/ try { //Include Packet Header if (packetHeaderSize > 0) buf.writeBytes(packetHeader); //Include Payload (if available) if (payloadSize > 0) buf.writeBytes(payload); //Store the buffer into a byte array data = new byte[buf.readableBytes()]; if (data.length > 0) { buf.readBytes(data); } } finally { buf.release(); } log.debug("Constructing '{}' with total of {} bytes of data", packet.getClass().getSimpleName(), data.length); //Return the backing array representation return data; }
From source file:com.ibasco.agql.protocols.valve.steam.master.packets.MasterServerRequestPacket.java
License:Open Source License
@Override public byte[] getPayload() { String filterString = this.filter.toString(); int payloadSize = (3 + filterString.length() + (this.startIp.length())); final ByteBuf payload = PooledByteBufAllocator.DEFAULT.buffer(payloadSize); try {/* w w w .ja v a2 s . co m*/ payload.writeByte(getRegion()); payload.writeBytes(getStartIp().getBytes()); payload.writeByte(0); //terminating byte payload.writeBytes(filterString.getBytes()); byte[] payloadBytes = new byte[payload.readableBytes()]; payload.readBytes(payloadBytes); return payloadBytes; } finally { payload.release(); } }
From source file:com.ibm.crail.namenode.rpc.netty.common.NettyRequest.java
License:Apache License
public void update(ByteBuf buffer) throws IOException { this.cookie = buffer.readLong(); this.cmd = buffer.readShort(); this.type = buffer.readShort(); nioBuffer.clear();//ww w.j a va 2s. c om buffer.readBytes(nioBuffer); nioBuffer.flip(); switch (type) { case NameNodeProtocol.REQ_CREATE_FILE: this.createFileReq = new RpcRequestMessage.CreateFileReq(); createFileReq.update(nioBuffer); break; case NameNodeProtocol.REQ_GET_FILE: this.fileReq = new RpcRequestMessage.GetFileReq(); fileReq.update(nioBuffer); break; case NameNodeProtocol.REQ_SET_FILE: this.setFileReq = new RpcRequestMessage.SetFileReq(); setFileReq.update(nioBuffer); break; case NameNodeProtocol.REQ_REMOVE_FILE: this.removeReq = new RpcRequestMessage.RemoveFileReq(); removeReq.update(nioBuffer); break; case NameNodeProtocol.REQ_RENAME_FILE: this.renameFileReq = new RpcRequestMessage.RenameFileReq(); renameFileReq.update(nioBuffer); break; case NameNodeProtocol.REQ_GET_BLOCK: this.getBlockReq = new RpcRequestMessage.GetBlockReq(); getBlockReq.update(nioBuffer); break; case NameNodeProtocol.REQ_GET_LOCATION: this.getLocationReq = new RpcRequestMessage.GetLocationReq(); getLocationReq.update(nioBuffer); break; case NameNodeProtocol.REQ_SET_BLOCK: this.setBlockReq = new RpcRequestMessage.SetBlockReq(); setBlockReq.update(nioBuffer); break; case NameNodeProtocol.REQ_GET_DATANODE: this.getDataNodeReq = new RpcRequestMessage.GetDataNodeReq(); getDataNodeReq.update(nioBuffer); break; case NameNodeProtocol.REQ_DUMP_NAMENODE: this.dumpNameNodeReq = new RpcRequestMessage.DumpNameNodeReq(); dumpNameNodeReq.update(nioBuffer); break; case NameNodeProtocol.REQ_PING_NAMENODE: this.pingNameNodeReq = new RpcRequestMessage.PingNameNodeReq(); pingNameNodeReq.update(nioBuffer); break; } }
From source file:com.ibm.crail.namenode.rpc.netty.common.NettyResponse.java
License:Apache License
public void update(long cookie, ByteBuf buffer) { assert this.cookie == cookie; this.type = buffer.readShort(); this.error = buffer.readShort(); nioBuffer.clear();//w w w . j a v a2 s. c o m buffer.readBytes(nioBuffer); nioBuffer.flip(); switch (type) { case NameNodeProtocol.RES_VOID: //this.voidRes = new RpcResponseMessage.VoidRes(); voidRes.update(nioBuffer); voidRes.setError(error); break; case NameNodeProtocol.RES_CREATE_FILE: //this.createFileRes = new RpcResponseMessage.CreateFileRes(); createFileRes.update(nioBuffer); createFileRes.setError(error); break; case NameNodeProtocol.RES_GET_FILE: //this.getFileRes = new RpcResponseMessage.GetFileRes(); getFileRes.update(nioBuffer); getFileRes.setError(error); break; case NameNodeProtocol.RES_DELETE_FILE: //this.delFileRes = new RpcResponseMessage.DeleteFileRes(); delFileRes.update(nioBuffer); delFileRes.setError(error); break; case NameNodeProtocol.RES_RENAME_FILE: //this.renameRes = new RpcResponseMessage.RenameRes(); renameRes.update(nioBuffer); renameRes.setError(error); break; case NameNodeProtocol.RES_GET_BLOCK: //this.getBlockRes = new RpcResponseMessage.GetBlockRes(); getBlockRes.update(nioBuffer); getBlockRes.setError(error); break; case NameNodeProtocol.RES_GET_LOCATION: //this.getLocationRes = new RpcResponseMessage.GetLocationRes(); getLocationRes.update(nioBuffer); getLocationRes.setError(error); break; case NameNodeProtocol.RES_GET_DATANODE: //this.getDataNodeRes = new RpcResponseMessage.GetDataNodeRes(); getDataNodeRes.update(nioBuffer); getDataNodeRes.setError(error); break; case NameNodeProtocol.RES_PING_NAMENODE: //this.pingNameNodeRes = new RpcResponseMessage.PingNameNodeRes(); pingNameNodeRes.update(nioBuffer); pingNameNodeRes.setError(error); break; } }
From source file:com.ibm.mqlight.api.security.PemFile.java
License:Apache License
/** * Obtains the private key data as a byte array from the PEM file. * <p>// www. j a va 2 s . co m * Within the PEM file the private key data is base 64 encoded. This method will decode the data for the returned private key * data. * <p> * Note that for an encrypted private key the data will remain encrypted. * * @return The private key data. * @throws KeyException If a private key cannot be found in the PEM file. * @throws IOException If the PEM file cannot be read for any reason. */ public byte[] getPrivateKeyBytes() throws KeyException, IOException { final String methodName = "getPrivateKeyBytes"; logger.entry(this, methodName); final String fileData = getPemFileData(); Matcher m = KEY_PATTERN.matcher(fileData); final byte[] keyBytes; final String base64KeyDataStr; if (m.find()) { base64KeyDataStr = m.group(1); } else { m = ENCRYPTED_KEY_PATTERN.matcher(fileData); if (m.find()) { base64KeyDataStr = m.group(1); } else { final KeyException exception = new KeyException("Private key not found in PEM file: " + pemFile); logger.throwing(this, methodName, exception); throw exception; } } final ByteBuf base64KeyData = Unpooled.copiedBuffer(base64KeyDataStr, Charset.forName("US-ASCII")); final ByteBuf keyData = Base64.decode(base64KeyData); base64KeyData.release(); keyBytes = new byte[keyData.readableBytes()]; keyData.readBytes(keyBytes).release(); logger.exit(this, methodName, keyBytes); return keyBytes; }
From source file:com.informatica.surf.sources.http.HttpListener.java
License:Apache License
public byte[] nextMessage() throws InterruptedException { _logger.debug("Checking queued messages"); ByteBuf buf = _messageQueue.take(); byte b[] = new byte[buf.readableBytes()]; _logger.debug("Readable bytes = {}", b.length); buf.readBytes(b); buf.release();//from w ww . jav a 2 s . c om return b; }
From source file:com.intuit.karate.netty.FeatureServerHandler.java
License:Open Source License
@Override protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest msg) { long startTime = System.currentTimeMillis(); backend.getContext().logger.debug("handling method: {}, uri: {}", msg.method(), msg.uri()); FullHttpResponse nettyResponse;//from w ww . java 2 s. c o m if (msg.uri().startsWith(STOP_URI)) { backend.getContext().logger.info("stop uri invoked, shutting down"); ByteBuf responseBuf = Unpooled.copiedBuffer("stopped", CharsetUtil.UTF_8); nettyResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, responseBuf); stopFunction.run(); } else { StringUtils.Pair url = HttpUtils.parseUriIntoUrlBaseAndPath(msg.uri()); HttpRequest request = new HttpRequest(); if (url.left == null) { String requestScheme = ssl ? "https" : "http"; String host = msg.headers().get(HttpUtils.HEADER_HOST); request.setUrlBase(requestScheme + "://" + host); } else { request.setUrlBase(url.left); } request.setUri(url.right); request.setMethod(msg.method().name()); msg.headers().forEach(h -> request.addHeader(h.getKey(), h.getValue())); QueryStringDecoder decoder = new QueryStringDecoder(url.right); decoder.parameters().forEach((k, v) -> request.putParam(k, v)); HttpContent httpContent = (HttpContent) msg; ByteBuf content = httpContent.content(); if (content.isReadable()) { byte[] bytes = new byte[content.readableBytes()]; content.readBytes(bytes); request.setBody(bytes); } HttpResponse response = backend.buildResponse(request, startTime); HttpResponseStatus httpResponseStatus = HttpResponseStatus.valueOf(response.getStatus()); byte[] responseBody = response.getBody(); if (responseBody != null) { ByteBuf responseBuf = Unpooled.copiedBuffer(responseBody); nettyResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, httpResponseStatus, responseBuf); } else { nettyResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, httpResponseStatus); } MultiValuedMap karateHeaders = response.getHeaders(); if (karateHeaders != null) { HttpHeaders nettyHeaders = nettyResponse.headers(); karateHeaders.forEach((k, v) -> nettyHeaders.add(k, v)); } } ctx.write(nettyResponse); ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE); }