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.InputStream;
033import java.io.IOException;
034
035/**
036 * A wrapper InputStream that will record all reads from an underlying
037 * InputStream. The recorded bytes will append to any previous
038 * recorded bytes until the clear method is called.
039 */
040public class RecordingInputStream extends InputStream
041{
042  private boolean enableRecording;
043  private InputStream parentStream;
044  private ByteStringBuilder buffer;
045
046  /**
047   * Constructs a new RecordingInputStream that will record all reads
048   * from the given input stream.
049   *
050   * @param parentStream The input stream to record.
051   */
052  public RecordingInputStream(InputStream parentStream)
053  {
054    this.enableRecording = false;
055    this.parentStream = parentStream;
056    this.buffer = new ByteStringBuilder(32);
057  }
058
059  /** {@inheritDoc} */
060  @Override
061  public int read() throws IOException {
062    int readByte = parentStream.read();
063    if(enableRecording)
064    {
065      buffer.appendByte(readByte);
066    }
067    return readByte;
068  }
069
070  /** {@inheritDoc} */
071  @Override
072  public int read(byte[] bytes) throws IOException {
073    int bytesRead = parentStream.read(bytes);
074    if(enableRecording)
075    {
076      buffer.appendBytes(bytes, 0, bytesRead);
077    }
078    return bytesRead;
079  }
080
081  /** {@inheritDoc} */
082  @Override
083  public int read(byte[] bytes, int i, int i1) throws IOException {
084    int bytesRead = parentStream.read(bytes, i, i1);
085    if(enableRecording)
086    {
087      buffer.appendBytes(bytes, i, bytesRead);
088    }
089    return bytesRead;
090  }
091
092  /** {@inheritDoc} */
093  @Override
094  public long skip(long l) throws IOException {
095    return parentStream.skip(l);
096  }
097
098  /** {@inheritDoc} */
099  @Override
100  public int available() throws IOException {
101    return parentStream.available();
102  }
103
104  /** {@inheritDoc} */
105  @Override
106  public void close() throws IOException {
107    parentStream.close();
108  }
109
110  /** {@inheritDoc} */
111  @Override
112  public void mark(int i) {
113    parentStream.mark(i);
114  }
115
116  /** {@inheritDoc} */
117  @Override
118  public void reset() throws IOException {
119    parentStream.reset();
120  }
121
122  /** {@inheritDoc} */
123  @Override
124  public boolean markSupported() {
125    return parentStream.markSupported();
126  }
127
128  /**
129   * Retrieve the bytes read from this input stream since the last
130   * clear.
131   *
132   * @return the bytes read from this input stream since the last
133   *         clear.
134   */
135  public ByteString getRecordedBytes() {
136    return buffer.toByteString();
137  }
138
139  /**
140   * Clear the bytes currently recorded by this input stream.
141   */
142  public void clearRecordedBytes() {
143    buffer.clear();
144  }
145
146  /**
147   * Retrieves whether recording is enabled.
148   *
149   * @return whether recording is enabled.
150   */
151  public boolean isRecordingEnabled()
152  {
153    return enableRecording;
154  }
155
156  /**
157   * Set whether if this input stream is recording all reads or not.
158   *
159   * @param enabled <code>true</code> to recording all reads or
160   *                <code>false</code> otherwise.
161   */
162  public void setRecordingEnabled(boolean enabled)
163  {
164    this.enableRecording = enabled;
165  }
166}