List of usage examples for io.netty.buffer ByteBuf getBytes
public abstract ByteBuf getBytes(int index, ByteBuffer dst);
From source file:org.thingsplode.synapse.proxy.handlers.HttpResponse2ResponseDecoder.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, HttpResponse httpResponse, List<Object> out) throws Exception { if (!httpResponse.decoderResult().isSuccess()) { logger.warn("HTTP Resonse decoding was not succesfull."); }/*from www . jav a 2s . co m*/ //todo: convert to commands too Response rsp = new Response(new Response.ResponseHeader(httpResponse.status())); rsp.getHeader().addAllProperties(httpResponse.headers().entries()); rsp.getHeader().setMsgId(httpResponse.headers().get(AbstractMessage.PROP_MESSAGE_ID)); rsp.getHeader().setCorrelationId(httpResponse.headers().get(AbstractMessage.PROP_CORRELATION_ID)); rsp.getHeader().setProtocolVersion(httpResponse.headers().get(AbstractMessage.PROP_PROTOCOL_VERSION)); rsp.getHeader().setContentType(new MediaType(httpResponse.headers().get(HttpHeaderNames.CONTENT_TYPE))); rsp.getHeader().setRemoteAddress(ctx.channel().remoteAddress()); if (httpResponse instanceof FullHttpResponse && marshalledBodyExpected(httpResponse.status())) { ByteBuf contentBuffer = ((FullHttpResponse) httpResponse).content(); byte[] dst = new byte[contentBuffer.capacity()]; contentBuffer.getBytes(0, dst); String jsonResponse = new String(dst, Charset.forName("UTF-8")); String bodyType = httpResponse.headers().get(AbstractMessage.PROP_BODY_TYPE); if (Util.notEmpty(bodyType)) { try { Class clz = Class.forName(bodyType); Object payload = EndpointProxy.SERIALIZATION_SERVICE.getSerializer(getMediaType(httpResponse)) .unMarshall(clz, jsonResponse); rsp.setBody(payload); } catch (ClassNotFoundException cnfe) { logger.warn("There's a body of type [" + bodyType + "] on the response, but the corresponding class cannot be found. Skipping the deserialization of the body."); } } else if (logger.isDebugEnabled() && Util.isEmpty(bodyType) && Util.notEmpty(jsonResponse) && !"{ }".equals(jsonResponse)) { logger.warn("Body serialization will be skipped due to missing body type info on the header."); } } out.add(rsp); }
From source file:org.tiger.netty.rpc.all.codec.NettyMessageDecoder.java
License:Apache License
private void print(ByteBuf sendBuf) { if (sendBuf == null) return;//from w w w .j a va 2 s.c o m /*while(sendBuf.isReadable()){ System.out.print(sendBuf.readByte()+" "); //System.out.flush(); }*/ //System.out.println("ascii>>"+sendBuf.toString(CharsetUtil.US_ASCII)); if (sendBuf != null && sendBuf.capacity() > 0) { byte[] bt = new byte[sendBuf.capacity()]; sendBuf.getBytes(0, bt); System.out.println("bt>" + Arrays.toString(bt)); } else { System.out.println("***>>" + sendBuf); } }
From source file:org.tiger.netty.rpc.all.codec.NettyMessageEncoder.java
License:Apache License
private void print(ByteBuf sendBuf) { byte[] bt = new byte[sendBuf.capacity()]; sendBuf.getBytes(0, bt); System.out.println("bt>" + Arrays.toString(bt)); }
From source file:org.waarp.common.digest.FilesystemBasedDigest.java
License:Open Source License
/** * Update the digest with new buffer// ww w . ja v a 2 s . c o m */ public void Update(ByteBuf buffer) { byte[] bytes = null; int start = 0; int length = buffer.readableBytes(); if (buffer.hasArray()) { start = buffer.arrayOffset(); bytes = buffer.array(); } else { if (reusableBytes == null || reusableBytes.length != length) { reusableBytes = new byte[length]; } bytes = reusableBytes; buffer.getBytes(buffer.readerIndex(), bytes); } Update(bytes, start, length); }
From source file:org.waarp.common.digest.FilesystemBasedDigest.java
License:Open Source License
/** * Get hash with given {@link ByteBuf} (from Netty) * /*from ww w . j a va 2s . c o m*/ * @param buffer * this buffer will not be changed * @param algo * @return the hash * @throws IOException */ public static byte[] getHash(ByteBuf buffer, DigestAlgo algo) throws IOException { Checksum checksum = null; byte[] bytes = null; int start = 0; int length = buffer.readableBytes(); if (buffer.hasArray()) { start = buffer.arrayOffset(); bytes = buffer.array(); } else { bytes = new byte[length]; buffer.getBytes(buffer.readerIndex(), bytes); } switch (algo) { case ADLER32: checksum = new Adler32(); case CRC32: if (checksum == null) { // not ADLER32 checksum = new CRC32(); } checksum.update(bytes, start, length); bytes = null; bytes = Long.toOctalString(checksum.getValue()).getBytes(UTF8); checksum = null; return bytes; case MD5: if (useFastMd5) { MD5 md5 = new MD5(); md5.Update(bytes, start, length); bytes = md5.Final(); md5 = null; return bytes; } case MD2: case SHA1: case SHA256: case SHA384: case SHA512: String algoname = algo.name; MessageDigest digest = null; try { digest = MessageDigest.getInstance(algoname); } catch (NoSuchAlgorithmException e) { throw new IOException(algoname + " Algorithm not supported by this JVM", e); } digest.update(bytes, start, length); bytes = digest.digest(); digest = null; return bytes; default: throw new IOException(algo.name + " Algorithm not supported by this JVM"); } }
From source file:org.waarp.common.digest.MD5.java
License:Open Source License
/** * Updates hash with given {@link ByteBuf} (from Netty) * // ww w . j a v a 2s. c om * @param buffer * ByteBuf to use for updating the hash and this buffer will not be changed */ public void Update(ByteBuf buffer) { byte[] bytes; int start = 0; int len = buffer.readableBytes(); if (buffer.hasArray()) { start = buffer.arrayOffset(); bytes = buffer.array(); } else { if (reusableBytes == null || reusableBytes.length != len) { reusableBytes = new byte[len]; } bytes = reusableBytes; buffer.getBytes(start, bytes); } Update(state, bytes, start, len); }
From source file:org.wso2.carbon.gateway.internal.mediation.camel.CarbonMessageTypeConverter.java
License:Open Source License
private byte[] toByteArray(ByteBuf buffer, Exchange exchange) { byte[] bytes = new byte[buffer.readableBytes()]; int readerIndex = buffer.readerIndex(); buffer.getBytes(readerIndex, bytes); return bytes; }
From source file:pl.tcs.btppllib.DeviceGetTimeTest.java
protected static ByteBuf buildGetTimeRequest() { log.debug("*** Build GetTime request"); ByteBuf retBuf = Unpooled.buffer(256); // preamble:/*from w w w . j a v a 2 s. c o m*/ retBuf.writeByte(0); retBuf.writeByte(0); retBuf.writeByte(0); retBuf.writeByte(0x12); // rest: retBuf.writeByte(toUnsigned(TG_HEADER_SZ)); retBuf.writeByte(toUnsigned(BtpplChecksumType.BtpplFletcherOnly.getVal())); final long utcTime = getUTCTime(); // retBuf.writeShort((int)utcTime); retBuf.writeShort(0x4397); retBuf.writeShort(subsecondDiscriminator); // subsecondDiscriminator++; retBuf.writeShort(0); // member retBuf.writeShort(815); // otype retBuf.writeShort(103); // method (103 = GetTime) retBuf.writeShort(1); retBuf.writeShort(100); // int writerIdx = retBuf.writerIndex(); // log.debug("@Writer index: " + writerIdx); int nb = retBuf.readableBytes() - 4; byte[] fletcherInput = new byte[nb]; retBuf.getBytes(4, fletcherInput); // for (int i = 0; i < fletcherInput.length; i++) { // fletcherInput[i] = (byte)(fletcherInput[i] & 0xFF); // } log.info("BUF: " + getBufStr(fletcherInput)); FletcherChecksum fcs = new FletcherChecksum(fletcherInput); // byte[] checksum = fcs.getChecksum(); // retBuf.writeByte(toUnsigned(checksum[0])); // retBuf.writeByte(toUnsigned(checksum[1])); int checksumInt = fcs.getChecksumAsInt(); log.info(String.format("********** 0x%x VS 0x%x", checksumInt, 0xe530)); retBuf.writeShort(checksumInt); // retBuf.writeShort(0xe530); log.debug("LogBuf: " + ByteBufUtil.prettyHexDump(retBuf)); return retBuf; }
From source file:pl.tcs.btppllib.telegrams.OcitRequestTelegram.java
public ByteBuf toByteBuf() { ByteBuf retBuf = Unpooled.buffer(); retBuf.writeByte(toUnsigned(getHdrLen())); retBuf.writeByte(toUnsigned(getOpts())); retBuf.writeShort((int) getUTCTime()); retBuf.writeShort(toUnsigned(getJobTimeCnt())); retBuf.writeShort(toUnsigned(getMember())); retBuf.writeShort(toUnsigned(getOtype())); retBuf.writeShort(toUnsigned(getMethod())); retBuf.writeShort(toUnsigned(getZnr())); retBuf.writeShort(toUnsigned(getFnr())); for (TypeDescription elem : pathElems) { writeContentElem(retBuf, elem);/*from w w w. j av a 2 s. com*/ } for (TypeDescription elem : paramElems) { writeContentElem(retBuf, elem); } byte[] bb = new byte[retBuf.readableBytes()]; retBuf.getBytes(0, bb); FletcherChecksum fcs = new FletcherChecksum(bb); int checksum = fcs.getChecksumAsInt(); retBuf.writeShort(checksum); return retBuf; }
From source file:ratpack.session.clientside.internal.DefaultClientSessionService.java
License:Apache License
@Override public String serializeSession(ByteBufAllocator bufferAllocator, Set<Map.Entry<String, Object>> entries) { ByteBuf[] buffers = new ByteBuf[3 * entries.size() + entries.size() - 1]; try {/* w w w. j a v a 2s . c o m*/ int i = 0; for (Map.Entry<String, Object> entry : entries) { buffers[i++] = encode(bufferAllocator, entry.getKey()); buffers[i++] = EQUALS; buffers[i++] = encode(bufferAllocator, entry.getValue().toString()); if (i < buffers.length) { buffers[i++] = AMPERSAND; } } ByteBuf payloadBuffer = Unpooled.wrappedBuffer(buffers.length, buffers); byte[] payloadBytes = new byte[payloadBuffer.readableBytes()]; payloadBuffer.getBytes(0, payloadBytes); if (crypto != null) { payloadBytes = crypto.encrypt(payloadBuffer); payloadBuffer = Unpooled.wrappedBuffer(payloadBytes); } String payloadString = ENCODER.encodeToString(payloadBytes); byte[] digest = signer.sign(payloadBuffer); String digestString = ENCODER.encodeToString(digest); return payloadString + SESSION_SEPARATOR + digestString; } finally { for (ByteBuf buffer : buffers) { if (buffer != null) { buffer.release(); } } } }