You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2021/07/25 19:05:11 UTC
[commons-io] 03/05: Sort members.
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-io.git
commit 22487c3cebb4d2dc41db60ccd3b20e1dd8c14d48
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Jul 25 14:53:55 2021 -0400
Sort members.
---
.../commons/io/output/FileWriterWithEncoding.java | 246 ++++++++++-----------
1 file changed, 123 insertions(+), 123 deletions(-)
diff --git a/src/main/java/org/apache/commons/io/output/FileWriterWithEncoding.java b/src/main/java/org/apache/commons/io/output/FileWriterWithEncoding.java
index c1ff5c6..d01937a 100644
--- a/src/main/java/org/apache/commons/io/output/FileWriterWithEncoding.java
+++ b/src/main/java/org/apache/commons/io/output/FileWriterWithEncoding.java
@@ -54,85 +54,93 @@ public class FileWriterWithEncoding extends Writer {
// Cannot extend ProxyWriter, as requires writer to be
// known when super() is called
- /** The writer to decorate. */
- private final Writer out;
-
/**
- * Constructs a FileWriterWithEncoding with a file encoding.
+ * Initializes the wrapped file writer. Ensure that a cleanup occurs if the writer creation fails.
*
- * @param fileName the name of the file to write to, not null
- * @param charsetName the name of the requested charset, not null
- * @throws NullPointerException if the file name or encoding is null
- * @throws IOException in case of an I/O error
+ * @param file the file to be accessed
+ * @param encoding the encoding to use - may be Charset, CharsetEncoder or String, null uses the default Charset.
+ * @param append true to append
+ * @return the initialized writer
+ * @throws IOException if an error occurs
*/
- public FileWriterWithEncoding(final String fileName, final String charsetName) throws IOException {
- this(new File(fileName), charsetName, false);
+ private static Writer initWriter(final File file, final Object encoding, final boolean append) throws IOException {
+ Objects.requireNonNull(file, "file");
+ OutputStream stream = null;
+ final boolean fileExistedAlready = file.exists();
+ try {
+ stream = Files.newOutputStream(file.toPath(), append ? StandardOpenOption.APPEND : StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
+ if (encoding == null || encoding instanceof Charset) {
+ return new OutputStreamWriter(stream, Charsets.toCharset((Charset) encoding));
+ }
+ if (encoding instanceof CharsetEncoder) {
+ return new OutputStreamWriter(stream, (CharsetEncoder) encoding);
+ }
+ return new OutputStreamWriter(stream, (String) encoding);
+ } catch (final IOException | RuntimeException ex) {
+ try {
+ IOUtils.close(stream);
+ } catch (final IOException e) {
+ ex.addSuppressed(e);
+ }
+ if (!fileExistedAlready) {
+ FileUtils.deleteQuietly(file);
+ }
+ throw ex;
+ }
}
- /**
- * Constructs a FileWriterWithEncoding with a file encoding.
- *
- * @param fileName the name of the file to write to, not null
- * @param charsetName the name of the requested charset, not null
- * @param append true if content should be appended, false to overwrite
- * @throws NullPointerException if the file name or encoding is null
- * @throws IOException in case of an I/O error
- */
- public FileWriterWithEncoding(final String fileName, final String charsetName, final boolean append)
- throws IOException {
- this(new File(fileName), charsetName, append);
- }
+ /** The writer to decorate. */
+ private final Writer out;
/**
* Constructs a FileWriterWithEncoding with a file encoding.
*
- * @param fileName the name of the file to write to, not null
- * @param charset the charset to use, not null
- * @throws NullPointerException if the file name or encoding is null
+ * @param file the file to write to, not null
+ * @param charset the encoding to use, not null
+ * @throws NullPointerException if the file or encoding is null
* @throws IOException in case of an I/O error
*/
- public FileWriterWithEncoding(final String fileName, final Charset charset) throws IOException {
- this(new File(fileName), charset, false);
+ public FileWriterWithEncoding(final File file, final Charset charset) throws IOException {
+ this(file, charset, false);
}
/**
* Constructs a FileWriterWithEncoding with a file encoding.
*
- * @param fileName the name of the file to write to, not null
- * @param charset the encoding to use, not null
- * @param append true if content should be appended, false to overwrite
- * @throws NullPointerException if the file name or encoding is null
- * @throws IOException in case of an I/O error
+ * @param file the file to write to, not null.
+ * @param encoding the name of the requested charset, null uses the default Charset.
+ * @param append true if content should be appended, false to overwrite.
+ * @throws NullPointerException if the file is null.
+ * @throws IOException in case of an I/O error.
*/
- public FileWriterWithEncoding(final String fileName, final Charset charset, final boolean append)
- throws IOException {
- this(new File(fileName), charset, append);
+ public FileWriterWithEncoding(final File file, final Charset encoding, final boolean append) throws IOException {
+ this.out = initWriter(file, encoding, append);
}
/**
* Constructs a FileWriterWithEncoding with a file encoding.
*
- * @param fileName the name of the file to write to, not null
- * @param encoding the encoding to use, not null
- * @throws NullPointerException if the file name or encoding is null
+ * @param file the file to write to, not null
+ * @param charsetEncoder the encoding to use, not null
+ * @throws NullPointerException if the file or encoding is null
* @throws IOException in case of an I/O error
*/
- public FileWriterWithEncoding(final String fileName, final CharsetEncoder encoding) throws IOException {
- this(new File(fileName), encoding, false);
+ public FileWriterWithEncoding(final File file, final CharsetEncoder charsetEncoder) throws IOException {
+ this(file, charsetEncoder, false);
}
/**
* Constructs a FileWriterWithEncoding with a file encoding.
*
- * @param fileName the name of the file to write to, not null
- * @param charsetEncoder the encoding to use, not null
- * @param append true if content should be appended, false to overwrite
- * @throws NullPointerException if the file name or encoding is null
- * @throws IOException in case of an I/O error
+ * @param file the file to write to, not null.
+ * @param charsetEncoder the encoding to use, null uses the default Charset.
+ * @param append true if content should be appended, false to overwrite.
+ * @throws NullPointerException if the file is null.
+ * @throws IOException in case of an I/O error.
*/
- public FileWriterWithEncoding(final String fileName, final CharsetEncoder charsetEncoder, final boolean append)
+ public FileWriterWithEncoding(final File file, final CharsetEncoder charsetEncoder, final boolean append)
throws IOException {
- this(new File(fileName), charsetEncoder, append);
+ this.out = initWriter(file, charsetEncoder, append);
}
/**
@@ -163,97 +171,97 @@ public class FileWriterWithEncoding extends Writer {
/**
* Constructs a FileWriterWithEncoding with a file encoding.
*
- * @param file the file to write to, not null
+ * @param fileName the name of the file to write to, not null
+ * @param charset the charset to use, not null
+ * @throws NullPointerException if the file name or encoding is null
+ * @throws IOException in case of an I/O error
+ */
+ public FileWriterWithEncoding(final String fileName, final Charset charset) throws IOException {
+ this(new File(fileName), charset, false);
+ }
+
+ /**
+ * Constructs a FileWriterWithEncoding with a file encoding.
+ *
+ * @param fileName the name of the file to write to, not null
* @param charset the encoding to use, not null
- * @throws NullPointerException if the file or encoding is null
+ * @param append true if content should be appended, false to overwrite
+ * @throws NullPointerException if the file name or encoding is null
* @throws IOException in case of an I/O error
*/
- public FileWriterWithEncoding(final File file, final Charset charset) throws IOException {
- this(file, charset, false);
+ public FileWriterWithEncoding(final String fileName, final Charset charset, final boolean append)
+ throws IOException {
+ this(new File(fileName), charset, append);
}
/**
* Constructs a FileWriterWithEncoding with a file encoding.
*
- * @param file the file to write to, not null.
- * @param encoding the name of the requested charset, null uses the default Charset.
- * @param append true if content should be appended, false to overwrite.
- * @throws NullPointerException if the file is null.
- * @throws IOException in case of an I/O error.
+ * @param fileName the name of the file to write to, not null
+ * @param encoding the encoding to use, not null
+ * @throws NullPointerException if the file name or encoding is null
+ * @throws IOException in case of an I/O error
*/
- public FileWriterWithEncoding(final File file, final Charset encoding, final boolean append) throws IOException {
- this.out = initWriter(file, encoding, append);
+ public FileWriterWithEncoding(final String fileName, final CharsetEncoder encoding) throws IOException {
+ this(new File(fileName), encoding, false);
}
/**
* Constructs a FileWriterWithEncoding with a file encoding.
*
- * @param file the file to write to, not null
+ * @param fileName the name of the file to write to, not null
* @param charsetEncoder the encoding to use, not null
- * @throws NullPointerException if the file or encoding is null
+ * @param append true if content should be appended, false to overwrite
+ * @throws NullPointerException if the file name or encoding is null
* @throws IOException in case of an I/O error
*/
- public FileWriterWithEncoding(final File file, final CharsetEncoder charsetEncoder) throws IOException {
- this(file, charsetEncoder, false);
+ public FileWriterWithEncoding(final String fileName, final CharsetEncoder charsetEncoder, final boolean append)
+ throws IOException {
+ this(new File(fileName), charsetEncoder, append);
}
/**
* Constructs a FileWriterWithEncoding with a file encoding.
*
- * @param file the file to write to, not null.
- * @param charsetEncoder the encoding to use, null uses the default Charset.
- * @param append true if content should be appended, false to overwrite.
- * @throws NullPointerException if the file is null.
- * @throws IOException in case of an I/O error.
+ * @param fileName the name of the file to write to, not null
+ * @param charsetName the name of the requested charset, not null
+ * @throws NullPointerException if the file name or encoding is null
+ * @throws IOException in case of an I/O error
*/
- public FileWriterWithEncoding(final File file, final CharsetEncoder charsetEncoder, final boolean append)
- throws IOException {
- this.out = initWriter(file, charsetEncoder, append);
+ public FileWriterWithEncoding(final String fileName, final String charsetName) throws IOException {
+ this(new File(fileName), charsetName, false);
}
/**
- * Initializes the wrapped file writer. Ensure that a cleanup occurs if the writer creation fails.
+ * Constructs a FileWriterWithEncoding with a file encoding.
*
- * @param file the file to be accessed
- * @param encoding the encoding to use - may be Charset, CharsetEncoder or String, null uses the default Charset.
- * @param append true to append
- * @return the initialized writer
- * @throws IOException if an error occurs
+ * @param fileName the name of the file to write to, not null
+ * @param charsetName the name of the requested charset, not null
+ * @param append true if content should be appended, false to overwrite
+ * @throws NullPointerException if the file name or encoding is null
+ * @throws IOException in case of an I/O error
*/
- private static Writer initWriter(final File file, final Object encoding, final boolean append) throws IOException {
- Objects.requireNonNull(file, "file");
- OutputStream stream = null;
- final boolean fileExistedAlready = file.exists();
- try {
- stream = Files.newOutputStream(file.toPath(), append ? StandardOpenOption.APPEND : StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
- if (encoding == null || encoding instanceof Charset) {
- return new OutputStreamWriter(stream, Charsets.toCharset((Charset) encoding));
- }
- if (encoding instanceof CharsetEncoder) {
- return new OutputStreamWriter(stream, (CharsetEncoder) encoding);
- }
- return new OutputStreamWriter(stream, (String) encoding);
- } catch (final IOException | RuntimeException ex) {
- try {
- IOUtils.close(stream);
- } catch (final IOException e) {
- ex.addSuppressed(e);
- }
- if (!fileExistedAlready) {
- FileUtils.deleteQuietly(file);
- }
- throw ex;
- }
+ public FileWriterWithEncoding(final String fileName, final String charsetName, final boolean append)
+ throws IOException {
+ this(new File(fileName), charsetName, append);
}
/**
- * Writes a character.
- * @param idx the character to write
+ * Closes the stream.
* @throws IOException if an I/O error occurs.
*/
@Override
- public void write(final int idx) throws IOException {
- out.write(idx);
+ public void close() throws IOException {
+ out.close();
+ }
+
+ /**
+ * Flushes the stream.
+ * @throws IOException if an I/O error occurs.
+ */
+ @Override
+ public void flush() throws IOException {
+ out.flush();
}
/**
@@ -279,6 +287,16 @@ public class FileWriterWithEncoding extends Writer {
}
/**
+ * Writes a character.
+ * @param idx the character to write
+ * @throws IOException if an I/O error occurs.
+ */
+ @Override
+ public void write(final int idx) throws IOException {
+ out.write(idx);
+ }
+
+ /**
* Writes the characters from a string.
* @param str the string to write
* @throws IOException if an I/O error occurs.
@@ -299,22 +317,4 @@ public class FileWriterWithEncoding extends Writer {
public void write(final String str, final int st, final int end) throws IOException {
out.write(str, st, end);
}
-
- /**
- * Flushes the stream.
- * @throws IOException if an I/O error occurs.
- */
- @Override
- public void flush() throws IOException {
- out.flush();
- }
-
- /**
- * Closes the stream.
- * @throws IOException if an I/O error occurs.
- */
- @Override
- public void close() throws IOException {
- out.close();
- }
}