www.pudn.com > jnp-src.rar > ByteArrayOutputTest.java


/*
 * Java Network Programming, Second Edition
 * Merlin Hughes, Michael Shoffner, Derek Hamner
 * Manning Publications Company; ISBN 188477749X
 *
 * http://nitric.com/jnp/
 *
 * Copyright (c) 1997-1999 Merlin Hughes, Michael Shoffner, Derek Hamner;
 * all rights reserved; see license.txt for details.
 */

import java.io.*; 
 
public class ByteArrayOutputTest { 
  static public void main (String[] args) throws IOException { 
    ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream (); 
    byte[] buffer = new byte[16]; 
    int numberRead; 
    while ((numberRead = System.in.read (buffer)) > -1) 
      byteArrayOut.write (buffer, 0, numberRead); 
    System.out.println ("Read " + byteArrayOut.size () + " bytes."); 
    System.out.write (byteArrayOut.toByteArray ()); 
  
    PrintStream printStream = new PrintStream (byteArrayOut); 
    for (int i = 0; i < args.length; ++ i) { 
      printStream.println ("Written to " + args[i] + "."); 
      FileOutputStream fileOut = new FileOutputStream (args[i]); 
      byteArrayOut.writeTo (fileOut); 
      fileOut.close (); 
    } 
  } 
}