java nio之通道和Gather技术

简介: 学到这一步,就可以和操作系统的一些IO调用结合起来了。 感觉到设计系统和语言的人是多么的牛X。 能考虑到的细节是多么的复杂!

学到这一步,就可以和操作系统的一些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);
	}
	
	

}


目录
相关文章
|
6天前
|
监控 Java 物联网
Java串口通信技术探究1:深入理解RXTX库
Java串口通信技术探究1:深入理解RXTX库
20 2
|
6天前
|
存储 缓存 前端开发
Java串口通信技术探究3:RXTX库线程 优化系统性能的SerialPortEventListener类
Java串口通信技术探究3:RXTX库线程 优化系统性能的SerialPortEventListener类
24 3
|
6天前
|
安全 IDE Java
Java串口通信技术探究2:RXTX库单例测试及应用
Java串口通信技术探究2:RXTX库单例测试及应用
24 4
|
6天前
|
存储 前端开发 安全
13:会话跟踪技术Session的深度应用与实践-Java Web
13:会话跟踪技术Session的深度应用与实践-Java Web
22 3
|
6天前
|
存储 前端开发 搜索推荐
12:会话跟踪技术Cookie的深度应用与实践-Java Web
12:会话跟踪技术Cookie的深度应用与实践-Java Web
20 4
|
8天前
|
供应链 Java API
Java 8新特性解析及应用区块链技术在供应链管理中的应用与挑战
【4月更文挑战第30天】本文将深入探讨Java 8的新特性,包括Lambda表达式、Stream API和Optional类等。通过对这些新特性的详细解析和应用实例,帮助读者更好地理解和掌握Java 8的新技术。
|
9天前
|
Java 关系型数据库 MySQL
【JDBC编程】基于MySql的Java应用程序中访问数据库与交互数据的技术
【JDBC编程】基于MySql的Java应用程序中访问数据库与交互数据的技术
|
14天前
|
缓存 Java 编译器
第一章 Java线程池技术应用
第一章 Java线程池技术应用
18 0
|
14天前
|
负载均衡 Java 数据库连接
Java从入门到精通:4.2.2学习新技术与框架——不断扩展自己的知识面,跟上技术的发展趋势
Java从入门到精通:4.2.2学习新技术与框架——不断扩展自己的知识面,跟上技术的发展趋势
|
14天前
|
SQL Java 数据库连接
Java从入门到精通:3.1.2深入学习Java EE技术——Hibernate与MyBatis等ORM框架的掌握
Java从入门到精通:3.1.2深入学习Java EE技术——Hibernate与MyBatis等ORM框架的掌握