java nio之通道和Gather技术
学到这一步,就可以和操作系统的一些IO调用结合起来了。 感觉到设计系统和语言的人是多么的牛X。 能考虑到的细节是多么的复杂! 下一步, file io和socket~~~~ 看书的深度和系统性,还是一篇网上的文章无法比的! 致敬! 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); } }