當前位置: 首頁>>代碼示例>>Java>>正文


Java DefaultFileRegion類代碼示例

本文整理匯總了Java中io.netty.channel.DefaultFileRegion的典型用法代碼示例。如果您正苦於以下問題:Java DefaultFileRegion類的具體用法?Java DefaultFileRegion怎麽用?Java DefaultFileRegion使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


DefaultFileRegion類屬於io.netty.channel包,在下文中一共展示了DefaultFileRegion類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的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


注:本文中的io.netty.channel.DefaultFileRegion類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。