本文整理汇总了Java中org.jboss.netty.channel.ChannelFutureProgressListener类的典型用法代码示例。如果您正苦于以下问题:Java ChannelFutureProgressListener类的具体用法?Java ChannelFutureProgressListener怎么用?Java ChannelFutureProgressListener使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ChannelFutureProgressListener类属于org.jboss.netty.channel包,在下文中一共展示了ChannelFutureProgressListener类的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: messageReceived
import org.jboss.netty.channel.ChannelFutureProgressListener; //导入依赖的package包/类
@Override
public void messageReceived( ChannelHandlerContext ctx, MessageEvent e ) throws Exception
{
HttpRequest request = (HttpRequest)e.getMessage( ) ;
if( request.getMethod( ) != GET )
{
sendError( ctx, FORBIDDEN ) ;
return ;
}
final String path = sanitizeUri( request.getUri( ) ) ;
if( path == null )
{
sendError( ctx, FORBIDDEN ) ;
return ;
}
File file = new File(path) ;
if( file.isHidden( ) || !file.exists( ) )
{
sendError( ctx, NOT_FOUND ) ;
return ;
}
RandomAccessFile raf ;
try
{
raf = new RandomAccessFile( file, "r" ) ;
}
catch( FileNotFoundException fnfe )
{
sendError( ctx, NOT_FOUND ) ;
return;
}
long fileLength = raf.length( ) ;
HttpResponse response = new DefaultHttpResponse( HTTP_1_1, OK ) ;
setContentLength( response, fileLength ) ;
Channel ch = e.getChannel( ) ;
//Escreve a linha inicial do cabe�alho
ch.write( response ) ;
// Escreve o conte�do
ChannelFuture writeFuture ;
if( ch.getPipeline( ).get( SslHandler.class ) != null )
{
writeFuture = ch.write( new ChunkedFile( raf, 0, fileLength, 8192 ) ) ;
}
else
{
final FileRegion region = new DefaultFileRegion( raf.getChannel( ), 0, fileLength ) ;
writeFuture = ch.write( region ) ;
writeFuture.addListener( new ChannelFutureProgressListener( )
{
@Override
public void operationComplete( ChannelFuture arg0 ) throws Exception
{
region.releaseExternalResources( ) ;
}
@Override
public void operationProgressed( ChannelFuture future, long amount, long current, long total ) throws Exception
{
System.out.printf( "%s: %d / %d (+%d)%n", path, current, total, amount );
}
}) ;
}
// Decide se fecha a conex�o ou n�o!!
if( !isKeepAlive( request ) )
{
writeFuture.addListener( ChannelFutureListener.CLOSE ) ;
}
}