当前位置: 首页>>代码示例>>Java>>正文


Java ChunkedFile类代码示例

本文整理汇总了Java中org.jboss.netty.handler.stream.ChunkedFile的典型用法代码示例。如果您正苦于以下问题:Java ChunkedFile类的具体用法?Java ChunkedFile怎么用?Java ChunkedFile使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


ChunkedFile类属于org.jboss.netty.handler.stream包,在下文中一共展示了ChunkedFile类的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: sendFile

import org.jboss.netty.handler.stream.ChunkedFile; //导入依赖的package包/类
private ChannelFuture sendFile(ChannelHandlerContext ctx, Channel ch, FileChunk file) throws IOException {
  RandomAccessFile raf;
  try {
    raf = new RandomAccessFile(file.getFile(), "r");
  } catch (FileNotFoundException fnfe) {
    return null;
  }

  ChannelFuture writeFuture;
  if (ch.getPipeline().get(SslHandler.class) != null) {
    // Cannot use zero-copy with HTTPS.
    writeFuture = ch.write(new ChunkedFile(raf, file.startOffset(), file.length(), 8192));
  } else {
    // No encryption - use zero-copy.
    final FileRegion region = new DefaultFileRegion(raf.getChannel(), file.startOffset(), file.length());
    writeFuture = ch.write(region);
    writeFuture.addListener(new ChannelFutureListener() {
      public void operationComplete(ChannelFuture future) {
        region.releaseExternalResources();
      }
    });
  }

  return writeFuture;
}
 
开发者ID:apache,项目名称:incubator-tajo,代码行数:26,代码来源:HttpDataServerHandler.java

示例2: fullContentResponse

import org.jboss.netty.handler.stream.ChunkedFile; //导入依赖的package包/类
private void fullContentResponse(HttpRequest request, String index, final String digest) throws  IOException {
    BlobShard blobShard = localBlobShard(index, digest);
    HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
    final RandomAccessFile raf = blobShard.blobContainer().getRandomAccessFile(digest);
    try {
        HttpHeaders.setContentLength(response, raf.length());
        setDefaultGetHeaders(response);
        LOGGER.trace("HttpResponse: {}", response);
        Channel channel = ctx.getChannel();
        channel.write(response);
        ChannelFuture writeFuture;
        if (sslEnabled) {
            // Cannot use zero-copy with HTTPS.
            writeFuture = channel.write(new ChunkedFile(raf, 0, raf.length(), 8192));
        } else {
            writeFuture = transferFile(digest, raf, 0, raf.length());
        }
        if (!HttpHeaders.isKeepAlive(request)) {
            writeFuture.addListener(ChannelFutureListener.CLOSE);
        }
    } catch (Throwable t) {
        /**
         * Make sure RandomAccessFile is closed when exception is raised.
         * In case of success, the ChannelFutureListener in "transferFile" will take care
         * that the resources are released.
         */
        raf.close();
        throw t;
    }
}
 
开发者ID:baidu,项目名称:Elasticsearch,代码行数:31,代码来源:HttpBlobHandler.java

示例3: sendFile

import org.jboss.netty.handler.stream.ChunkedFile; //导入依赖的package包/类
private ChannelFuture sendFile(ChannelHandlerContext ctx,
                               Channel ch,
                               FileChunk file) throws IOException {
  RandomAccessFile raf;
  try {
    raf = new RandomAccessFile(file.getFile(), "r");
  } catch (FileNotFoundException fnfe) {
    return null;
  }

  ChannelFuture writeFuture;
  if (ch.getPipeline().get(SslHandler.class) != null) {
    // Cannot use zero-copy with HTTPS.
    writeFuture = ch.write(new ChunkedFile(raf, file.startOffset(),
        file.length(), 8192));
  } else {
    // No encryption - use zero-copy.
    final FileRegion region = new DefaultFileRegion(raf.getChannel(),
        file.startOffset(), file.length());
    writeFuture = ch.write(region);
    writeFuture.addListener(new ChannelFutureListener() {
      public void operationComplete(ChannelFuture future) {
        region.releaseExternalResources();
      }
    });
  }

  return writeFuture;
}
 
开发者ID:apache,项目名称:incubator-tajo,代码行数:30,代码来源:HttpDataServerHandler.java

示例4: apply

import org.jboss.netty.handler.stream.ChunkedFile; //导入依赖的package包/类
@Override
public void apply(final Request request, final Response response) {
	try {
		response.status = 206;
		Header rangeHeader = request.headers.get("range");
		String rangeValue = rangeHeader.value().trim().substring("bytes=".length());
		long fileLength = this.file.length();
		long start, end;
		if (rangeValue.startsWith("-")) {
			end = fileLength - 1;
			start = fileLength - 1 - Long.parseLong(rangeValue.substring("-".length()));
		} else {
			String[] range = rangeValue.split("-");
			start = Long.parseLong(range[0]);
			end = range.length > 1 ? Long.parseLong(range[1]) : fileLength - 1;
		}
		if (end > fileLength - 1) {
			end = fileLength - 1;
		}
		if (start <= end) {
			long contentLength = end - start + 1;
			
			Loggers.Play.debug("Partial download, start: " + start + ", contentLength: " + contentLength);
			
			response.setHeader("Content-Length", contentLength + "");
			response.setHeader("Content-Range", "bytes " + start + "-" + end + "/" + fileLength);
			response.setHeader("Content-Type", mime);
			RandomAccessFile raf = new RandomAccessFile(this.file, "r");
			response.direct = new ChunkedFile(raf, start, contentLength, 8192);
		}
	} catch (Exception e) {
		throw new UnexpectedException(e);
	}
}
 
开发者ID:hdsdi3g,项目名称:MyDMAM,代码行数:35,代码来源:PartialContent.java

示例5: messageReceived

import org.jboss.netty.handler.stream.ChunkedFile; //导入依赖的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 ) ;
    }
}
 
开发者ID:wsyssantos,项目名称:BettaServer,代码行数:76,代码来源:BettaUdpFileServerHandler.java


注:本文中的org.jboss.netty.handler.stream.ChunkedFile类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。