001/* 002 * CDDL HEADER START 003 * 004 * The contents of this file are subject to the terms of the 005 * Common Development and Distribution License, Version 1.0 only 006 * (the "License"). You may not use this file except in compliance 007 * with the License. 008 * 009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt 010 * or http://forgerock.org/license/CDDLv1.0.html. 011 * See the License for the specific language governing permissions 012 * and limitations under the License. 013 * 014 * When distributing Covered Code, include this CDDL HEADER in each 015 * file and include the License file at legal-notices/CDDLv1_0.txt. 016 * If applicable, add the following below this CDDL HEADER, with the 017 * fields enclosed by brackets "[]" replaced with your own identifying 018 * information: 019 * Portions Copyright [yyyy] [name of copyright owner] 020 * 021 * CDDL HEADER END 022 * 023 * 024 * Copyright 2006-2009 Sun Microsystems, Inc. 025 * Portions Copyright 2014-2015 ForgeRock AS 026 */ 027package org.opends.server.types; 028 029import org.forgerock.opendj.ldap.ByteString; 030import org.forgerock.opendj.ldap.ByteStringBuilder; 031 032import java.io.OutputStream; 033import java.io.IOException; 034 035/** 036 * A wrapper OutputStream that will record all writes to an underlying 037 * OutputStream. The recorded bytes will append to any previous 038 * recorded bytes until the clear method is called. 039 */ 040public class RecordingOutputStream extends OutputStream 041{ 042 private boolean enableRecording; 043 private OutputStream parentStream; 044 private ByteStringBuilder buffer; 045 046 /** 047 * Constructs a new RecordingOutputStream that will all writes to 048 * the given OutputStream. 049 * 050 * @param parentStream The output stream to record. 051 */ 052 public RecordingOutputStream(OutputStream parentStream) { 053 this.enableRecording = false; 054 this.parentStream = parentStream; 055 this.buffer = new ByteStringBuilder(32); 056 } 057 058 /** {@inheritDoc} */ 059 public void write(int i) throws IOException { 060 if(enableRecording) 061 { 062 buffer.append((byte) i); 063 } 064 parentStream.write(i); 065 } 066 067 /** {@inheritDoc} */ 068 @Override 069 public void write(byte[] bytes) throws IOException { 070 if(enableRecording) 071 { 072 buffer.append(bytes); 073 } 074 parentStream.write(bytes); 075 } 076 077 /** {@inheritDoc} */ 078 @Override 079 public void write(byte[] bytes, int i, int i1) throws IOException { 080 if(enableRecording) 081 { 082 buffer.append(bytes, i, i1); 083 } 084 parentStream.write(bytes, i, i1); 085 } 086 087 /** {@inheritDoc} */ 088 @Override 089 public void flush() throws IOException { 090 parentStream.flush(); 091 } 092 093 /** {@inheritDoc} */ 094 @Override 095 public void close() throws IOException { 096 parentStream.close(); 097 } 098 099 /** 100 * Retrieve the bytes read from this output stream since the last 101 * clear. 102 * 103 * @return the bytes read from this output stream since the last 104 * clear. 105 */ 106 public ByteString getRecordedBytes() { 107 return buffer.toByteString(); 108 } 109 110 /** 111 * Clear the bytes currently recorded by this output stream. 112 */ 113 public void clearRecordedBytes() { 114 buffer.clear(); 115 } 116 117 /** 118 * Retrieves whether recording is enabled. 119 * 120 * @return whether recording is enabled. 121 */ 122 public boolean isRecordingEnabled() 123 { 124 return enableRecording; 125 } 126 127 /** 128 * Set whether if this output stream is recording all reads or not. 129 * 130 * @param enabled <code>true</code> to recording all reads or 131 * <code>false</code> otherwise. 132 */ 133 public void setRecordingEnabled(boolean enabled) 134 { 135 this.enableRecording = enabled; 136 } 137}