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 public int read() throws IOException { 061 int readByte = parentStream.read(); 062 if(enableRecording) 063 { 064 buffer.append((byte)readByte); 065 } 066 return readByte; 067 } 068 069 /** {@inheritDoc} */ 070 @Override 071 public int read(byte[] bytes) throws IOException { 072 int bytesRead = parentStream.read(bytes); 073 if(enableRecording) 074 { 075 buffer.append(bytes, 0, bytesRead); 076 } 077 return bytesRead; 078 } 079 080 /** {@inheritDoc} */ 081 @Override 082 public int read(byte[] bytes, int i, int i1) throws IOException { 083 int bytesRead = parentStream.read(bytes, i, i1); 084 if(enableRecording) 085 { 086 buffer.append(bytes, i, bytesRead); 087 } 088 return bytesRead; 089 } 090 091 /** {@inheritDoc} */ 092 @Override 093 public long skip(long l) throws IOException { 094 return parentStream.skip(l); 095 } 096 097 /** {@inheritDoc} */ 098 @Override 099 public int available() throws IOException { 100 return parentStream.available(); 101 } 102 103 /** {@inheritDoc} */ 104 @Override 105 public void close() throws IOException { 106 parentStream.close(); 107 } 108 109 /** {@inheritDoc} */ 110 @Override 111 public void mark(int i) { 112 parentStream.mark(i); 113 } 114 115 /** {@inheritDoc} */ 116 @Override 117 public void reset() throws IOException { 118 parentStream.reset(); 119 } 120 121 /** {@inheritDoc} */ 122 @Override 123 public boolean markSupported() { 124 return parentStream.markSupported(); 125 } 126 127 /** 128 * Retrieve the bytes read from this input stream since the last 129 * clear. 130 * 131 * @return the bytes read from this input stream since the last 132 * clear. 133 */ 134 public ByteString getRecordedBytes() { 135 return buffer.toByteString(); 136 } 137 138 /** 139 * Clear the bytes currently recorded by this input stream. 140 */ 141 public void clearRecordedBytes() { 142 buffer.clear(); 143 } 144 145 /** 146 * Retrieves whether recording is enabled. 147 * 148 * @return whether recording is enabled. 149 */ 150 public boolean isRecordingEnabled() 151 { 152 return enableRecording; 153 } 154 155 /** 156 * Set whether if this input stream is recording all reads or not. 157 * 158 * @param enabled <code>true</code> to recording all reads or 159 * <code>false</code> otherwise. 160 */ 161 public void setRecordingEnabled(boolean enabled) 162 { 163 this.enableRecording = enabled; 164 } 165}