FileDocCategorySizeDatePackage
Marketing.javaAPI DocExample2933Mon May 20 00:24:28 BST 2002com.ronsoft.books.nio.channels

Marketing

public class Marketing extends Object
Demonstrate gathering write using many buffers. Created April, 2002
author
Ron Hitchens (ron@ronsoft.com)
version
$Id: Marketing.java,v 1.4 2002/05/20 07:24:29 ron Exp $

Fields Summary
private static final String
DEMOGRAPHIC
private static String[]
col1
private static String[]
col2
private static String[]
col3
private static String
newline
private static Random
rand
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] argv)


	// "Leverage frictionless methodologies."
	      
		 
	
		int reps = 10;

		if (argv.length > 0) {
			reps = Integer.parseInt (argv [0]);
		}

		FileOutputStream fos = new FileOutputStream (DEMOGRAPHIC);
		GatheringByteChannel gatherChannel = fos.getChannel();

		// generate some brilliant marcom, er, repurposed content
		ByteBuffer [] bs = utterBS (reps);

		// deliver the message to the waiting market
		while (gatherChannel.write (bs) > 0) {
			// empty body
			// loop until write() returns zero
		}

		System.out.println ("Mindshare paradigms synergized to "
			+ DEMOGRAPHIC);

		fos.close();
	
private static java.nio.ByteBufferpickRandom(java.lang.String[] strings, java.lang.String suffix)


	// Pick one, make a buffer to hold it plus the suffix, load it with
	// the byte equivalent of the strings (will not work properly for
	// non-Latin characters), then flip the loaded buffer so it's ready
	// to be drained.
	        
		 
	
		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);
	
private static java.nio.ByteBuffer[]utterBS(int howMany)


	// The Marcom-atic 9000
	      
		 
	
		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);