List of usage examples for io.netty.buffer ByteBuf nioBuffer
public abstract ByteBuffer nioBuffer(int index, int length);
From source file:org.traccar.protocol.TeltonikaProtocolDecoder.java
License:Apache License
private void sendImageRequest(Channel channel, SocketAddress remoteAddress, long id, int offset, int size) { if (channel != null) { ByteBuf response = Unpooled.buffer(); response.writeInt(0);/*w ww . ja v a 2 s . c o m*/ response.writeShort(0); response.writeShort(19); // length response.writeByte(CODEC_12); response.writeByte(1); // nod response.writeByte(0x0D); // camera response.writeInt(11); // payload length response.writeByte(2); // command response.writeInt((int) id); response.writeInt(offset); response.writeShort(size); response.writeByte(1); // nod response.writeShort(0); response.writeShort( Checksum.crc16(Checksum.CRC16_IBM, response.nioBuffer(8, response.readableBytes() - 10))); channel.writeAndFlush(new NetworkMessage(response, remoteAddress)); } }
From source file:org.traccar.protocol.TeltonikaProtocolEncoder.java
License:Apache License
private ByteBuf encodeContent(String content) { ByteBuf buf = Unpooled.buffer(); buf.writeInt(0);/*from w w w . j a va 2 s .c om*/ buf.writeInt(content.length() + 10); buf.writeByte(TeltonikaProtocolDecoder.CODEC_12); buf.writeByte(1); // quantity buf.writeByte(5); // type buf.writeInt(content.length() + 2); buf.writeBytes(content.getBytes(StandardCharsets.US_ASCII)); buf.writeByte('\r'); buf.writeByte('\n'); buf.writeByte(1); // quantity buf.writeInt(Checksum.crc16(Checksum.CRC16_IBM, buf.nioBuffer(8, buf.writerIndex() - 8))); return buf; }
From source file:org.traccar.protocol.UlbotechProtocolDecoder.java
License:Apache License
@Override protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { ByteBuf buf = (ByteBuf) msg;/*from w w w . j av a 2 s.c o m*/ if (buf.getUnsignedByte(buf.readerIndex()) == 0xF8) { if (channel != null) { ByteBuf response = Unpooled.buffer(); response.writeByte(0xF8); response.writeByte(DATA_GPS); response.writeByte(0xFE); response.writeShort(buf.getShort(response.writerIndex() - 1 - 2)); response.writeShort(Checksum.crc16(Checksum.CRC16_XMODEM, response.nioBuffer(1, 4))); response.writeByte(0xF8); channel.writeAndFlush(new NetworkMessage(response, remoteAddress)); } return decodeBinary(channel, remoteAddress, buf); } else { if (channel != null) { channel.writeAndFlush( new NetworkMessage(Unpooled.copiedBuffer( String.format("*TS01,ACK:%04X#", Checksum.crc16(Checksum.CRC16_XMODEM, buf.nioBuffer(1, buf.writerIndex() - 2))), StandardCharsets.US_ASCII), remoteAddress)); } return decodeText(channel, remoteAddress, buf.toString(StandardCharsets.US_ASCII)); } }
From source file:parquet.hadoop.CodecFactoryExposer.java
License:Apache License
public BytesInput getBytesInput(ByteBuf uncompressedByteBuf, int uncompressedSize) throws IOException { ByteBuffer outBuffer = uncompressedByteBuf.nioBuffer(0, uncompressedSize); return new HadoopByteBufBytesInput(outBuffer, 0, outBuffer.limit()); }
From source file:parquet.hadoop.CodecFactoryExposer.java
License:Apache License
public BytesInput decompress(CompressionCodecName codecName, ByteBuf compressedByteBuf, ByteBuf uncompressedByteBuf, int compressedSize, int uncompressedSize) throws IOException { ByteBuffer inpBuffer = compressedByteBuf.nioBuffer(0, compressedSize); ByteBuffer outBuffer = uncompressedByteBuf.nioBuffer(0, uncompressedSize); CompressionCodec c = getCodec(codecName); //TODO: Create the decompressor only once at init time. Class<?> cx = c.getClass(); DirectDecompressionCodec d = null;// w ww . j a va 2 s.c o m DirectDecompressor decompr = null; if (DirectDecompressionCodec.class.isAssignableFrom(cx)) { d = (DirectDecompressionCodec) c; } if (d != null) { decompr = d.createDirectDecompressor(); } if (d != null && decompr != null) { decompr.decompress(inpBuffer, outBuffer); } else { logger.warn("This Hadoop implementation does not support a " + codecName + " direct decompression codec interface. " + "Direct decompression is available only on *nix systems with Hadoop 2.3 or greater. " + "Read operations will be a little slower. "); BytesInput outBytesInp = this.decompress(new HadoopByteBufBytesInput(inpBuffer, 0, inpBuffer.limit()), uncompressedSize, codecName); // COPY the data back into the output buffer. // (DrillBufs can only refer to direct memory, so we cannot pass back a BytesInput backed // by a byte array). outBuffer.put(outBytesInp.toByteArray()); } return new HadoopByteBufBytesInput(outBuffer, 0, outBuffer.limit()); }
From source file:qunar.tc.qmq.backup.service.impl.ActionSyncLogIterator.java
License:Apache License
@Override public LogVisitorRecord<Action> next(ByteBuf buf) { final int magic = buf.readInt(); if (magic != MagicCode.ACTION_LOG_MAGIC_V1) { return LogVisitorRecord.noMore(); }/* w w w . j a v a2 s . com*/ final byte attributes = buf.readByte(); if (attributes == ATTR_BLANK_RECORD) { if (buf.readableBytes() < Integer.BYTES) { return LogVisitorRecord.noMore(); } final int blankSize = buf.readInt(); buf.readerIndex(buf.readerIndex() + blankSize); return LogVisitorRecord.data(BLANK_ACTION); } else if (attributes == ATTR_EMPTY_RECORD) { buf.readerIndex(buf.readerIndex() + buf.readableBytes()); return LogVisitorRecord.empty(); } else if (attributes == ATTR_ACTION_RECORD) { if (buf.readableBytes() < Integer.BYTES + Byte.BYTES) { return LogVisitorRecord.noMore(); } final ActionType payloadType = ActionType.fromCode(buf.readByte()); final int payloadSize = buf.readInt(); if (buf.readableBytes() < payloadSize) { return LogVisitorRecord.noMore(); } if (buf.nioBufferCount() > 0) { final int payloadIndex = buf.readerIndex(); final Action action = payloadType.getReaderWriter().read(buf.nioBuffer(payloadIndex, payloadSize)); buf.readerIndex(buf.readerIndex() + payloadSize); return LogVisitorRecord.data(action); } else { return LogVisitorRecord.data(BLANK_ACTION); } } else { throw new RuntimeException("Unknown record type"); } }
From source file:qunar.tc.qmq.protocol.MessagesPayloadHolder.java
License:Apache License
private long messageCrc(ByteBuf out, int messageStart, int messageLength) { return Crc32.crc32(out.nioBuffer(messageStart, messageLength), 0, messageLength); }
From source file:ratpack.file.internal.FileReadingPublisher.java
License:Apache License
@Override public void subscribe(Subscriber<? super ByteBuf> s) { file.onError(s::onError)/*w ww .j av a 2 s. com*/ .then(channel -> s.onSubscribe(new ManagedSubscription<ByteBuf>(s, ByteBuf::release) { private final AtomicBoolean reading = new AtomicBoolean(); private long position = start; @Override protected void onRequest(long n) { doRead(); } @Override protected void onCancel() { } private void read() { if (reading.compareAndSet(false, true)) { doRead(); } } private void doRead() { Promise.<ByteBuf>async(down -> { int size = (int) Math.min(stop - position, bufferSize); ByteBuf buffer = allocator.buffer(size, size); channel.read(buffer.nioBuffer(0, size), position, buffer, new CompletionHandler<Integer, ByteBuf>() { @Override public void completed(Integer result, ByteBuf attachment) { attachment.writerIndex(Math.max(result, 0)); down.success(attachment); } @Override public void failed(Throwable exc, ByteBuf attachment) { attachment.release(); down.error(exc); } }); }).onError(this::complete).then(read -> { if (read.readableBytes() == 0) { read.release(); complete(null); } else { position += read.readableBytes(); emitNext(read); if (position == stop) { complete(null); } else if (hasDemand()) { doRead(); } else { reading.set(false); if (hasDemand()) { read(); } } } }); } private void complete(Throwable error) { Promise<?> p = error == null ? Promise.ofNull() : Promise.error(error); p.close(Blocking.op(((AsynchronousFileChannel) channel)::close)).onError(this::emitError) .then(v -> emitComplete()); } })); }