List of usage examples for io.netty.buffer ByteBuf nioBuffer
public abstract ByteBuffer nioBuffer(int index, int length);
From source file:org.apache.pulsar.client.impl.MessageCrypto.java
License:Apache License
private ByteBuf decryptData(SecretKey dataKeySecret, MessageMetadata msgMetadata, ByteBuf payload) { // unpack iv and encrypted data ByteString ivString = msgMetadata.getEncryptionParam(); ivString.copyTo(iv, 0);//from www . j a v a 2 s .c o m GCMParameterSpec gcmParams = new GCMParameterSpec(tagLen, iv); ByteBuf targetBuf = null; try { cipher.init(Cipher.DECRYPT_MODE, dataKeySecret, gcmParams); ByteBuffer sourceNioBuf = payload.nioBuffer(payload.readerIndex(), payload.readableBytes()); int maxLength = cipher.getOutputSize(payload.readableBytes()); targetBuf = PooledByteBufAllocator.DEFAULT.buffer(maxLength, maxLength); ByteBuffer targetNioBuf = targetBuf.nioBuffer(0, maxLength); int decryptedSize = cipher.doFinal(sourceNioBuf, targetNioBuf); targetBuf.writerIndex(decryptedSize); } catch (InvalidKeyException | InvalidAlgorithmParameterException | IllegalBlockSizeException | BadPaddingException | ShortBufferException e) { log.error("{} Failed to decrypt message {}", logCtx, e.getMessage()); if (targetBuf != null) { targetBuf.release(); targetBuf = null; } } return targetBuf; }
From source file:org.apache.pulsar.common.compression.CompressionCodecZstd.java
License:Apache License
@Override public ByteBuf encode(ByteBuf source) { int uncompressedLength = source.readableBytes(); int maxLength = (int) Zstd.compressBound(uncompressedLength); ByteBuf target = PooledByteBufAllocator.DEFAULT.directBuffer(maxLength, maxLength); int compressedLength; if (source.hasMemoryAddress()) { compressedLength = (int) Zstd.compressUnsafe(target.memoryAddress(), maxLength, source.memoryAddress() + source.readerIndex(), uncompressedLength, ZSTD_COMPRESSION_LEVEL); } else {//from w w w . j ava 2 s.com ByteBuffer sourceNio = source.nioBuffer(source.readerIndex(), source.readableBytes()); ByteBuffer targetNio = target.nioBuffer(0, maxLength); compressedLength = Zstd.compress(targetNio, sourceNio, ZSTD_COMPRESSION_LEVEL); } target.writerIndex(compressedLength); return target; }
From source file:org.apache.pulsar.common.compression.CompressionCodecZstd.java
License:Apache License
@Override public ByteBuf decode(ByteBuf encoded, int uncompressedLength) throws IOException { ByteBuf uncompressed = PooledByteBufAllocator.DEFAULT.directBuffer(uncompressedLength, uncompressedLength); if (encoded.hasMemoryAddress()) { Zstd.decompressUnsafe(uncompressed.memoryAddress(), uncompressedLength, encoded.memoryAddress() + encoded.readerIndex(), encoded.readableBytes()); } else {/*from ww w . ja v a 2s . c o m*/ ByteBuffer uncompressedNio = uncompressed.nioBuffer(0, uncompressedLength); ByteBuffer encodedNio = encoded.nioBuffer(encoded.readerIndex(), encoded.readableBytes()); Zstd.decompress(uncompressedNio, encodedNio); } uncompressed.writerIndex(uncompressedLength); return uncompressed; }
From source file:org.apache.spark.sql.hive.thriftserver.rsc.KryoMessageCodec.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { if (in.readableBytes() < 4) { return;/* w w w . j av a2s . c om*/ } in.markReaderIndex(); int msgSize = in.readInt(); checkSize(msgSize); if (in.readableBytes() < msgSize) { // Incomplete message in buffer. in.resetReaderIndex(); return; } try { ByteBuffer nioBuffer = maybeDecrypt(in.nioBuffer(in.readerIndex(), msgSize)); Object msg = serializer.deserialize(nioBuffer); LOG.info("Decoded message of type {} ({} bytes)", msg != null ? msg.getClass().getName() : msg, msgSize); out.add(msg); } catch (Exception e) { Throwable throwable = e; while (throwable != null) { LOG.info("tlitest cause: " + throwable.getCause()); LOG.info("tlitest message: " + throwable.getMessage()); StringBuilder builder = new StringBuilder(); for (StackTraceElement elem : throwable.getStackTrace()) { builder.append(elem); builder.append("\n"); } LOG.info(builder.toString()); throwable = throwable.getCause(); } throw e; } finally { in.skipBytes(msgSize); } }
From source file:org.apache.tajo.plan.function.stream.TextFieldSerializerDeserializer.java
License:Apache License
@Override public Datum deserialize(ByteBuf buf, Column col, int columnIndex, ByteBuf nullChars) throws IOException { Datum datum;/* w w w .j a v a 2s. com*/ TajoDataTypes.Type type = col.getDataType().getType(); boolean nullField; if (type == TajoDataTypes.Type.TEXT || type == TajoDataTypes.Type.CHAR) { nullField = isNullText(buf, nullChars); } else { nullField = isNull(buf, nullChars); } if (nullField) { datum = NullDatum.get(); } else { switch (type) { case BOOLEAN: byte bool = buf.readByte(); datum = DatumFactory.createBool(bool == 't' || bool == 'T'); break; case BIT: datum = DatumFactory.createBit(Byte.parseByte( decoder.decode(buf.nioBuffer(buf.readerIndex(), buf.readableBytes())).toString())); break; case CHAR: datum = DatumFactory.createChar( decoder.decode(buf.nioBuffer(buf.readerIndex(), buf.readableBytes())).toString().trim()); break; case INT1: case INT2: datum = DatumFactory.createInt2((short) NumberUtil.parseInt(buf)); break; case INT4: datum = DatumFactory.createInt4(NumberUtil.parseInt(buf)); break; case INT8: datum = DatumFactory.createInt8(NumberUtil.parseLong(buf)); break; case FLOAT4: datum = DatumFactory.createFloat4( decoder.decode(buf.nioBuffer(buf.readerIndex(), buf.readableBytes())).toString()); break; case FLOAT8: datum = DatumFactory.createFloat8(NumberUtil.parseDouble(buf)); break; case TEXT: { byte[] bytes = new byte[buf.readableBytes()]; buf.readBytes(bytes); datum = DatumFactory.createText(bytes); break; } case DATE: datum = DatumFactory.createDate( decoder.decode(buf.nioBuffer(buf.readerIndex(), buf.readableBytes())).toString()); break; case TIME: if (hasTimezone) { datum = DatumFactory.createTime( decoder.decode(buf.nioBuffer(buf.readerIndex(), buf.readableBytes())).toString(), timezone); } else { datum = DatumFactory.createTime( decoder.decode(buf.nioBuffer(buf.readerIndex(), buf.readableBytes())).toString()); } break; case TIMESTAMP: if (hasTimezone) { datum = DatumFactory.createTimestamp( decoder.decode(buf.nioBuffer(buf.readerIndex(), buf.readableBytes())).toString(), timezone); } else { datum = DatumFactory.createTimestamp( decoder.decode(buf.nioBuffer(buf.readerIndex(), buf.readableBytes())).toString()); } break; case INTERVAL: datum = DatumFactory.createInterval( decoder.decode(buf.nioBuffer(buf.readerIndex(), buf.readableBytes())).toString()); break; case PROTOBUF: { ProtobufDatumFactory factory = ProtobufDatumFactory.get(col.getDataType()); Message.Builder builder = factory.newBuilder(); try { byte[] bytes = new byte[buf.readableBytes()]; buf.readBytes(bytes); protobufJsonFormat.merge(bytes, builder); datum = factory.createDatum(builder.build()); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException(e); } break; } case INET4: datum = DatumFactory.createInet4( decoder.decode(buf.nioBuffer(buf.readerIndex(), buf.readableBytes())).toString()); break; case BLOB: { byte[] bytes = new byte[buf.readableBytes()]; buf.readBytes(bytes); datum = DatumFactory.createBlob(Base64.decodeBase64(bytes)); break; } default: datum = NullDatum.get(); break; } } return datum; }
From source file:org.apache.tajo.storage.regex.RegexLineDeserializer.java
License:Apache License
@Override public void deserialize(final ByteBuf lineBuf, Tuple output) throws IOException, TextLineParsingError { if (lineBuf == null || targetColumnIndexes.length == 0) { return;/* w w w . ja v a2 s . c o m*/ } String line = decoder.decode(lineBuf.nioBuffer(lineBuf.readerIndex(), lineBuf.readableBytes())).toString(); int[] projection = targetColumnIndexes; // Projection int currentTarget = 0; int currentIndex = 0; Matcher m = inputPattern.matcher(line); if (!m.matches()) { unmatchedRows++; if (unmatchedRows >= nextUnmatchedRows) { nextUnmatchedRows *= 100; // Report the row LOG.warn("" + unmatchedRows + " unmatched rows are found: " + line); } } else { int groupCount = m.groupCount(); int currentGroup = 1; while (currentGroup <= groupCount) { if (projection.length > currentTarget && currentIndex == projection[currentTarget]) { try { Datum datum = fieldSerDer.deserialize(currentIndex, lineBuf.setIndex(m.start(currentGroup), m.end(currentGroup)), nullChars); output.put(currentTarget, datum); } catch (Exception e) { partialMatchedRows++; if (partialMatchedRows >= nextPartialMatchedRows) { nextPartialMatchedRows *= 100; // Report the row LOG.warn("" + partialMatchedRows + " partially unmatched rows are found, " + " cannot find group " + currentIndex + ": " + line); } output.put(currentTarget, NullDatum.get()); } currentTarget++; } if (projection.length == currentTarget) { break; } currentIndex++; currentGroup++; } } /* If a text row is less than table schema size, tuple should set to NullDatum */ if (projection.length > currentTarget) { for (; currentTarget < projection.length; currentTarget++) { output.put(currentTarget, NullDatum.get()); } } }
From source file:org.curioswitch.common.server.framework.redis.ProtobufRedisCodec.java
License:Open Source License
private static void encodeTo(Message message, ByteBuf target) { try {/* w ww . j av a 2s. c om*/ message.writeTo( CodedOutputStream.newInstance(target.nioBuffer(target.writerIndex(), target.writableBytes()))); } catch (IOException e) { throw new UncheckedIOException("Could not encode message.", e); } }
From source file:org.dcache.http.ReusableChunkedNioFile.java
License:Apache License
/** * Like {@link ChunkedNioFile#readChunk}, but uses position independent * IO calls./* w w w . j a v a 2 s . c o m*/ */ @Override public ByteBuf readChunk(ByteBufAllocator allocator) throws Exception { long offset = _offset; if (offset >= _endOffset) { return null; } int length = (int) Math.min(_chunkSize, _endOffset - offset); ByteBuf chunk = allocator.buffer(length); boolean release = true; try { ByteBuffer buffer = chunk.nioBuffer(0, length); while (buffer.hasRemaining()) { /* use position independent thread safe call */ int bytes = _channel.read(buffer, offset); if (bytes < 0) { break; } offset += bytes; } chunk.writerIndex(buffer.position()); _offset = offset; release = false; return chunk; } finally { if (release) { chunk.release(); } } }
From source file:org.opendaylight.usc.protocol.UscFrame.java
License:Open Source License
/** * Decodes a ByteBuf into a UscFrame// w ww .j a va 2 s.c om * * @param buf * @return * @throws IOException */ public static UscFrame getFromByteBuf(ByteBuf buf) throws IOException { final UscHeader header = UscHeader.fromByteBuffer(buf.nioBuffer(0, UscHeader.HEADER_LENGTH)); buf.readerIndex(UscHeader.HEADER_LENGTH); final int port = header.getApplicationPort(); final int sessionId = header.getSessionId(); final UscFrame result; switch (header.getOperationType()) { case DATA: result = new UscData(port, sessionId, buf.copy()); break; case CONTROL: result = new UscControl(port, sessionId, buf.readUnsignedShort()); break; case ERROR: result = new UscError(port, sessionId, buf.readUnsignedShort()); break; default: result = null; throw new IOException("Invalid operation type"); } return result; }
From source file:org.traccar.protocol.ApelProtocolDecoder.java
License:Apache License
private void sendSimpleMessage(Channel channel, short type) { ByteBuf request = Unpooled.buffer(8); request.writeShortLE(type);//w ww . ja v a 2 s. c o m request.writeShortLE(0); request.writeIntLE(Checksum.crc32(request.nioBuffer(0, 4))); channel.writeAndFlush(new NetworkMessage(request, channel.remoteAddress())); }