Author: tchemit Date: 2011-01-20 08:24:37 +0100 (Thu, 20 Jan 2011) New Revision: 367 Url: http://nuiton.org/repositories/revision/processor/367 Log: Evolution #1212: Add usefull methods in ProcessorUtil Modified: trunk/nuiton-processor/src/main/java/org/nuiton/processor/ProcessorUtil.java Modified: trunk/nuiton-processor/src/main/java/org/nuiton/processor/ProcessorUtil.java =================================================================== --- trunk/nuiton-processor/src/main/java/org/nuiton/processor/ProcessorUtil.java 2011-01-04 17:18:10 UTC (rev 366) +++ trunk/nuiton-processor/src/main/java/org/nuiton/processor/ProcessorUtil.java 2011-01-20 07:24:37 UTC (rev 367) @@ -33,7 +33,9 @@ import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; +import java.io.InputStream; import java.io.InputStreamReader; +import java.io.OutputStream; import java.io.OutputStreamWriter; /** @@ -128,6 +130,7 @@ * @since 1.0.3 * @deprecated since 1.0.4, prefer use the {@link #doProcess(Processor, String, String, String)}. */ + @Deprecated public static void doProcess(Processor processor, String in, String out) throws IOException { doProcess(processor, new File(in), new File(out), DEFAULT_ENCODING); @@ -167,8 +170,65 @@ } } + /** + * Launch the process of the given {@code processor} to the given streams. + * + * @param processor the processor to launch + * @param in the input file + * @param out the output file + * @param encoding encoding to use to read and write files. + * @throws IOException if any problems while processing file + * @since 1.1 + */ + public static void doProcess(Processor processor, + InputStream in, + OutputStream out, + String encoding) throws IOException { + InputStreamReader input = new InputStreamReader(in, encoding); + try { + OutputStreamWriter output = new OutputStreamWriter(out, encoding); + try { + processor.process(input, output); + } catch (IOException eee) { + if (log.isErrorEnabled()) { + log.error( + "Error while processing file " + in + " to " + out + + " with processor " + processor, eee); + } + throw eee; + } finally { + output.close(); + } + } finally { + input.close(); + } + } /** + * Launch the process of the given {@code processor} to the given readers. + * + * @param processor the processor to launch + * @param in the input file + * @param out the output file + * @throws IOException if any problems while processing file + * @since 1.1 + */ + public static void doProcess(Processor processor, + InputStreamReader in, + OutputStreamWriter out) throws IOException { + try { + processor.process(in, out); + } catch (IOException eee) { + if (log.isErrorEnabled()) { + log.error( + "Error while processing file " + in + " to " + out + + " with processor " + processor, eee); + } + throw eee; + } + } + + /** * Launch the process of the given {@code processor} to the given files using * the system default encoding. *