001/* 002 * The contents of this file are subject to the terms of the Common Development and 003 * Distribution License (the License). You may not use this file except in compliance with the 004 * License. 005 * 006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the 007 * specific language governing permission and limitations under the License. 008 * 009 * When distributing Covered Software, include this CDDL Header Notice in each file and include 010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL 011 * Header, with the fields enclosed by brackets [] replaced by your own identifying 012 * information: "Portions Copyright [year] [name of copyright owner]". 013 * 014 * Copyright 2006-2008 Sun Microsystems, Inc. 015 * Portions Copyright 2014-2015 ForgeRock AS. 016 */ 017package org.opends.server.loggers; 018 019import java.io.IOException; 020import java.io.OutputStream; 021 022/** 023 * A metered stream is a subclass of OutputStream that 024 * (a) forwards all its output to a target stream 025 * (b) keeps track of how many bytes have been written. 026 */ 027public final class MeteredStream extends OutputStream 028{ 029 OutputStream out; 030 volatile long written; 031 032 /** 033 * Create the stream wrapped around the specified output 034 * stream. 035 * 036 * @param out The target output stream to keep track of. 037 * @param written The number of bytes written to the stream. 038 */ 039 public MeteredStream(OutputStream out, long written) 040 { 041 this.out = out; 042 this.written = written; 043 } 044 045 /** 046 * Write the specified byte to the stream. 047 * 048 * @param b The value to be written to the stream. 049 * 050 * @exception IOException if the write failed. 051 */ 052 public void write(int b) throws IOException 053 { 054 out.write(b); 055 written++; 056 } 057 058 /** 059 * Write the specified buffer to the stream. 060 * 061 * @param buff The value to be written to the stream. 062 * 063 * @exception IOException if the write failed. 064 */ 065 public void write(byte buff[]) throws IOException 066 { 067 out.write(buff); 068 written += buff.length; 069 } 070 071 /** 072 * Write the specified buffer to the stream. 073 * 074 * @param buff The value to be written to the stream. 075 * @param off The offset to write from. 076 * @param len The length of the buffer to write. 077 * 078 * @exception IOException if the write failed. 079 */ 080 public void write(byte buff[], int off, int len) throws IOException 081 { 082 out.write(buff,off,len); 083 written += len; 084 } 085 086 /** 087 * Flush the output stream which flushes the target output stream. 088 * 089 * @exception IOException if the flush failed. 090 */ 091 public void flush() throws IOException 092 { 093 out.flush(); 094 } 095 096 /** 097 * Close the output stream which closes the target output stream. 098 * 099 * @exception IOException if the close failed. 100 */ 101 public void close() throws IOException 102 { 103 out.close(); 104 } 105 106 /** 107 * Returns the number of bytes written in this stream. 108 * 109 * @return the number of bytes 110 */ 111 public long getBytesWritten() 112 { 113 return written; 114 } 115} 116