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-2009 Sun Microsystems, Inc.
015 * Portions Copyright 2014-2015 ForgeRock AS.
016 */
017package org.opends.server.types;
018
019import org.forgerock.opendj.ldap.ByteString;
020import org.forgerock.opendj.ldap.ByteStringBuilder;
021
022import java.io.OutputStream;
023import java.io.IOException;
024
025/**
026 * A wrapper OutputStream that will record all writes to an underlying
027 * OutputStream. The recorded bytes will append to any previous
028 * recorded bytes until the clear method is called.
029 */
030public class RecordingOutputStream extends OutputStream
031{
032  private boolean enableRecording;
033  private OutputStream parentStream;
034  private ByteStringBuilder buffer;
035
036  /**
037   * Constructs a new RecordingOutputStream that will all writes to
038   * the given OutputStream.
039   *
040   * @param parentStream The output stream to record.
041   */
042  public RecordingOutputStream(OutputStream parentStream) {
043    this.enableRecording = false;
044    this.parentStream = parentStream;
045    this.buffer = new ByteStringBuilder(32);
046  }
047
048  /** {@inheritDoc} */
049  @Override
050  public void write(int i) throws IOException {
051    if(enableRecording)
052    {
053      buffer.appendByte(i);
054    }
055    parentStream.write(i);
056  }
057
058  /** {@inheritDoc} */
059  @Override
060  public void write(byte[] bytes) throws IOException {
061    if(enableRecording)
062    {
063      buffer.appendBytes(bytes);
064    }
065    parentStream.write(bytes);
066  }
067
068  /** {@inheritDoc} */
069  @Override
070  public void write(byte[] bytes, int i, int i1) throws IOException {
071    if(enableRecording)
072    {
073      buffer.appendBytes(bytes, i, i1);
074    }
075    parentStream.write(bytes, i, i1);
076  }
077
078  /** {@inheritDoc} */
079  @Override
080  public void flush() throws IOException {
081    parentStream.flush();
082  }
083
084  /** {@inheritDoc} */
085  @Override
086  public void close() throws IOException {
087    parentStream.close();
088  }
089
090  /**
091   * Retrieve the bytes read from this output stream since the last
092   * clear.
093   *
094   * @return the bytes read from this output stream since the last
095   *         clear.
096   */
097  public ByteString getRecordedBytes() {
098    return buffer.toByteString();
099  }
100
101  /**
102   * Clear the bytes currently recorded by this output stream.
103   */
104  public void clearRecordedBytes() {
105    buffer.clear();
106  }
107
108  /**
109   * Retrieves whether recording is enabled.
110   *
111   * @return whether recording is enabled.
112   */
113  public boolean isRecordingEnabled()
114  {
115    return enableRecording;
116  }
117
118  /**
119   * Set whether if this output stream is recording all reads or not.
120   *
121   * @param enabled <code>true</code> to recording all reads or
122   *                <code>false</code> otherwise.
123   */
124  public void setRecordingEnabled(boolean enabled)
125  {
126    this.enableRecording = enabled;
127  }
128}