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  @Override
060  public void write(int i) throws IOException {
061    if(enableRecording)
062    {
063      buffer.appendByte(i);
064    }
065    parentStream.write(i);
066  }
067
068  /** {@inheritDoc} */
069  @Override
070  public void write(byte[] bytes) throws IOException {
071    if(enableRecording)
072    {
073      buffer.appendBytes(bytes);
074    }
075    parentStream.write(bytes);
076  }
077
078  /** {@inheritDoc} */
079  @Override
080  public void write(byte[] bytes, int i, int i1) throws IOException {
081    if(enableRecording)
082    {
083      buffer.appendBytes(bytes, i, i1);
084    }
085    parentStream.write(bytes, i, i1);
086  }
087
088  /** {@inheritDoc} */
089  @Override
090  public void flush() throws IOException {
091    parentStream.flush();
092  }
093
094  /** {@inheritDoc} */
095  @Override
096  public void close() throws IOException {
097    parentStream.close();
098  }
099
100  /**
101   * Retrieve the bytes read from this output stream since the last
102   * clear.
103   *
104   * @return the bytes read from this output stream since the last
105   *         clear.
106   */
107  public ByteString getRecordedBytes() {
108    return buffer.toByteString();
109  }
110
111  /**
112   * Clear the bytes currently recorded by this output stream.
113   */
114  public void clearRecordedBytes() {
115    buffer.clear();
116  }
117
118  /**
119   * Retrieves whether recording is enabled.
120   *
121   * @return whether recording is enabled.
122   */
123  public boolean isRecordingEnabled()
124  {
125    return enableRecording;
126  }
127
128  /**
129   * Set whether if this output stream is recording all reads or not.
130   *
131   * @param enabled <code>true</code> to recording all reads or
132   *                <code>false</code> otherwise.
133   */
134  public void setRecordingEnabled(boolean enabled)
135  {
136    this.enableRecording = enabled;
137  }
138}