Skip to content

Commit 22487c3

Browse files
author
Gary Gregory
committed
Sort members.
1 parent 9373a18 commit 22487c3

1 file changed

Lines changed: 123 additions & 123 deletions

File tree

src/main/java/org/apache/commons/io/output/FileWriterWithEncoding.java

Lines changed: 123 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -54,85 +54,93 @@ public class FileWriterWithEncoding extends Writer {
5454
// Cannot extend ProxyWriter, as requires writer to be
5555
// known when super() is called
5656

57-
/** The writer to decorate. */
58-
private final Writer out;
59-
6057
/**
61-
* Constructs a FileWriterWithEncoding with a file encoding.
58+
* Initializes the wrapped file writer. Ensure that a cleanup occurs if the writer creation fails.
6259
*
63-
* @param fileName the name of the file to write to, not null
64-
* @param charsetName the name of the requested charset, not null
65-
* @throws NullPointerException if the file name or encoding is null
66-
* @throws IOException in case of an I/O error
60+
* @param file the file to be accessed
61+
* @param encoding the encoding to use - may be Charset, CharsetEncoder or String, null uses the default Charset.
62+
* @param append true to append
63+
* @return the initialized writer
64+
* @throws IOException if an error occurs
6765
*/
68-
public FileWriterWithEncoding(final String fileName, final String charsetName) throws IOException {
69-
this(new File(fileName), charsetName, false);
66+
private static Writer initWriter(final File file, final Object encoding, final boolean append) throws IOException {
67+
Objects.requireNonNull(file, "file");
68+
OutputStream stream = null;
69+
final boolean fileExistedAlready = file.exists();
70+
try {
71+
stream = Files.newOutputStream(file.toPath(), append ? StandardOpenOption.APPEND : StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
72+
if (encoding == null || encoding instanceof Charset) {
73+
return new OutputStreamWriter(stream, Charsets.toCharset((Charset) encoding));
74+
}
75+
if (encoding instanceof CharsetEncoder) {
76+
return new OutputStreamWriter(stream, (CharsetEncoder) encoding);
77+
}
78+
return new OutputStreamWriter(stream, (String) encoding);
79+
} catch (final IOException | RuntimeException ex) {
80+
try {
81+
IOUtils.close(stream);
82+
} catch (final IOException e) {
83+
ex.addSuppressed(e);
84+
}
85+
if (!fileExistedAlready) {
86+
FileUtils.deleteQuietly(file);
87+
}
88+
throw ex;
89+
}
7090
}
7191

72-
/**
73-
* Constructs a FileWriterWithEncoding with a file encoding.
74-
*
75-
* @param fileName the name of the file to write to, not null
76-
* @param charsetName the name of the requested charset, not null
77-
* @param append true if content should be appended, false to overwrite
78-
* @throws NullPointerException if the file name or encoding is null
79-
* @throws IOException in case of an I/O error
80-
*/
81-
public FileWriterWithEncoding(final String fileName, final String charsetName, final boolean append)
82-
throws IOException {
83-
this(new File(fileName), charsetName, append);
84-
}
92+
/** The writer to decorate. */
93+
private final Writer out;
8594

8695
/**
8796
* Constructs a FileWriterWithEncoding with a file encoding.
8897
*
89-
* @param fileName the name of the file to write to, not null
90-
* @param charset the charset to use, not null
91-
* @throws NullPointerException if the file name or encoding is null
98+
* @param file the file to write to, not null
99+
* @param charset the encoding to use, not null
100+
* @throws NullPointerException if the file or encoding is null
92101
* @throws IOException in case of an I/O error
93102
*/
94-
public FileWriterWithEncoding(final String fileName, final Charset charset) throws IOException {
95-
this(new File(fileName), charset, false);
103+
public FileWriterWithEncoding(final File file, final Charset charset) throws IOException {
104+
this(file, charset, false);
96105
}
97106

98107
/**
99108
* Constructs a FileWriterWithEncoding with a file encoding.
100109
*
101-
* @param fileName the name of the file to write to, not null
102-
* @param charset the encoding to use, not null
103-
* @param append true if content should be appended, false to overwrite
104-
* @throws NullPointerException if the file name or encoding is null
105-
* @throws IOException in case of an I/O error
110+
* @param file the file to write to, not null.
111+
* @param encoding the name of the requested charset, null uses the default Charset.
112+
* @param append true if content should be appended, false to overwrite.
113+
* @throws NullPointerException if the file is null.
114+
* @throws IOException in case of an I/O error.
106115
*/
107-
public FileWriterWithEncoding(final String fileName, final Charset charset, final boolean append)
108-
throws IOException {
109-
this(new File(fileName), charset, append);
116+
public FileWriterWithEncoding(final File file, final Charset encoding, final boolean append) throws IOException {
117+
this.out = initWriter(file, encoding, append);
110118
}
111119

112120
/**
113121
* Constructs a FileWriterWithEncoding with a file encoding.
114122
*
115-
* @param fileName the name of the file to write to, not null
116-
* @param encoding the encoding to use, not null
117-
* @throws NullPointerException if the file name or encoding is null
123+
* @param file the file to write to, not null
124+
* @param charsetEncoder the encoding to use, not null
125+
* @throws NullPointerException if the file or encoding is null
118126
* @throws IOException in case of an I/O error
119127
*/
120-
public FileWriterWithEncoding(final String fileName, final CharsetEncoder encoding) throws IOException {
121-
this(new File(fileName), encoding, false);
128+
public FileWriterWithEncoding(final File file, final CharsetEncoder charsetEncoder) throws IOException {
129+
this(file, charsetEncoder, false);
122130
}
123131

124132
/**
125133
* Constructs a FileWriterWithEncoding with a file encoding.
126134
*
127-
* @param fileName the name of the file to write to, not null
128-
* @param charsetEncoder the encoding to use, not null
129-
* @param append true if content should be appended, false to overwrite
130-
* @throws NullPointerException if the file name or encoding is null
131-
* @throws IOException in case of an I/O error
135+
* @param file the file to write to, not null.
136+
* @param charsetEncoder the encoding to use, null uses the default Charset.
137+
* @param append true if content should be appended, false to overwrite.
138+
* @throws NullPointerException if the file is null.
139+
* @throws IOException in case of an I/O error.
132140
*/
133-
public FileWriterWithEncoding(final String fileName, final CharsetEncoder charsetEncoder, final boolean append)
141+
public FileWriterWithEncoding(final File file, final CharsetEncoder charsetEncoder, final boolean append)
134142
throws IOException {
135-
this(new File(fileName), charsetEncoder, append);
143+
this.out = initWriter(file, charsetEncoder, append);
136144
}
137145

138146
/**
@@ -163,97 +171,97 @@ public FileWriterWithEncoding(final File file, final String charsetName, final b
163171
/**
164172
* Constructs a FileWriterWithEncoding with a file encoding.
165173
*
166-
* @param file the file to write to, not null
174+
* @param fileName the name of the file to write to, not null
175+
* @param charset the charset to use, not null
176+
* @throws NullPointerException if the file name or encoding is null
177+
* @throws IOException in case of an I/O error
178+
*/
179+
public FileWriterWithEncoding(final String fileName, final Charset charset) throws IOException {
180+
this(new File(fileName), charset, false);
181+
}
182+
183+
/**
184+
* Constructs a FileWriterWithEncoding with a file encoding.
185+
*
186+
* @param fileName the name of the file to write to, not null
167187
* @param charset the encoding to use, not null
168-
* @throws NullPointerException if the file or encoding is null
188+
* @param append true if content should be appended, false to overwrite
189+
* @throws NullPointerException if the file name or encoding is null
169190
* @throws IOException in case of an I/O error
170191
*/
171-
public FileWriterWithEncoding(final File file, final Charset charset) throws IOException {
172-
this(file, charset, false);
192+
public FileWriterWithEncoding(final String fileName, final Charset charset, final boolean append)
193+
throws IOException {
194+
this(new File(fileName), charset, append);
173195
}
174196

175197
/**
176198
* Constructs a FileWriterWithEncoding with a file encoding.
177199
*
178-
* @param file the file to write to, not null.
179-
* @param encoding the name of the requested charset, null uses the default Charset.
180-
* @param append true if content should be appended, false to overwrite.
181-
* @throws NullPointerException if the file is null.
182-
* @throws IOException in case of an I/O error.
200+
* @param fileName the name of the file to write to, not null
201+
* @param encoding the encoding to use, not null
202+
* @throws NullPointerException if the file name or encoding is null
203+
* @throws IOException in case of an I/O error
183204
*/
184-
public FileWriterWithEncoding(final File file, final Charset encoding, final boolean append) throws IOException {
185-
this.out = initWriter(file, encoding, append);
205+
public FileWriterWithEncoding(final String fileName, final CharsetEncoder encoding) throws IOException {
206+
this(new File(fileName), encoding, false);
186207
}
187208

188209
/**
189210
* Constructs a FileWriterWithEncoding with a file encoding.
190211
*
191-
* @param file the file to write to, not null
212+
* @param fileName the name of the file to write to, not null
192213
* @param charsetEncoder the encoding to use, not null
193-
* @throws NullPointerException if the file or encoding is null
214+
* @param append true if content should be appended, false to overwrite
215+
* @throws NullPointerException if the file name or encoding is null
194216
* @throws IOException in case of an I/O error
195217
*/
196-
public FileWriterWithEncoding(final File file, final CharsetEncoder charsetEncoder) throws IOException {
197-
this(file, charsetEncoder, false);
218+
public FileWriterWithEncoding(final String fileName, final CharsetEncoder charsetEncoder, final boolean append)
219+
throws IOException {
220+
this(new File(fileName), charsetEncoder, append);
198221
}
199222

200223
/**
201224
* Constructs a FileWriterWithEncoding with a file encoding.
202225
*
203-
* @param file the file to write to, not null.
204-
* @param charsetEncoder the encoding to use, null uses the default Charset.
205-
* @param append true if content should be appended, false to overwrite.
206-
* @throws NullPointerException if the file is null.
207-
* @throws IOException in case of an I/O error.
226+
* @param fileName the name of the file to write to, not null
227+
* @param charsetName the name of the requested charset, not null
228+
* @throws NullPointerException if the file name or encoding is null
229+
* @throws IOException in case of an I/O error
208230
*/
209-
public FileWriterWithEncoding(final File file, final CharsetEncoder charsetEncoder, final boolean append)
210-
throws IOException {
211-
this.out = initWriter(file, charsetEncoder, append);
231+
public FileWriterWithEncoding(final String fileName, final String charsetName) throws IOException {
232+
this(new File(fileName), charsetName, false);
212233
}
213234

214235
/**
215-
* Initializes the wrapped file writer. Ensure that a cleanup occurs if the writer creation fails.
236+
* Constructs a FileWriterWithEncoding with a file encoding.
216237
*
217-
* @param file the file to be accessed
218-
* @param encoding the encoding to use - may be Charset, CharsetEncoder or String, null uses the default Charset.
219-
* @param append true to append
220-
* @return the initialized writer
221-
* @throws IOException if an error occurs
238+
* @param fileName the name of the file to write to, not null
239+
* @param charsetName the name of the requested charset, not null
240+
* @param append true if content should be appended, false to overwrite
241+
* @throws NullPointerException if the file name or encoding is null
242+
* @throws IOException in case of an I/O error
222243
*/
223-
private static Writer initWriter(final File file, final Object encoding, final boolean append) throws IOException {
224-
Objects.requireNonNull(file, "file");
225-
OutputStream stream = null;
226-
final boolean fileExistedAlready = file.exists();
227-
try {
228-
stream = Files.newOutputStream(file.toPath(), append ? StandardOpenOption.APPEND : StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
229-
if (encoding == null || encoding instanceof Charset) {
230-
return new OutputStreamWriter(stream, Charsets.toCharset((Charset) encoding));
231-
}
232-
if (encoding instanceof CharsetEncoder) {
233-
return new OutputStreamWriter(stream, (CharsetEncoder) encoding);
234-
}
235-
return new OutputStreamWriter(stream, (String) encoding);
236-
} catch (final IOException | RuntimeException ex) {
237-
try {
238-
IOUtils.close(stream);
239-
} catch (final IOException e) {
240-
ex.addSuppressed(e);
241-
}
242-
if (!fileExistedAlready) {
243-
FileUtils.deleteQuietly(file);
244-
}
245-
throw ex;
246-
}
244+
public FileWriterWithEncoding(final String fileName, final String charsetName, final boolean append)
245+
throws IOException {
246+
this(new File(fileName), charsetName, append);
247247
}
248248

249249
/**
250-
* Writes a character.
251-
* @param idx the character to write
250+
* Closes the stream.
252251
* @throws IOException if an I/O error occurs.
253252
*/
254253
@Override
255-
public void write(final int idx) throws IOException {
256-
out.write(idx);
254+
public void close() throws IOException {
255+
out.close();
256+
}
257+
258+
/**
259+
* Flushes the stream.
260+
* @throws IOException if an I/O error occurs.
261+
*/
262+
@Override
263+
public void flush() throws IOException {
264+
out.flush();
257265
}
258266

259267
/**
@@ -278,6 +286,16 @@ public void write(final char[] chr, final int st, final int end) throws IOExcept
278286
out.write(chr, st, end);
279287
}
280288

289+
/**
290+
* Writes a character.
291+
* @param idx the character to write
292+
* @throws IOException if an I/O error occurs.
293+
*/
294+
@Override
295+
public void write(final int idx) throws IOException {
296+
out.write(idx);
297+
}
298+
281299
/**
282300
* Writes the characters from a string.
283301
* @param str the string to write
@@ -299,22 +317,4 @@ public void write(final String str) throws IOException {
299317
public void write(final String str, final int st, final int end) throws IOException {
300318
out.write(str, st, end);
301319
}
302-
303-
/**
304-
* Flushes the stream.
305-
* @throws IOException if an I/O error occurs.
306-
*/
307-
@Override
308-
public void flush() throws IOException {
309-
out.flush();
310-
}
311-
312-
/**
313-
* Closes the stream.
314-
* @throws IOException if an I/O error occurs.
315-
*/
316-
@Override
317-
public void close() throws IOException {
318-
out.close();
319-
}
320320
}

0 commit comments

Comments
 (0)