org.jboss.netty.channel.DefaultFileRegion Java Examples

The following examples show how to use org.jboss.netty.channel.DefaultFileRegion. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: NettyProtocolTransport.java    From james-project with Apache License 2.0 6 votes vote down vote up
@Override
protected void writeToClient(InputStream in, ProtocolSession session, boolean startTLS) {
    if (startTLS) {
        prepareStartTLS();
    }
    if (!isTLSStarted()) {
        if (in instanceof FileInputStream) {
            FileChannel fChannel = ((FileInputStream) in).getChannel();
            try {
                channel.write(new DefaultFileRegion(fChannel, 0, fChannel.size(), true));

            } catch (IOException e) {
                // We handle this later
                channel.write(new ChunkedStream(new ExceptionInputStream(e)));
            }
            return;
        }
    }
    channel.write(new ChunkedStream(in));
}
 
Example #2
Source File: ChannelImapResponseWriter.java    From james-project with Apache License 2.0 6 votes vote down vote up
@Override
public void write(Literal literal) throws IOException {
    if (channel.isConnected()) {
        InputStream in = literal.getInputStream();
        if (in instanceof FileInputStream && channel.getFactory() instanceof NioServerSocketChannelFactory) {
            FileChannel fc = ((FileInputStream) in).getChannel();
            // Zero-copy is only possible if no SSL/TLS  and no COMPRESS is in place
            //
            // See JAMES-1305 and JAMES-1306
            ChannelPipeline cp = channel.getPipeline();
            if (zeroCopy && cp.get(SslHandler.class) == null && cp.get(ZlibEncoder.class) == null) {
                channel.write(new DefaultFileRegion(fc, fc.position(), literal.size()));
            } else {
                channel.write(new ChunkedNioFile(fc, 8192));
            }
        } else {
            channel.write(new ChunkedStream(literal.getInputStream()));
        }
    }
}
 
Example #3
Source File: SocketSendBufferPool.java    From android-netty with Apache License 2.0 5 votes vote down vote up
public void release() {
    if (file instanceof DefaultFileRegion) {
        if (((DefaultFileRegion) file).releaseAfterTransfer()) {
            // Make sure the FileRegion resource are released otherwise it may cause a FD
            // leak or something similar
            file.releaseExternalResources();
        }
    }
}
 
Example #4
Source File: ShuffleHandler.java    From tez with Apache License 2.0 5 votes vote down vote up
protected ChannelFuture sendMapOutput(ChannelHandlerContext ctx, Channel ch,
    String user, String mapId, int reduce, MapOutputInfo mapOutputInfo)
    throws IOException {
  final TezIndexRecord info = mapOutputInfo.indexRecord;
  final ShuffleHeader header =
    new ShuffleHeader(mapId, info.getPartLength(), info.getRawLength(), reduce);
  final DataOutputBuffer dob = new DataOutputBuffer();
  header.write(dob);
  ch.write(wrappedBuffer(dob.getData(), 0, dob.getLength()));
  final File spillfile =
      new File(mapOutputInfo.mapOutputFileName.toString());
  RandomAccessFile spill;
  try {
    spill = SecureIOUtils.openForRandomRead(spillfile, "r", user, null);
  } catch (FileNotFoundException e) {
    LOG.info(spillfile + " not found");
    return null;
  }
  ChannelFuture writeFuture;
  final DefaultFileRegion partition =
      new DefaultFileRegion(spill.getChannel(), info.getStartOffset(), info.getPartLength());
  writeFuture = ch.write(partition);
  writeFuture.addListener(new ChannelFutureListener() {
    // TODO error handling; distinguish IO/connection failures,
    //      attribute to appropriate spill output
    @Override
    public void operationComplete(ChannelFuture future) {
      partition.releaseExternalResources();
    }
  });
  return writeFuture;
}