| 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.internal.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 | * @author Stefan Makswit |
| 20 | * @version $Revision$ |
| 21 | * @since 08.11.2006 |
| 22 | */ |
| 23 | public final class CountOutputStream extends OutputStream |
| 24 | { |
| 25 | /** the decorated output stream */ |
| 26 | private final OutputStream out; |
| 27 | /** the byte counter */ |
| 28 | private int bytesWritten = 0; |
| 29 | |
| 30 | |
| 31 | /** |
| 32 | * Constructor with providing the output stream to decorate. |
| 33 | * @param out an <code>OutputStream</code> |
| 34 | */ |
| 35 | CountOutputStream(OutputStream out) |
| 36 | { |
| 37 | this.out = out; |
| 38 | } |
| 39 | |
| 40 | |
| 41 | /** |
| 42 | * Counts the written bytes. |
| 43 | * @see java.io.OutputStream#write(byte[], int, int) |
| 44 | */ |
| 45 | public void write(byte[] buf, int off, int len) throws IOException |
| 46 | { |
| 47 | out.write(buf, off, len); |
| 48 | bytesWritten += len; |
| 49 | } |
| 50 | |
| 51 | |
| 52 | /** |
| 53 | * Counts the written bytes. |
| 54 | * @see java.io.OutputStream#write(byte[]) |
| 55 | */ |
| 56 | public void write(byte[] buf) throws IOException |
| 57 | { |
| 58 | out.write(buf); |
| 59 | bytesWritten += buf.length; |
| 60 | } |
| 61 | |
| 62 | |
| 63 | /** |
| 64 | * Counts the written bytes. |
| 65 | * @see java.io.OutputStream#write(int) |
| 66 | */ |
| 67 | public void write(int b) throws IOException |
| 68 | { |
| 69 | out.write(b); |
| 70 | bytesWritten++; |
| 71 | } |
| 72 | |
| 73 | |
| 74 | /** |
| 75 | * @return the bytesWritten |
| 76 | */ |
| 77 | public int getBytesWritten() |
| 78 | { |
| 79 | return bytesWritten; |
| 80 | } |
| 81 | } |