Author: bleny Date: 2012-01-16 12:33:11 +0100 (Mon, 16 Jan 2012) New Revision: 2278 Url: http://nuiton.org/repositories/revision/nuiton-utils/2278 Log: #1906 allow giving a charset in CSV export Modified: trunk/nuiton-csv/src/main/java/org/nuiton/util/csv/Export.java Modified: trunk/nuiton-csv/src/main/java/org/nuiton/util/csv/Export.java =================================================================== --- trunk/nuiton-csv/src/main/java/org/nuiton/util/csv/Export.java 2012-01-10 09:15:15 UTC (rev 2277) +++ trunk/nuiton-csv/src/main/java/org/nuiton/util/csv/Export.java 2012-01-16 11:33:11 UTC (rev 2278) @@ -29,12 +29,14 @@ import org.apache.commons.logging.LogFactory; import org.nuiton.util.StringUtil; -import java.io.BufferedWriter; +import java.io.ByteArrayOutputStream; import java.io.File; -import java.io.FileWriter; +import java.io.FileOutputStream; import java.io.InputStream; -import java.io.StringWriter; +import java.io.OutputStream; +import java.io.OutputStreamWriter; import java.io.Writer; +import java.nio.charset.Charset; import java.util.Collection; import java.util.LinkedList; import java.util.List; @@ -78,24 +80,39 @@ return new Export<E>(model, data); } + public static <E> void exportToWriter(ExportModel<E> model, + Iterable<E> data, + Writer writer) throws Exception { + Export<E> exporter = newExport(model, data); + exporter.write(writer); + } + public static <E> void exportToFile(ExportModel<E> model, Iterable<E> data, File file) throws Exception { Export<E> exporter = newExport(model, data); - exporter.exportToFile(file); + exporter.write(file); } + public static <E> void exportToFile(ExportModel<E> model, + Iterable<E> data, + File file, + Charset charset) throws Exception { + Export<E> exporter = newExport(model, data); + exporter.write(file, charset); + } + public static <E> String exportToString(ExportModel<E> model, Iterable<E> data) throws Exception { Export<E> exporter = newExport(model, data); - return exporter.startExportAsString(); + return exporter.toString(Charset.defaultCharset()); } - public static <E> void exportToWriter(ExportModel<E> model, - Iterable<E> data, - Writer writer) throws Exception { + public static <E> String exportToString(ExportModel<E> model, + Iterable<E> data, + Charset charset) throws Exception { Export<E> exporter = newExport(model, data); - exporter.startExport(writer); + return exporter.toString(charset); } protected Export(ExportModel<E> model, Iterable<E> data) { @@ -103,16 +120,7 @@ this.data = data; } - public void exportToFile(File file) throws Exception { - Writer writer = new BufferedWriter(new FileWriter(file)); - try { - startExport(writer); - } finally { - writer.close(); - } - } - - public void startExport(Writer writer) throws Exception { + public void write(Writer writer) throws Exception { String separator = String.valueOf(model.getSeparator()); // add headers @@ -152,19 +160,68 @@ } } - public String startExportAsString() throws Exception { - StringWriter writer = new StringWriter(); + public void write(OutputStream outputStream, Charset charset) throws Exception { + Writer writer = new OutputStreamWriter(outputStream, charset); + write(writer); + } + + public void write(OutputStream outputStream) throws Exception { + write(outputStream, Charset.defaultCharset()); + } + + public void write(File file, Charset charset) throws Exception { + FileOutputStream fileOutputStream = new FileOutputStream(file); try { - startExport(writer); - String result = writer.toString(); - return result; + write(fileOutputStream, charset); } finally { - writer.close(); + fileOutputStream.close(); } } + public void write(File file) throws Exception { + write(file, Charset.defaultCharset()); + } + + public String toString(Charset charset) throws Exception { + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream, charset); + write(outputStreamWriter); + String result = new String(outputStream.toByteArray(), charset); + return result; + } + + /** + * @deprecated since 2.4.3, use {@link #write(java.io.File)} instead. + */ + @Deprecated + public void exportToFile(File file) throws Exception { + write(file); + } + + /** + * @deprecated since 2.4.3, use {@link #write(java.io.Writer)} instead. + */ + @Deprecated + public void startExport(Writer writer) throws Exception { + write(writer); + } + + /** + * @deprecated since 2.4.3, use {@link #toString(java.nio.charset.Charset)} instead. + */ + @Deprecated + public String startExportAsString() throws Exception { + return toString(Charset.defaultCharset()); + } + + /** + * @deprecated since 2.4.3. It's not the role of the API to give an InputStream + * you can use {@link #toString(java.nio.charset.Charset)} and + * {@link org.apache.commons.io.IOUtils#toInputStream(String)} + */ + @Deprecated public InputStream startExport() throws Exception { - String content = startExportAsString(); + String content = toString(Charset.defaultCharset()); return IOUtils.toInputStream(content); } }
participants (1)
-
bleny@users.nuiton.org