java nio之通道和Gather技术

学到这一步,就可以和操作系统的一些IO调用结合起来了。

感觉到设计系统和语言的人是多么的牛X。

能考虑到的细节是多么的复杂!

下一步,

file io和socket~~~~

看书的深度和系统性,还是一篇网上的文章无法比的!



致敬!

eeef825aeb66e906723284cf5931a5ff94d72ac8


package com.ronsoft.books.nio.channels;

import java.nio.ByteBuffer;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
import java.nio.channels.Channels;
import java.io.IOException;

import java.nio.channels.GatheringByteChannel;
import java.io.FileOutputStream;
import java.util.Random;
import java.util.List;
import java.util.LinkedList;


public class ChannelCopy {
	private static final String DEMOGRAPHIC = "blahblah.txt";

	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		/*
		ReadableByteChannel source = Channels.newChannel(System.in);
		WritableByteChannel dest = Channels.newChannel(System.out);
		channelCopy1(source, dest);
		// channelCopy2(source, dest);
		source.close();
		dest.close();
		*/
		
		int reps = 10;
		if (args.length > 0) {
			reps = Integer.parseInt(args[0]);
		}
		
		FileOutputStream fos = new FileOutputStream(DEMOGRAPHIC);
		GatheringByteChannel gatherChannel = fos.getChannel();
		ByteBuffer []bs = utterBS(reps);
		
		while (gatherChannel.write(bs) > 0) {
			
		}
		System.out.println("Mindshare paradigms synergized to " + DEMOGRAPHIC);
		fos.close();	

	}
	
	private static void channelCopy1(ReadableByteChannel src,
			WritableByteChannel dest) throws IOException{
		ByteBuffer buffer = ByteBuffer.allocate(16 * 1024);
		while (src.read(buffer) != -1) {
			buffer.flip();
			dest.write(buffer);
			buffer.compact();
		}
		buffer.flip();
		while (buffer.hasRemaining()) {
			dest.write(buffer);
		}
		
	}
	
	private static void channelCopy2(ReadableByteChannel src,
			WritableByteChannel dest) throws IOException {
		ByteBuffer buffer = ByteBuffer.allocate(10 * 1024);
		while (src.read(buffer) != -1) {
			buffer.flip();
			while (buffer.hasRemaining()) {
				dest.write(buffer);
			}
			buffer.clear();
			
		}
	}
	
	private static String []col1 = {
			"Aggregate", "Enable", "Leverage",
			"Facilitate", "Synergize", "Repurpose",
			"Strategize", "Reinvent", "Harness"
	};
	
	private static String []col2 = {
			"cross-platform", "best-of-breed", "frictionless",
			"ubiquitous", "extensible", "compelling",
			"mission-critical", "collaborative", "integrated"
	};
	
	private static String [] col3 = {
			"methodologies", "infomediaries", "platforms",
			"schemas", "mindshare", "paradigms",
			"functionalities", "web services", "infrastructures"
	};
	
	private static String newLine = System.getProperty("line.separator");
	@SuppressWarnings({ "unchecked", "rawtypes" })
	private static ByteBuffer []utterBS(int howMany) throws Exception {
		List list = new LinkedList();
		for (int i = 0; i < howMany; i++) {
			list.add(pickRandom(col1, " "));
			list.add(pickRandom(col2, " "));
			list.add(pickRandom(col3, newLine));
		}
		ByteBuffer []bufs = new ByteBuffer[list.size()];
		list.toArray(bufs);
		return (bufs);
	}
	
	private static Random rand = new Random();
	private static ByteBuffer pickRandom(String []strings, String suffix) throws Exception {
		String string = strings[rand.nextInt(strings.length)];
		int total = string.length() + suffix.length();
		ByteBuffer buf = ByteBuffer.allocate(total);
		buf.put(string.getBytes("US-ASCII"));
		buf.put(suffix.getBytes("US-ASCII"));
		buf.flip();
		return (buf);
	}
	
	

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值