Home | History | Annotate | Download | only in impl
      1 // =================================================================================================
      2 // ADOBE SYSTEMS INCORPORATED
      3 // Copyright 2006 Adobe Systems Incorporated
      4 // All Rights Reserved
      5 //
      6 // NOTICE:  Adobe permits you to use, modify, and distribute this file in accordance with the terms
      7 // of the Adobe license agreement accompanying it.
      8 // =================================================================================================
      9 
     10 package com.adobe.xmp.impl;
     11 
     12 import java.io.IOException;
     13 import java.io.OutputStream;
     14 
     15 
     16 /**
     17  * An <code>OutputStream</code> that counts the written bytes.
     18  *
     19  * @since   08.11.2006
     20  */
     21 public final class CountOutputStream extends OutputStream
     22 {
     23 	/** the decorated output stream */
     24 	private final OutputStream out;
     25 	/** the byte counter */
     26 	private int bytesWritten = 0;
     27 
     28 
     29 	/**
     30 	 * Constructor with providing the output stream to decorate.
     31 	 * @param out an <code>OutputStream</code>
     32 	 */
     33 	CountOutputStream(OutputStream out)
     34 	{
     35 		this.out = out;
     36 	}
     37 
     38 
     39 	/**
     40 	 * Counts the written bytes.
     41 	 * @see java.io.OutputStream#write(byte[], int, int)
     42 	 */
     43 	public void write(byte[] buf, int off, int len) throws IOException
     44 	{
     45 		out.write(buf, off, len);
     46 		bytesWritten += len;
     47 	}
     48 
     49 
     50 	/**
     51 	 * Counts the written bytes.
     52 	 * @see java.io.OutputStream#write(byte[])
     53 	 */
     54 	public void write(byte[] buf) throws IOException
     55 	{
     56 		out.write(buf);
     57 		bytesWritten += buf.length;
     58 	}
     59 
     60 
     61 	/**
     62 	 * Counts the written bytes.
     63 	 * @see java.io.OutputStream#write(int)
     64 	 */
     65 	public void write(int b) throws IOException
     66 	{
     67 		out.write(b);
     68 		bytesWritten++;
     69 	}
     70 
     71 
     72 	/**
     73 	 * @return the bytesWritten
     74 	 */
     75 	public int getBytesWritten()
     76 	{
     77 		return bytesWritten;
     78 	}
     79 }