ThrowingPrintWriter.java

  1. /*
  2.  * Copyright (C) 2012, Robin Rosenberg <robin.rosenberg@dewire.com> and others
  3.  *
  4.  * This program and the accompanying materials are made available under the
  5.  * terms of the Eclipse Distribution License v. 1.0 which is available at
  6.  * https://www.eclipse.org/org/documents/edl-v10.php.
  7.  *
  8.  * SPDX-License-Identifier: BSD-3-Clause
  9.  */
  10. package org.eclipse.jgit.util.io;

  11. import java.io.IOException;
  12. import java.io.Writer;
  13. import java.security.AccessController;
  14. import java.security.PrivilegedAction;

  15. import org.eclipse.jgit.util.SystemReader;

  16. /**
  17.  * An alternative PrintWriter that doesn't catch exceptions.
  18.  *
  19.  * @since 2.2
  20.  */
  21. public class ThrowingPrintWriter extends Writer {

  22.     private final Writer out;

  23.     private final String LF;

  24.     /**
  25.      * Construct a JGitPrintWriter
  26.      *
  27.      * @param out
  28.      *            the underlying {@link java.io.Writer}
  29.      */
  30.     public ThrowingPrintWriter(Writer out) {
  31.         this.out = out;
  32.         LF = AccessController
  33.                 .doPrivileged((PrivilegedAction<String>) () -> SystemReader
  34.                         .getInstance().getProperty("line.separator") //$NON-NLS-1$
  35.                 );
  36.     }

  37.     /** {@inheritDoc} */
  38.     @Override
  39.     public void write(char[] cbuf, int off, int len) throws IOException {
  40.         out.write(cbuf, off, len);
  41.     }

  42.     /** {@inheritDoc} */
  43.     @Override
  44.     public void flush() throws IOException {
  45.         out.flush();
  46.     }

  47.     /** {@inheritDoc} */
  48.     @Override
  49.     public void close() throws IOException {
  50.         out.close();
  51.     }

  52.     /**
  53.      * Print a string and terminate with a line feed.
  54.      *
  55.      * @param s a {@link java.lang.String} object.
  56.      * @throws java.io.IOException
  57.      */
  58.     public void println(String s) throws IOException {
  59.         print(s + LF);
  60.     }

  61.     /**
  62.      * Print a platform dependent new line
  63.      *
  64.      * @throws java.io.IOException
  65.      */
  66.     public void println() throws IOException {
  67.         print(LF);
  68.     }

  69.     /**
  70.      * Print a char
  71.      *
  72.      * @param value a char.
  73.      * @throws java.io.IOException
  74.      */
  75.     public void print(char value) throws IOException {
  76.         print(String.valueOf(value));
  77.     }

  78.     /**
  79.      * Print an int as string
  80.      *
  81.      * @param value
  82.      *            an int.
  83.      * @throws java.io.IOException
  84.      */
  85.     public void print(int value) throws IOException {
  86.         print(String.valueOf(value));
  87.     }

  88.     /**
  89.      * Print a long as string
  90.      *
  91.      * @param value a long.
  92.      * @throws java.io.IOException
  93.      */
  94.     public void print(long value) throws IOException {
  95.         print(String.valueOf(value));
  96.     }

  97.     /**
  98.      * Print a short as string
  99.      *
  100.      * @param value a short.
  101.      * @throws java.io.IOException
  102.      */
  103.     public void print(short value) throws IOException {
  104.         print(String.valueOf(value));
  105.     }

  106.     /**
  107.      * Print a formatted message according to
  108.      * {@link java.lang.String#format(String, Object...)}.
  109.      *
  110.      * @param fmt
  111.      *            a {@link java.lang.String} object.
  112.      * @param args
  113.      *            objects.
  114.      * @throws java.io.IOException
  115.      */
  116.     public void format(String fmt, Object... args) throws IOException {
  117.         print(String.format(fmt, args));
  118.     }

  119.     /**
  120.      * Print an object's toString representations
  121.      *
  122.      * @param any
  123.      *            an object.
  124.      * @throws java.io.IOException
  125.      */
  126.     public void print(Object any) throws IOException {
  127.         out.write(String.valueOf(any));
  128.     }
  129. }