List of usage examples for io.netty.buffer ByteBuf array
public abstract byte[] array();
From source file:com.yahoo.pulsar.common.compression.Crc32cChecksumTest.java
License:Apache License
@Test public void testCrc32cSoftware() { ByteBuf payload = Unpooled.wrappedBuffer(inputBytes); // compute checksum using sw algo int sw = SOFTWARE_CRC32C_HASH.calculate(payload.array(), payload.arrayOffset() + payload.readerIndex(), payload.readableBytes());/* w w w .j a v a2 s. c o m*/ assertEquals(sw, expectedChecksum); }
From source file:com.yahoo.pulsar.common.util.XXHashChecksum.java
License:Apache License
public static long computeChecksum(ByteBuf payload) { if (payload.hasArray()) { return checksum.hash(payload.array(), payload.arrayOffset() + payload.readerIndex(), payload.readableBytes(), 0L); } else {//from w w w . ja va 2 s . c om ByteBuffer payloadNio = payload.nioBuffer(payload.readerIndex(), payload.readableBytes()); return checksum.hash(payloadNio, 0, payload.readableBytes(), 0L); } }
From source file:de.dfki.kiara.http.HttpHandler.java
License:Open Source License
@Override protected void channelRead0(final ChannelHandlerContext ctx, Object msg) throws Exception { logger.debug("Handler: {} / Channel: {}", this, ctx.channel()); if (mode == Mode.SERVER) { if (msg instanceof FullHttpRequest) { final FullHttpRequest request = (FullHttpRequest) msg; HttpRequestMessage transportMessage = new HttpRequestMessage(this, request); transportMessage.setPayload(request.content().nioBuffer()); if (logger.isDebugEnabled()) { logger.debug("RECEIVED REQUEST WITH CONTENT {}", Util.bufferToString(transportMessage.getPayload())); }/*from www.j av a 2 s .c o m*/ synchronized (listeners) { if (!listeners.isEmpty()) { for (TransportMessageListener listener : listeners) { listener.onMessage(transportMessage); } } } boolean keepAlive = HttpHeaders.isKeepAlive(request); } } else { // CLIENT if (msg instanceof HttpResponse) { HttpResponse response = (HttpResponse) msg; headers = response.headers(); //if (!response.headers().isEmpty()) { // contentType = response.headers().get("Content-Type"); //} } if (msg instanceof HttpContent) { HttpContent content = (HttpContent) msg; ByteBuf buf = content.content(); if (buf.isReadable()) { if (buf.hasArray()) { bout.write(buf.array(), buf.readerIndex(), buf.readableBytes()); } else { byte[] bytes = new byte[buf.readableBytes()]; buf.getBytes(buf.readerIndex(), bytes); bout.write(bytes); } } if (content instanceof LastHttpContent) { //ctx.close(); bout.flush(); HttpResponseMessage response = new HttpResponseMessage(this, headers); response.setPayload(ByteBuffer.wrap(bout.toByteArray(), 0, bout.size())); onResponse(response); bout.reset(); } } } }
From source file:de.dfki.kiara.netty.ByteBufferDecoder.java
License:Open Source License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception { final byte[] array; final int offset; final int length = msg.readableBytes(); if (msg.hasArray()) { array = msg.array(); offset = msg.arrayOffset() + msg.readerIndex(); } else {/*from w w w . j av a 2 s. c o m*/ array = new byte[length]; msg.getBytes(msg.readerIndex(), array, 0, length); offset = 0; } out.add(ByteBuffer.wrap(array, offset, length)); }
From source file:de.sanandrew.mods.turretmod.network.PacketSyncTileEntity.java
License:Creative Commons License
public PacketSyncTileEntity(TileClientSync tile) { this.pos = tile.getTile().getPos(); ByteBuf buf = Unpooled.buffer(); tile.toBytes(buf);/*from w w w . j ava 2s . co m*/ this.tileBytes = buf.array(); }
From source file:de.saxsys.synchronizefx.netty.base.CommandToBinaryByteBuf.java
License:Open Source License
@Override protected void decode(final ChannelHandlerContext ctx, final ByteBuf msg, final List<Object> out) throws Exception { byte[] data;/*ww w. ja v a2s.c om*/ if (msg.hasArray()) { data = msg.array(); } else { data = new byte[msg.readableBytes()]; msg.readBytes(data); } out.add(serializer.deserialize(data)); }
From source file:discord4j.gateway.payload.JacksonPayloadReader.java
License:Open Source License
@Override public Mono<GatewayPayload<?>> read(ByteBuf payload) { return Mono.create(sink -> { try {//w w w . j av a 2 s . c om GatewayPayload<?> value = mapper.readValue(payload.array(), new TypeReference<GatewayPayload<?>>() { }); sink.success(value); } catch (IOException | IllegalArgumentException e) { if (lenient) { // if eof input - just ignore if (payload.readableBytes() > 0) { log.warn("Error while decoding JSON ({} bytes): {}", payload.readableBytes(), payload.toString(StandardCharsets.UTF_8), e); } sink.success(); } else { sink.error(Exceptions.propagate(e)); } } }); }
From source file:divconq.ctp.stream.FileSourceStream.java
License:Open Source License
public void readLocalFile() { FileSystemFile fs = (FileSystemFile) this.current; if (this.in == null) { this.insize = fs.getSize(); // As a source we are responsible for progress tracking OperationContext.get().setAmountCompleted(0); try {/*from w ww. ja v a2 s . com*/ this.in = FileChannel.open(fs.localPath(), StandardOpenOption.READ); } catch (IOException x) { OperationContext.get().getTaskRun().kill("Unable to read source file " + x); return; } } while (true) { // TODO sizing? ByteBuf data = Hub.instance.getBufferAllocator().heapBuffer(32768); ByteBuffer buffer = ByteBuffer.wrap(data.array(), data.arrayOffset(), data.capacity()); int pos = -1; try { pos = (int) this.in.read(buffer); } catch (IOException x1) { OperationContext.get().getTaskRun().kill("Problem reading source file: " + x1); data.release(); return; } FileDescriptor fref = FileDescriptor.fromFileStore(this.current); fref.setPath(this.current.path().subpath(this.source.path())); System.out.println("writing: " + fref.getPath() + " from: " + this.inprog); if (pos == -1) { try { this.in.close(); } catch (IOException x) { OperationContext.get().getTaskRun().kill("Problem closing source file: " + x); data.release(); return; } OperationContext.get().setAmountCompleted(100); fref.setEof(true); this.current = null; this.in = null; this.insize = 0; this.inprog = 0; } else { this.inprog += pos; data.writerIndex(pos); OperationContext.get().setAmountCompleted((int) (this.inprog * 100 / this.insize)); } if (this.downstream.handle(fref, data) != ReturnOption.CONTINUE) break; if (this.current == null) { // we need the next file OperationContext.get().getTaskRun().resume(); // wait on the implied request break; } } }
From source file:divconq.ctp.stream.GzipStream.java
License:Open Source License
@Override public ReturnOption handle(FileDescriptor file, ByteBuf data) { if (file == FileDescriptor.FINAL) return this.downstream.handle(file, data); // we don't know what to do with a folder at this stage - gzip is for file content only // folder scanning is upstream in the FileSourceStream and partners if (file.isFolder()) return ReturnOption.CONTINUE; // init if not set for this round of processing if (this.deflater == null) { this.deflater = new Deflater(this.compressionLevel, true); this.crc.reset(); this.writeHeader = true; }//from w ww .j a v a2 s .c o m ByteBuf in = data; ByteBuf out = null; if (in != null) { byte[] inAry = in.array(); // always allow for a header (10) plus footer (8) plus extra (12) // in addition to content int sizeEstimate = (int) Math.ceil(in.readableBytes() * 1.001) + 30; out = Hub.instance.getBufferAllocator().heapBuffer(sizeEstimate); if (this.writeHeader) { this.writeHeader = false; out.writeBytes(gzipHeader); } this.crc.update(inAry, in.arrayOffset(), in.writerIndex()); this.deflater.setInput(inAry, in.arrayOffset(), in.writerIndex()); while (!this.deflater.needsInput()) deflate(out); } else out = Hub.instance.getBufferAllocator().heapBuffer(30); FileDescriptor blk = new FileDescriptor(); if (StringUtil.isEmpty(this.lastpath)) { if (StringUtil.isNotEmpty(this.nameHint)) this.lastpath = "/" + this.nameHint; else if (file.getPath() != null) this.lastpath = "/" + GzipUtils.getCompressedFilename(file.path().getFileName()); else this.lastpath = "/" + FileUtil.randomFilename("gz"); } blk.setPath(this.lastpath); file.setModTime(System.currentTimeMillis()); if (file.isEof()) { this.deflater.finish(); while (!this.deflater.finished()) deflate(out); int crcValue = (int) this.crc.getValue(); out.writeByte(crcValue); out.writeByte(crcValue >>> 8); out.writeByte(crcValue >>> 16); out.writeByte(crcValue >>> 24); int uncBytes = this.deflater.getTotalIn(); out.writeByte(uncBytes); out.writeByte(uncBytes >>> 8); out.writeByte(uncBytes >>> 16); out.writeByte(uncBytes >>> 24); this.deflater.end(); this.deflater = null; // cause a reset for next time we use stream blk.setEof(true); } if (in != null) in.release(); return this.downstream.handle(blk, out); }
From source file:divconq.ctp.stream.GzipStream.java
License:Open Source License
protected void deflate(ByteBuf out) { int numBytes = 0; do {/*from w w w . j a v a 2s . c o m*/ byte[] o = out.array(); numBytes = this.deflater.deflate(o, out.arrayOffset() + out.writerIndex(), out.writableBytes(), Deflater.SYNC_FLUSH); out.writerIndex(out.writerIndex() + numBytes); } while (numBytes > 0); }