List of usage examples for io.netty.util ReferenceCountUtil release
public static boolean release(Object msg)
From source file:org.dcache.xrootd.protocol.messages.AsyncResponse.java
License:Open Source License
@Override protected void deallocate() { ReferenceCountUtil.release(response); }
From source file:org.dcache.xrootd.stream.AbstractChunkedReadvResponse.java
License:Open Source License
@Override public ReadVResponse nextChunk(ByteBufAllocator alloc) throws Exception { if (isEndOfInput()) { return null; }//from w w w .j av a2 s. com int count = getChunksInNextFrame(maxFrameSize); ByteBuf[] chunks = new ByteBuf[requests.length]; try { for (int i = index; i < index + count; i++) { chunks[i] = read(alloc, requests[i]); } ReadVResponse response = new ReadVResponse(request, requests, chunks, index, count, index + count < requests.length); index += count; return response; } catch (RuntimeException | IOException | XrootdException e) { for (ByteBuf chunk : chunks) { if (chunk != null) { ReferenceCountUtil.release(chunk); } } throw e; } }
From source file:org.dcache.xrootd.stream.ChunkedFileChannelReadResponse.java
License:Open Source License
@Override protected ByteBuf read(ByteBufAllocator alloc, long position, int length) throws IOException { ByteBuf chunk = alloc.ioBuffer(length); try {// w w w . j a v a2 s . c o m chunk.writerIndex(length); ByteBuffer buffer = chunk.nioBuffer(); while (length > 0) { /* use position independent thread safe call */ int bytes = channel.read(buffer, position); if (bytes < 0) { break; } position += bytes; length -= bytes; } chunk.writerIndex(chunk.writerIndex() - length); return chunk; } catch (RuntimeException | IOException e) { ReferenceCountUtil.release(chunk); throw e; } }
From source file:org.dcache.xrootd.stream.ChunkedFileChannelReadvResponse.java
License:Open Source License
@Override protected ByteBuf read(ByteBufAllocator alloc, int fd, long position, int length) throws IOException, XrootdException { checkValidFileDescriptor(fd);//from w w w . j a va 2s .c o m FileChannel channel = channels.get(fd); ByteBuf chunk = alloc.ioBuffer(length); try { chunk.writerIndex(length); ByteBuffer buffer = chunk.nioBuffer(); while (length > 0) { /* use position independent thread safe call */ int bytes = channel.read(buffer, position); if (bytes < 0) { break; } position += bytes; length -= bytes; } chunk.writerIndex(chunk.writerIndex() - length); return chunk; } catch (RuntimeException | IOException e) { ReferenceCountUtil.release(chunk); throw e; } }
From source file:org.dcache.xrootd.stream.ChunkedFileReadvResponse.java
License:Open Source License
@Override protected ByteBuf read(ByteBufAllocator alloc, int fd, long position, int length) throws IOException, XrootdException { if (fd < 0 || fd >= files.size() || files.get(fd) == null) { throw new XrootdException(kXR_FileNotOpen, "Invalid file descriptor"); }/* w w w . j a v a 2s .com*/ FileChannel channel = files.get(fd).getChannel(); ByteBuf chunk = alloc.ioBuffer(length); try { chunk.writerIndex(length); ByteBuffer buffer = chunk.nioBuffer(); while (length > 0) { /* use position independent thread safe call */ int bytes = channel.read(buffer, position); if (bytes < 0) { break; } position += bytes; length -= bytes; } chunk.writerIndex(chunk.writerIndex() - length); return chunk; } catch (RuntimeException | IOException e) { ReferenceCountUtil.release(chunk); throw e; } }
From source file:org.dcache.xrootd.stream.ChunkedResponseWriteHandler.java
License:Open Source License
private boolean doFlush(final ChannelHandlerContext ctx) throws Exception { final Channel channel = ctx.channel(); if (!channel.isActive()) { discard(null);/* www . j a v a 2s .com*/ return false; } boolean flushed = false; while (channel.isWritable()) { if (currentWrite == null) { currentWrite = queue.poll(); } if (currentWrite == null) { break; } final PendingWrite currentWrite = this.currentWrite; final ChunkedResponse pendingMessage = currentWrite.msg; boolean endOfInput; Object message = null; try { message = pendingMessage.nextChunk(ctx.alloc()); endOfInput = pendingMessage.isEndOfInput(); } catch (final Throwable t) { this.currentWrite = null; if (message != null) { ReferenceCountUtil.release(message); } currentWrite.fail(t); break; } if (message == null) { // If message is null write an empty ByteBuf. // See https://github.com/netty/netty/issues/1671 message = Unpooled.EMPTY_BUFFER; } final int amount = amount(message); ChannelFuture f = ctx.write(message); if (endOfInput) { this.currentWrite = null; // Register a listener which will close the input once the write is complete. // This is needed because the Chunk may have some resource bound that can not // be closed before its not written. // // See https://github.com/netty/netty/issues/303 f.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { if (!future.isSuccess()) { currentWrite.fail(future.cause()); } else { currentWrite.progress(amount); currentWrite.success(); } } }); } else { f.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { if (!future.isSuccess()) { currentWrite.fail(future.cause()); } else { currentWrite.progress(amount); } } }); } // Always need to flush ctx.flush(); flushed = true; if (!channel.isActive()) { discard(new ClosedChannelException()); break; } } return flushed; }
From source file:org.dcache.xrootd.tpc.TpcSourceReadHandler.java
License:Open Source License
@Override protected void doOnReadResponse(ChannelHandlerContext ctx, InboundReadResponse response) { try {//from w ww . ja v a 2 s.c o m int status = response.getStatus(); XrootdTpcInfo tpcInfo = client.getInfo(); int bytesRcvd = response.getDlen(); LOGGER.trace( "Read response received for {} on {}, channel {}, " + "stream {}: status {}, " + "got {} more bytes.", tpcInfo.getLfn(), tpcInfo.getSrc(), ctx.channel().id(), client.getStreamId(), status, bytesRcvd); if (status != kXR_ok && status != kXR_oksofar) { String error = String.format("Read of %s failed with status %s.", tpcInfo.getLfn(), status); handleTransferTerminated(kXR_error, error, ctx); return; } long writeOffset = client.getWriteOffset(); if (bytesRcvd > 0) { try { response.setWriteOffset(writeOffset); client.getWriteHandler().write(response); writeOffset += bytesRcvd; client.setWriteOffset(writeOffset); } catch (ClosedChannelException e) { handleTransferTerminated(kXR_ServerError, "Channel " + ctx.channel().id() + " was forcefully " + "closed by the server.", ctx); return; } catch (IOException e) { handleTransferTerminated(kXR_IOError, e.toString(), ctx); return; } LOGGER.trace("Read of {} on {}, channel {}, stream {}: " + "wrote {}, " + "so far {}, expected {}.", tpcInfo.getLfn(), tpcInfo.getSrc(), ctx.channel().id(), client.getStreamId(), bytesRcvd, writeOffset, tpcInfo.getAsize()); } if (status == kXR_oksofar) { LOGGER.trace("Waiting for more data for {} on {}, " + "channel {}, stream {}", tpcInfo.getLfn(), tpcInfo.getSrc(), ctx.channel().id(), client.getStreamId()); return; } if (writeOffset < tpcInfo.getAsize()) { sendReadRequest(ctx); } else if (tpcInfo.getCks() != null) { sendChecksumRequest(ctx); } else { LOGGER.trace( "Read for {} on {}, channel {}, stream {}," + " completed without " + "checksum verification.", tpcInfo.getLfn(), tpcInfo.getSrc(), ctx.channel().id(), client.getStreamId()); handleTransferTerminated(kXR_ok, null, ctx); } } finally { ReferenceCountUtil.release(response); } }
From source file:org.eclipse.neoscada.protocol.iec60870.apci.APDUEncoder.java
License:Open Source License
private void handleIFormat(final InformationTransfer msg, ByteBuf out) { final ByteBuf data = msg.getData(); try {/*from w ww. j ava 2s . c om*/ out = out.order(ByteOrder.LITTLE_ENDIAN); final int len = data.readableBytes(); if (len > Constants.APCI_MAX_DATA_LENGTH) { throw new EncoderException(String.format("Packet too big - %s bytes", len)); } out.ensureWritable(6 + len); out.writeByte(Constants.START_BYTE); out.writeByte(4 + len); out.writeShort(msg.getSendSequenceNumber() << 1); out.writeShort(msg.getReceiveSequenceNumber() << 1); out.writeBytes(data); } finally { ReferenceCountUtil.release(msg.getData()); } }
From source file:org.eclipse.neoscada.protocol.iec60870.apci.MessageChannel.java
License:Open Source License
private ByteBuf encode(final ChannelHandlerContext ctx, final Object msg) { ByteBuf buf = ctx.alloc().buffer(255); try {//from w w w. j a va2 s.com this.manager.encodeMessage(msg, buf); if (buf.isReadable()) { // copy away the reference so it does not get released final ByteBuf buf2 = buf; buf = null; return buf2; } } finally { ReferenceCountUtil.release(buf); } return null; }
From source file:org.fengbaoxp.netty.official.DiscardServerHandler.java
License:Apache License
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { ByteBuf in = (ByteBuf) msg;//from www. ja v a 2 s . c om try { StringBuilder smsg = new StringBuilder(); while (in.isReadable()) { smsg.append((char) in.readByte()); } logger.info(smsg.toString()); } finally { ReferenceCountUtil.release(msg); } }