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