• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Java DefaultFileRegion类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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



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

示例1: toContent

import io.netty.channel.DefaultFileRegion; //导入依赖的package包/类
private static Object toContent(Object content) {

        if (content instanceof File) {
            File file = (File) content;
            return new DefaultFileRegion(file, 0, file.length());
        }

        if (content instanceof InputStream) {
            return new ChunkedStream((InputStream) content);
        }

        if (content instanceof ReadableByteChannel) {
            return new ChunkedNioStream((ReadableByteChannel) content);
        }

        if (content instanceof byte[]) {
            return Unpooled.wrappedBuffer((byte[]) content);
        }

        throw new IllegalArgumentException("unknown content type : " + content.getClass().getName());
    }
 
开发者ID:rodbate,项目名称:fastdfs-spring-boot,代码行数:22,代码来源:FileOperationEncoder.java


示例2: toContent

import io.netty.channel.DefaultFileRegion; //导入依赖的package包/类
private static Object toContent(Object content) {

        if (content instanceof File) {
            File file = (File) content;
            return new DefaultFileRegion(file, 0, file.length());
        }

        if (content instanceof InputStream) {
            return new ChunkedStream((InputStream) content);
        }

        if (content instanceof ReadableByteChannel) {
            return new ChunkedNioStream((ReadableByteChannel) content);
        }

        if (content instanceof byte[]) {
            return Unpooled.wrappedBuffer((byte[]) content);
        }

        throw new IllegalArgumentException(
            "unknown content type : " + content.getClass().getName());
    }
 
开发者ID:warlock-china,项目名称:azeroth,代码行数:23,代码来源:FileOperationEncoder.java


示例3: messageReceived

import io.netty.channel.DefaultFileRegion; //导入依赖的package包/类
@Override
protected void messageReceived(ChannelHandlerContext cxt, String msg)
		throws Exception {
	File file = new File(msg);
	if(file.exists()) {
		if(!file.isFile()){
			cxt.writeAndFlush("No file " + file + CR);
		}
		cxt.writeAndFlush("file " + file.length() + CR);
		RandomAccessFile randomAccessFile = new RandomAccessFile(msg, "r");
		FileRegion fileRegion = new DefaultFileRegion(randomAccessFile.getChannel(), 0, randomAccessFile.length());
		cxt.write(fileRegion);
		cxt.writeAndFlush(CR);
		randomAccessFile.close();
	}else{
		cxt.writeAndFlush("File not found: " + file + CR);
	}
}
 
开发者ID:hdcuican,项目名称:java_learn,代码行数:19,代码来源:FileServerHandler.java


示例4: channelRead0

import io.netty.channel.DefaultFileRegion; //导入依赖的package包/类
@Override
public void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
    RandomAccessFile raf = null;
    long length = -1;
    try {
        raf = new RandomAccessFile(msg, "r");
        length = raf.length();
    } catch (Exception e) {
        ctx.writeAndFlush("ERR: " + e.getClass().getSimpleName() + ": " + e.getMessage() + '\n');
        return;
    } finally {
        if (length < 0 && raf != null) {
            raf.close();
        }
    }

    ctx.write("OK: " + raf.length() + '\n');
    if (ctx.pipeline().get(SslHandler.class) == null) {
        // SSL not enabled - can use zero-copy file transfer.
        ctx.write(new DefaultFileRegion(raf.getChannel(), 0, length));
    } else {
        // SSL enabled - cannot use zero-copy file transfer.
        ctx.write(new ChunkedFile(raf));
    }
    ctx.writeAndFlush("\n");
}
 
开发者ID:cowthan,项目名称:JavaAyo,代码行数:27,代码来源:FileServerHandler.java


示例5: testEncode

import io.netty.channel.DefaultFileRegion; //导入依赖的package包/类
/**
 * This unit test case ensures that {@link FileRegionEncoder} indeed wraps {@link FileRegion} to
 * {@link ByteBuf}.
 * @throws IOException if there is an error.
 */
