Code Pretty Print Script

Tuesday, November 12, 2013

Need to write the same content to multiple OutputStream(s)?

Are you tired of writing the same content to multiple OutputStream(s)? Do you wish there was an equivalent to the tee command in Java?
/*
 * Copyright © 2013 - Elliott Frisch
 * 
 * THIS SOFTWARE IS PROVIDED UNDER THE CREATIVE COMMONS
 * LICENSE 3.0 "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
 * EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR
 * A PARTICULAR PURPOSE.
 * 
 * To use this software you must agree to the complete
 * license terms available at:
 * http://creativecommons.org/licenses/by/3.0/us/deed.en_US
 * 
 * It is the intent of the author(s) that you may use or
 * modify this software for any purpose (including your own
 * commercial gain) provided that this notice remains in its
 * entirety.
 * 
 * Created by Elliott Frisch - www.frischcode.com
 */
package com.frischcode.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Rationale: You have multiple OutputStream(s) and
 * you want to write the same message to all of them. This
 * is especially useful for debugging and was inspired by
 * the tee
 * command.
 * 
 * @author Elliott Frisch
 */
public class OutputStreamMultiplier extends
    OutputStream {
  /**
   * Demonstrates the usage of OutputStreamMultiplier.
   * 
   * @param args
   *          ignored
   */
  public static void main(String[] args) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    OutputStreamMultiplier osm = new OutputStreamMultiplier(
        System.out, baos);
    String message = "Hello, World!\n";
    try {
      osm.write(message.getBytes());
      osm.flush();
      String result = new String(baos.toByteArray());
      if (!result.equals(message)) {
        System.out.println("Received - " + result);
      }
      if (result == message) {
        System.out.println("References ARE the same.");
      } else {
        System.out.println("References differ.");
      }
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      try {
        osm.close();
      } catch (Exception ignored) {
      }
    }
  }

  /**
   * Creates an empty set of OutputStream(s) to multiply.
   */
  public OutputStreamMultiplier() {
    super();
  }

  /**
   * Constructs an OutputStreamMultiplier with
   * OutputStream(s) to multiply.
   * 
   * @param osArr
   *          The OutputStream(s) to write to.
   */
  public OutputStreamMultiplier(OutputStream... osArr) {
    super();
    addStream(osArr);
  }

  /**
   * Adds all of the OutputStream(s) passed in to the list
   * (of streams to multiply).
   * 
   * @param osArr
   *          The OutputStream(s) to write to.
   */
  public void addStream(OutputStream... osArr) {
    if (osArr != null) {
      for (OutputStream os : osArr) {
        if (os == null || streams.contains(os)) {
          continue;
        }
        streams.add(os);
      }
    }
  }

  /**
   * First, close all of the OutputStream(s) pointed to by
   * this Multiplier and clear the list.
   */
  public void clear() {
    try {
      close();
    } catch (IOException ignored) {
    }
    streams.clear();
  }

  /**
   * Attempt(s) to write b (the byte) to all of the
   * OutputStream(s) pointed to by this Multiplier.
   * 
   * @see java.io.OutputStream#write(int)
   */
  @Override
  public void write(int b) throws IOException {
    if (streams == null || streams.size() == 0) {
      return;
    }
    for (final OutputStream os : streams) {
      if (os == null) {
        continue;
      }
      try {
        os.write(b);
      } catch (IOException ignored) {
      }
    }
  }

  /**
   * Attempt(s) to flush all of the OutputStream(s) pointed
   * to by this Multiplier.
   * 
   * @see java.io.OutputStream#flush()
   */
  @Override
  public void flush() throws IOException {
    if (streams == null || streams.size() == 0) {
      return;
    }
    for (final OutputStream os : streams) {
      if (os == null) {
        continue;
      }
      try {
        os.flush();
      } catch (IOException ignored) {
      }
    }
  }

  /**
   * Attempt(s) to close all of the OutputStream(s) pointed
   * to by this Multiplier.
   * 
   * @see java.io.OutputStream#close()
   */
  @Override
  public void close() throws IOException {
    if (streams == null || streams.size() == 0) {
      return;
    }
    for (final OutputStream os : streams) {
      if (os == null) {
        continue;
      }
      try {
        os.close();
      } catch (IOException ignored) {
      }
    }
  }

  protected List streams = //
    new ArrayList();
}

No comments :

Post a Comment