View Javadoc
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  
12  import java.io.IOException;
13  import java.io.Writer;
14  import java.security.AccessController;
15  import java.security.PrivilegedAction;
16  
17  import org.eclipse.jgit.util.SystemReader;
18  
19  /**
20   * An alternative PrintWriter that doesn't catch exceptions.
21   *
22   * @since 2.2
23   */
24  public class ThrowingPrintWriter extends Writer {
25  
26  	private final Writer out;
27  
28  	private final String LF;
29  
30  	/**
31  	 * Construct a JGitPrintWriter
32  	 *
33  	 * @param out
34  	 *            the underlying {@link java.io.Writer}
35  	 */
36  	public ThrowingPrintWriter(Writer out) {
37  		this.out = out;
38  		LF = AccessController
39  				.doPrivileged((PrivilegedAction<String>) () -> SystemReader
40  						.getInstance().getProperty("line.separator") //$NON-NLS-1$
41  				);
42  	}
43  
44  	/** {@inheritDoc} */
45  	@Override
46  	public void write(char[] cbuf, int off, int len) throws IOException {
47  		out.write(cbuf, off, len);
48  	}
49  
50  	/** {@inheritDoc} */
51  	@Override
52  	public void flush() throws IOException {
53  		out.flush();
54  	}
55  
56  	/** {@inheritDoc} */
57  	@Override
58  	public void close() throws IOException {
59  		out.close();
60  	}
61  
62  	/**
63  	 * Print a string and terminate with a line feed.
64  	 *
65  	 * @param s a {@link java.lang.String} object.
66  	 * @throws java.io.IOException
67  	 */
68  	public void println(String s) throws IOException {
69  		print(s + LF);
70  	}
71  
72  	/**
73  	 * Print a platform dependent new line
74  	 *
75  	 * @throws java.io.IOException
76  	 */
77  	public void println() throws IOException {
78  		print(LF);
79  	}
80  
81  	/**
82  	 * Print a char
83  	 *
84  	 * @param value a char.
85  	 * @throws java.io.IOException
86  	 */
87  	public void print(char value) throws IOException {
88  		print(String.valueOf(value));
89  	}
90  
91  	/**
92  	 * Print an int as string
93  	 *
94  	 * @param value
95  	 *            an int.
96  	 * @throws java.io.IOException
97  	 */
98  	public void print(int value) throws IOException {
99  		print(String.valueOf(value));
100 	}
101 
102 	/**
103 	 * Print a long as string
104 	 *
105 	 * @param value a long.
106 	 * @throws java.io.IOException
107 	 */
108 	public void print(long value) throws IOException {
109 		print(String.valueOf(value));
110 	}
111 
112 	/**
113 	 * Print a short as string
114 	 *
115 	 * @param value a short.
116 	 * @throws java.io.IOException
117 	 */
118 	public void print(short value) throws IOException {
119 		print(String.valueOf(value));
120 	}
121 
122 	/**
123 	 * Print a formatted message according to
124 	 * {@link java.lang.String#format(String, Object...)}.
125 	 *
126 	 * @param fmt
127 	 *            a {@link java.lang.String} object.
128 	 * @param args
129 	 *            objects.
130 	 * @throws java.io.IOException
131 	 */
132 	public void format(String fmt, Object... args) throws IOException {
133 		print(String.format(fmt, args));
134 	}
135 
136 	/**
137 	 * Print an object's toString representations
138 	 *
139 	 * @param any
140 	 *            an object.
141 	 * @throws java.io.IOException
142 	 */
143 	public void print(Object any) throws IOException {
144 		out.write(String.valueOf(any));
145 	}
146 }