@Test
public void testEncode() throws IOException {
    FileRegionEncoder fileRegionEncoder = new FileRegionEncoder();
    EmbeddedChannel channel = new EmbeddedChannel(fileRegionEncoder);
    File file = File.createTempFile(UUID.randomUUID().toString(), ".data");
    file.deleteOnExit();
    Random random = new Random(System.currentTimeMillis());
    int dataLength = 1 << 10;
    byte[] data = new byte[dataLength];
    random.nextBytes(data);
    write(file, data);
    FileRegion fileRegion = new DefaultFileRegion(file, 0, dataLength);
    Assert.assertEquals(0, fileRegion.transfered());
    Assert.assertEquals(dataLength, fileRegion.count());
    Assert.assertTrue(channel.writeOutbound(fileRegion));
    ByteBuf out = (ByteBuf) channel.readOutbound();
    byte[] arr = new byte[out.readableBytes()];
    out.getBytes(0, arr);
    Assert.assertArrayEquals("Data should be identical", data, arr);
}
 
开发者ID:apache,项目名称:rocketmq,代码行数:26,代码来源:FileRegionEncoderTest.java


示例6: messageReceived

import io.netty.channel.DefaultFileRegion; //导入依赖的package包/类
public void messageReceived(ChannelHandlerContext ctx, String msg)
    throws Exception {
File file = new File(msg);
if (file.exists()) {
    if (!file.isFile()) {
	ctx.writeAndFlush("Not a file : " + file + CR);
	return;
    }
    ctx.write(file + " " + file.length() + CR);
    RandomAccessFile randomAccessFile = new RandomAccessFile(msg, "r");
    FileRegion region = new DefaultFileRegion(
	    randomAccessFile.getChannel(), 0, randomAccessFile.length());
    ctx.write(region);
    ctx.writeAndFlush(CR);
    randomAccessFile.close();
} else {
    ctx.writeAndFlush("File not found: " + file + CR);
}
   }
 
开发者ID:changyuefeng,项目名称:netty-book,代码行数:20,代码来源:FileServerHandler.java


示例7: doWriteSingle

import io.netty.channel.DefaultFileRegion; //导入依赖的package包/类
protected boolean doWriteSingle(ChannelOutboundBuffer in, int writeSpinCount) throws Exception {
    // The outbound buffer contains only one message or it contains a file region.
    Object msg = in.current();
    if (msg instanceof ByteBuf) {
        ByteBuf buf = (ByteBuf) msg;
        if (!writeBytes(in, buf, writeSpinCount)) {
            // was not able to write everything so break here we will get notified later again once
            // the network stack can handle more writes.
            return false;
        }
    } else if (msg instanceof DefaultFileRegion) {
        DefaultFileRegion region = (DefaultFileRegion) msg;
        if (!writeFileRegion(in, region, writeSpinCount)) {
            // was not able to write everything so break here we will get notified later again once
            // the network stack can handle more writes.
            return false;
        }
    } else {
        // Should never reach here.
        throw new Error();
    }

    return true;
}
 
开发者ID:wuyinxian124,项目名称:netty4.0.27Learn,代码行数:25,代码来源:AbstractEpollStreamChannel.java


示例8: messageReceived

import io.netty.channel.DefaultFileRegion; //导入依赖的package包/类
@Override
protected void messageReceived(ChannelHandlerContext ctx, String msg) throws Exception {

	File file = new File(msg);
	if (file.exists()) {
		if (!file.isFile()) {
			ctx.writeAndFlush("not a file :" + file + CR);
			return;
		}
		ctx.write(file + " " + file.length() + CR);

		RandomAccessFile raf = new RandomAccessFile(file, "r");
		FileRegion fileRegion = new DefaultFileRegion(raf.getChannel(), 0, raf.length());

		ctx.write(fileRegion);
		ctx.writeAndFlush(CR);
		raf.close();

	} else {
		ctx.writeAndFlush("file not found: " + file + CR);
	}
}
 
开发者ID:zoopaper,项目名称:netty-study,代码行数:23,代码来源:FileServerHandler.java


示例9: messageReceived

