[Japi-cvs] SF.net SVN: japi: [259] libs/argparser/trunk/src/doc/examples/Recode.java
Status: Beta
Brought to you by:
christianhujer
From: <chr...@us...> - 2006-12-14 23:09:35
|
Revision: 259 http://svn.sourceforge.net/japi/?rev=259&view=rev Author: christianhujer Date: 2006-12-14 15:09:33 -0800 (Thu, 14 Dec 2006) Log Message: ----------- Added new example: Recode. Added Paths: ----------- libs/argparser/trunk/src/doc/examples/Recode.java Added: libs/argparser/trunk/src/doc/examples/Recode.java =================================================================== --- libs/argparser/trunk/src/doc/examples/Recode.java (rev 0) +++ libs/argparser/trunk/src/doc/examples/Recode.java 2006-12-14 23:09:33 UTC (rev 259) @@ -0,0 +1,101 @@ +package examples; + +import java.io.File; +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; +import java.io.Reader; +import java.io.Writer; +import java.io.FileNotFoundException; +import java.util.List; +import net.sf.japi.io.args.BasicCommand; +import net.sf.japi.io.args.Option; +import org.jetbrains.annotations.NotNull; + +/** + * Recode is a Java program that recodes files from one encoding to another. + * Warning: This file changes permissions and eventually ownership. + * @author <a href="mailto:ch...@ri...">Christian Hujer</a> + */ +public class Recode extends BasicCommand { + + /** The InputEncoding to use. */ + private String inputEncoding; + + /** The OutputEncoding to use. */ + private String outputEncoding; + + @Option({"i", "inputEncoding"}) + public void setInputEncoding(final String inputEncoding) { + this.inputEncoding = inputEncoding; + } + + @Option({"o", "outputEncoding"}) + public void setOutputEncoding(final String outputEncoding) { + this.outputEncoding = outputEncoding; + } + + /** {@inheritDoc} */ + @SuppressWarnings({"InstanceMethodNamingConvention"}) + public int run(@NotNull List<String> args) throws Exception { + int returnCode = 0; + if (args.size() == 0) { + recode(System.in, System.out); + } else { + for (final String filename : args) { + try { + recode(filename); + } catch (final IOException e) { + returnCode = 1; + System.err.println(e); + } + } + } + return returnCode; + } + + /** + * Recodes a File from the specified input encoding to the specified output encoding. + * @param filename Filename of the file to recode + * @throws IOException In case of I/O problems. + */ + public void recode(final String filename) throws IOException { + final File file = new File(filename); + final File tmpFile = File.createTempFile("recode", null, file.getParentFile()); + final InputStream in = new FileInputStream(file); + try { + final OutputStream out = new FileOutputStream(tmpFile); + try { + recode(in, out); + } finally { + out.close(); + } + if (!tmpFile.renameTo(file)) { + tmpFile.delete(); + throw new FileNotFoundException("Couldn't open " + file + " for writing."); + } + } finally { + in.close(); + } + } + + /** + * Recodes a Stream and writes the data to another Stream. + * @param in InputStream to recode + * @param out OutputStream to recode + * @throws IOException In case of I/O problems. + */ + public void recode(final InputStream in, final OutputStream out) throws IOException { + final Reader cin = new InputStreamReader(in, inputEncoding); + final Writer cout = new OutputStreamWriter(out, outputEncoding); + final char[] buf = new char[4096]; + //noinspection StatementWithEmptyBody + for (int charsRead; (charsRead = cin.read(buf)) != -1; cout.write(buf, 0, charsRead)); + cout.flush(); + } + +} // class Recode Property changes on: libs/argparser/trunk/src/doc/examples/Recode.java ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:eol-style + LF This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |