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-2008 Sun Microsystems, Inc.
025 *      Portions Copyright 2013 ForgeRock AS
026 */
027package org.opends.server.types;
028
029import java.io.OutputStream;
030import java.io.PrintStream;
031
032/**
033 * This class defines a custom output stream that simply discards any
034 * data written to it.
035 */
036@org.opends.server.types.PublicAPI(
037     stability=org.opends.server.types.StabilityLevel.UNCOMMITTED,
038     mayInstantiate=true,
039     mayExtend=false,
040     mayInvoke=true)
041public final class NullOutputStream
042       extends OutputStream
043{
044  /**
045   * The singleton instance for this class.
046   */
047  private static final NullOutputStream instance =
048       new NullOutputStream();
049
050
051
052  /**
053   * The singleton print stream tied to the null output stream.
054   */
055  private static final PrintStream printStream =
056       new PrintStream(instance);
057
058
059
060  /**
061   * Retrieves an instance of this null output stream.
062   *
063   * @return  An instance of this null output stream.
064   */
065  public static NullOutputStream instance()
066  {
067    return instance;
068  }
069
070
071
072  /**
073   * Retrieves a print stream using this null output stream.
074   *
075   * @return  A print stream using this null output stream.
076   */
077  public static PrintStream printStream()
078  {
079    return printStream;
080  }
081
082
083  /**
084   * Returns s wrapped into a {@link PrintStream} if is not null,
085   * {@link NullOutputStream#printStream()} otherwise.
086   *
087   * @param s
088   *          the OutputStream to wrap into a {@link PrintStream}. Can be null.
089   * @return a PrintStream wrapping s if not null,
090   *         {@link NullOutputStream#printStream()} otherwise.
091   */
092  public static PrintStream wrapOrNullStream(OutputStream s)
093  {
094    if (s != null)
095    {
096      return new PrintStream(s);
097    }
098    return NullOutputStream.printStream();
099  }
100
101
102  /**
103   * Creates a new instance of this null output stream.
104   */
105  private NullOutputStream()
106  {
107    // No implementation is required.
108  }
109
110
111
112  /**
113   * Closes the output stream.  This has no effect.
114   */
115  @Override
116  public void close()
117  {
118    // No implementation is required.
119  }
120
121
122
123  /**
124   * Flushes the output stream.  This has no effect.
125   */
126  @Override
127  public void flush()
128  {
129    // No implementation is required.
130  }
131
132
133
134  /**
135   * Writes the provided data to this output stream.  This has no
136   * effect.
137   *
138   * @param  b  The byte array containing the data to be written.
139   */
140  @Override
141  public void write(byte[] b)
142  {
143    // No implementation is required.
144  }
145
146
147
148  /**
149   * Writes the provided data to this output stream.  This has no
150   * effect.
151   *
152   * @param  b    The byte array containing the data to be written.
153   * @param  off  The offset at which the real data begins.
154   * @param  len  The number of bytes to be written.
155   */
156  @Override
157  public void write(byte[] b, int off, int len)
158  {
159    // No implementation is required.
160  }
161
162
163
164  /**
165   * Writes the provided byte to this output stream.  This has no
166   * effect.
167   *
168   * @param  b  The byte to be written.
169   */
170  @Override
171  public void write(int b)
172  {
173    // No implementation is required.
174  }
175}
176