import io.netty.channel.DefaultFileRegion; //导入依赖的package包/类
public void messageReceived(ChannelHandlerContext ctx, String msg)
        throws Exception {
    File file = new File(msg);
    if (file.exists()) {
        if (!file.isFile()) {
            ctx.writeAndFlush("Not a file : " + file + CR);
            return;
        }
        ctx.write(file + " " + file.length() + CR);
        RandomAccessFile randomAccessFile = new RandomAccessFile(msg, "r");
        FileRegion region = new DefaultFileRegion(
                randomAccessFile.getChannel(), 0, randomAccessFile.length());
        ctx.write(region);
        ctx.writeAndFlush(CR);
        randomAccessFile.close();
    } else {
        ctx.writeAndFlush("File not found: " + file + CR);
    }
}
 
开发者ID:Hope6537,项目名称:hope-tactical-equipment,代码行数:20,代码来源:FileServerHandler.java


示例10: messageReceived

import io.netty.channel.DefaultFileRegion; //导入依赖的package包/类
@Override
protected void messageReceived(ChannelHandlerContext ctx, String msg) throws Exception {
    RandomAccessFile raf = null;
    long length = -1;
    try {
        raf = new RandomAccessFile(msg, "r");
        length = raf.length();
    } catch (Exception e) {
        ctx.writeAndFlush("ERR: " + e.getClass().getSimpleName() + ": " + e.getMessage() + '\n');
        return;
    } finally {
        if (length < 0 && raf != null) {
            raf.close();
        }
    }
    ctx.write("OK: " + raf.length() + '\n');
    if (ctx.pipeline().get(SslHandler.class) == null) {
        // SSL not enabled - can use zero-copy file transfer.
        ctx.write(new DefaultFileRegion(raf.getChannel(), 0, length));
    } else {
        // SSL enabled - cannot use zero-copy file transfer.
        ctx.write(new ChunkedFile(raf));
    }
    ctx.writeAndFlush("\n");
}
 
开发者ID:edgar615,项目名称:javase-study,代码行数:26,代码来源:FileServerHandler.java


示例11: channelRead0

import io.netty.channel.DefaultFileRegion; //导入依赖的package包/类
@Override
public void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
    File file = new File(msg);
    if (file.exists()) {
        if (!file.isFile()) {
            ctx.writeAndFlush("Not a file: " + file + '\n');
            return;
        }
        ctx.write(file + " " + file.length() + '\n');
        FileInputStream fis = new FileInputStream(file);
        FileRegion region = new DefaultFileRegion(fis.getChannel(), 0, file.length());
        ctx.write(region);
        ctx.writeAndFlush("\n");
        fis.close();
    } else {
        ctx.writeAndFlush("File not found: " + file + '\n');
    }
}
 
开发者ID:kyle-liu,项目名称:netty4study,代码行数:19,代码来源:FileServer.java


示例12: channelRead0

import io.netty.channel.DefaultFileRegion; //导入依赖的package包/类
@Override
protected void channelRead0(ChannelHandlerContext ctx, String msg)
		throws Exception {
	 File file = new File(msg);
           if (file.exists()) {
               if (!file.isFile()) {
                   ctx.writeAndFlush("Not a file: " + file + '\n');
                   return;
               }
               ctx.write(file + " " + file.length() + '\n');
               FileInputStream fis = new FileInputStream(file);
               FileRegion region = new DefaultFileRegion(fis.getChannel(), 0, file.length());
               ctx.write(region);
               ctx.writeAndFlush("\n");
               fis.close();
           } else {
               ctx.writeAndFlush("File not found: " + file + '\n');
           }
	
}
 
开发者ID:desperado1992,项目名称:distributeTemplate,代码行数:21,代码来源:FileServer.java


示例13: messageReceived

import io.netty.channel.DefaultFileRegion; //导入依赖的package包/类
@Override
public void messageReceived(ChannelHandlerContext ctx, String msg) throws Exception {
    File file = new File(msg);
    if (file.exists()) {
        if (!file.isFile()) {
            ctx.writeAndFlush("Not a file: " + file + '\n');
            return;
        }
        ctx.write(file + " " + file.length() + '\n');
        FileInputStream fis = new FileInputStream(file);
        FileRegion region = new DefaultFileRegion(fis.getChannel(), 0, file.length());
        ctx.write(region);
        ctx.writeAndFlush("\n");
        fis.close();
    } else {
        ctx.writeAndFlush("File not found: " + file + '\n');
    }
}
 
