FileDocCategorySizeDatePackage
LoopPrinter1.javaAPI DocExample2615Thu Feb 04 16:10:42 GMT 1999None

LoopPrinter1.java

/*
 *
 * Copyright (c) 1997-1999 Scott Oaks and Henry Wong. All Rights Reserved.
 *
 * Permission to use, copy, modify, and distribute this software
 * and its documentation for NON-COMMERCIAL purposes and
 * without fee is hereby granted.
 *
 * This sample source code is provided for example only,
 * on an unsupported, as-is basis. 
 *
 * AUTHOR MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
 * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. AUTHOR SHALL NOT BE LIABLE FOR
 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 *
 * THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
 * CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
 * PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
 * NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
 * SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
 * SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
 * PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES").  AUTHOR
 * SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
 * HIGH RISK ACTIVITIES.
 */


import java.util.*;
import java.io.*;

public class LoopPrinter1 {
	private Vector pStorage[];
	private int growSize;

	public LoopPrinter1(int initSize, int growSize) {
		pStorage = new Vector[initSize];
		this.growSize = growSize;
	}

	public LoopPrinter1() {
		this(100, 0);
	}

	private synchronized void enlargeStorage(int minSize) {
		int oldSize = pStorage.length;
		if (oldSize < minSize) {
			int newSize = (growSize > 0) ?
				oldSize + growSize : 2 * oldSize;
			if (newSize < minSize) {
				newSize = minSize;
			}	
			Vector newVec[] = new Vector[newSize];
			System.arraycopy(pStorage, 0, newVec, 0, oldSize);
			pStorage = newVec;
		}
	}
 
	public synchronized void print(int index, Object obj) {
		if (index >= pStorage.length) {
			enlargeStorage(index+1);
		}
		if (pStorage[index] == null) {
			pStorage[index] = new Vector();
		}
		pStorage[index].addElement(obj.toString());
	}
 
	public synchronized void println(int index, Object obj) {
		print(index, obj);
		print(index, "\n");
	}
 
	public synchronized void send2stream(PrintStream ps) {
		for (int i = 0; i < pStorage.length; i++) {
			if (pStorage[i] != null) {
				Enumeration e = pStorage[i].elements();
				while (e.hasMoreElements()) {
					ps.print(e.nextElement());
				}
			}	
		}
	}
}