开发者ID:nathanchen,项目名称:netty-netty-5.0.0.Alpha1,代码行数:19,代码来源:FileServer.java


示例14: sendFile

import io.netty.channel.DefaultFileRegion; //导入依赖的package包/类
/**
 * Send response immediately for a file response
 *
 * @param raf RandomAccessFile
 */
public void sendFile(RandomAccessFile raf, long length) {

    setDate();
    setPowerBy();
    setResponseTime();
    header(CONTENT_LENGTH, Long.toString(length));

    setHttpResponse(new DefaultHttpResponse(HTTP_1_1, getStatus(), true));

    // Write initial line and headers
    channelCxt.write(httpResponse);
    // Write content
    ChannelFuture sendFileFuture;
    ChannelFuture lastContentFuture;

    if (false /* if has ssl handler */) {

        // TODO support ssl
    } else {

        sendFileFuture = channelCxt.write(new DefaultFileRegion(raf.getChannel(), 0, length), channelCxt.newProgressivePromise());
        lastContentFuture = channelCxt.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
    }

    sendFileFuture.addListener(new ProgressiveFutureListener(raf));

    if (!keepAlive) {

        lastContentFuture.addListener(ChannelFutureListener.CLOSE);
    }

    flush();
}
 
开发者ID:thundernet8,项目名称:Razor,代码行数:39,代码来源:Response.java


示例15: convertToNetty

import io.netty.channel.DefaultFileRegion; //导入依赖的package包/类
@Override
public Object convertToNetty() throws IOException {
  if (conf.lazyFileDescriptor()) {
    return new DefaultFileRegion(file, offset, length);
  } else {
    FileChannel fileChannel = new FileInputStream(file).getChannel();
    return new DefaultFileRegion(fileChannel, offset, length);
  }
}
 
开发者ID:spafka,项目名称:spark_deep,代码行数:10,代码来源:FileSegmentManagedBuffer.java


示例16: write

import io.netty.channel.DefaultFileRegion; //导入依赖的package包/类
@Override
public void write(Channel channel, NettyResponseFuture<?> future) throws IOException {
    @SuppressWarnings("resource")
    // Netty will close the ChunkedNioFile or the DefaultFileRegion
    final FileChannel fileChannel = new RandomAccessFile(file, "r").getChannel();

    Object message = (ChannelManager.isSslHandlerConfigured(channel.pipeline()) || config.isDisableZeroCopy()) ? //
    new ChunkedNioFile(fileChannel, offset, length, config.getChunkedFileChunkSize())
            : new DefaultFileRegion(fileChannel, offset, length);

    channel.write(message, channel.newProgressivePromise())//
            .addListener(new ProgressListener(future.getAsyncHandler(), future, false, getContentLength()));
    channel.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
}
 
开发者ID:amaralDaniel,项目名称:megaphone,代码行数:15,代码来源:NettyFileBody.java


示例17: writeFileRegion

import io.netty.channel.DefaultFileRegion; //导入依赖的package包/类
/**
 * Write a {@link DefaultFileRegion}
 *
 * @param region        the {@link DefaultFileRegion} from which the bytes should be written
 * @return amount       the amount of written bytes
 */
private boolean writeFileRegion(
        ChannelOutboundBuffer in, DefaultFileRegion region, int writeSpinCount) throws Exception {
    final long regionCount = region.count();
    if (region.transfered() >= regionCount) {
        in.remove();
        return true;
    }

    final long baseOffset = region.position();
    boolean done = false;
    long flushedAmount = 0;

    for (int i = writeSpinCount - 1; i >= 0; i--) {
        final long offset = region.transfered();
        final long localFlushedAmount =
                Native.sendfile(fd().intValue(), region, baseOffset, offset, regionCount - offset);
        if (localFlushedAmount == 0) {
            break;
        }

        flushedAmount += localFlushedAmount;
        if (region.transfered() >= regionCount) {
            done = true;
            break;
        }
    }

    if (flushedAmount > 0) {
        in.progress(flushedAmount);
    }

    if (done) {
        in.remove();
    } else {
        // Returned EAGAIN need to set EPOLLOUT
        setFlag(Native.EPOLLOUT);
    }
    return done;
}
 
开发者ID:wuyinxian124,项目名称:netty4.0.27Learn,代码行数:46,代码来源:AbstractEpollStreamChannel.java


示例18: sendfile

import io.netty.channel.DefaultFileRegion; //导入依赖的package包/类
public static long sendfile(
        int dest, DefaultFileRegion src, long baseOffset, long offset, long length) throws IOException {
    // Open the file-region as it may be created via the lazy constructor. This is needed as we directly access
    // the FileChannel field directly via JNI
    src.open();

    long res = sendfile0(dest, src, baseOffset, offset, length);
    if (res >= 0) {
        return res;
    }
    return ioResult("sendfile", (int) res, CONNECTION_RESET_EXCEPTION_SENDFILE);
}
 
开发者ID:wuyinxian124,项目名称:netty4.0.27Learn,代码行数:13,代码来源:Native.java


示例19: channelRead0

import io.netty.channel.DefaultFileRegion; //导入依赖的package包/类
@Override
protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) throws Exception {
	
	if( wsURI.equalsIgnoreCase(request.getUri()) ) {
		
		ctx.fireChannelRead(request.retain());
	
	} else {
		
		if( HttpHeaders.is100ContinueExpected(request) ) {
			send100Continue(ctx);
		}
		
		try (
			RandomAccessFile rFile = new RandomAccessFile(indexHTML, "r")
		) {
			HttpResponse response = new DefaultHttpResponse( request.getProtocolVersion(), HttpResponseStatus.OK );
			response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/html; charset=UTF-8");
			boolean keepAlive = HttpHeaders.isKeepAlive(request);
			if( keepAlive ) {
				response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, rFile.length());
				response.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
			}
			
			ctx.write(response);
			
			if( ctx.pipeline().get(SslHandler.class) == null ) {
				ctx.write(new DefaultFileRegion(rFile.getChannel(), 0, rFile.length()));
			} else {
				ctx.write(new ChunkedNioFile(rFile.getChannel()));
			}
			
			ChannelFuture future = ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
			
			if( !keepAlive ) {
				future.addListener(ChannelFutureListener.CLOSE);
			}
		}
	}
}
 
开发者ID:bekwam,项目名称:examples-javafx-repos1,代码行数:41,代码来源:EchoServerHttpRequestHandler.java


示例20: download

import io.netty.channel.DefaultFileRegion; //导入依赖的package包/类
@Override
public void download(@NonNull String fileName, @NonNull File file) throws Exception {
    if (!file.exists() || !file.isFile()) {
        throw new NotFoundException("Not found file: " + file.getPath());
    }

    RandomAccessFile raf        = new RandomAccessFile(file, "r");
    Long             fileLength = raf.length();
    this.contentType = StringKit.mimeType(file.getName());

    io.netty.handler.codec.http.HttpResponse httpResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
    HttpHeaders                              httpHeaders  = httpResponse.headers().add(getDefaultHeader());

    boolean keepAlive = WebContext.request().keepAlive();
    if (keepAlive) {
        httpResponse.headers().set(HttpConst.CONNECTION, KEEP_ALIVE);
    }
    httpHeaders.set(HttpConst.CONTENT_TYPE, this.contentType);
    httpHeaders.set("Content-Disposition", "attachment; filename=" + new String(fileName.getBytes("UTF-8"), "ISO8859_1"));
    httpHeaders.setInt(HttpConst.CONTENT_LENGTH, fileLength.intValue());

    // Write the initial line and the header.
    ctx.write(httpResponse);

    ChannelFuture sendFileFuture = ctx.write(new DefaultFileRegion(raf.getChannel(), 0, fileLength), ctx.newProgressivePromise());
    // Write the end marker.
    ChannelFuture lastContentFuture = ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);

    sendFileFuture.addListener(ProgressiveFutureListener.build(raf));
    // Decide whether to close the connection or not.
    if (!keepAlive) {
        lastContentFuture.addListener(ChannelFutureListener.CLOSE);
    }
    isCommit = true;
}
 
开发者ID:lets-blade,项目名称:blade,代码行数:36,代码来源:HttpResponse.java



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Java DataSourceFactory类代码示例发布时间:2022-05-21
下一篇:
Java RMIConnectorServer类代码示例发布时间:2022-05-21
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap