[Schmitzm-commits] r1264 - in trunk/src/schmitzm: io lang

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Thu Nov 11 22:11:08 CET 2010


Author: mojays
Date: 2010-11-11 22:11:08 +0100 (Thu, 11 Nov 2010)
New Revision: 1264

Modified:
   trunk/src/schmitzm/io/IOUtil.java
   trunk/src/schmitzm/lang/ResourceProviderOperator.java
Log:
IOUtil: helper methods to create UTF8 file writer
ResourceProviderOperator: use of UTF8 encoding for writing new bundle files

Modified: trunk/src/schmitzm/io/IOUtil.java
===================================================================
--- trunk/src/schmitzm/io/IOUtil.java	2010-11-11 20:30:38 UTC (rev 1263)
+++ trunk/src/schmitzm/io/IOUtil.java	2010-11-11 21:11:08 UTC (rev 1264)
@@ -39,10 +39,14 @@
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.UnsupportedEncodingException;
+import java.io.Writer;
 import java.net.MalformedURLException;
 import java.net.URISyntaxException;
 import java.net.URL;
 import java.nio.channels.FileChannel;
+import java.nio.charset.Charset;
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipOutputStream;
 
@@ -368,6 +372,41 @@
 		// Sonst: BaseDir als Basis fuer Source verwenden
 		return new File(baseDir, path);
 	}
+	
+	/**
+	 * Creates a {@link Writer} to write to file with a special
+	 * encoding.
+	 * @param file output file
+	 * @param append indicated whether an existing file will be extended
+	 * @param charsetName name of the encoding char set
+	 * @throws FileNotFoundException if the file exists but is a directory rather than a regular file,
+	 *         does not exist but cannot be created, or cannot be opened for any other reason 
+	 * @throws UnsupportedEncodingException if the named encoding is not supported
+	 * @see Charset 
+	 */
+	public static OutputStreamWriter createEncodedFileWriter(File file, boolean append, String charsetName) throws FileNotFoundException, UnsupportedEncodingException {
+	  OutputStream output = new FileOutputStream(file, append);
+	  OutputStreamWriter writer = new OutputStreamWriter(output, charsetName);
+	  return writer;
+	}
+	   
+    /**
+     * Creates a {@link Writer} to write to file with UTF-8
+     * encoding.
+     * @param file output file
+     * @param append indicated whether an existing file will be extended
+     * @throws FileNotFoundException if the file exists but is a directory rather than a regular file,
+     *         does not exist but cannot be created, or cannot be opened for any other reason 
+     */
+	public static OutputStreamWriter createUTF8FileWriter(File file, boolean append) throws FileNotFoundException {
+	  try {
+      return createEncodedFileWriter(file, append, "UTF-8");
+    } catch (UnsupportedEncodingException err) {
+      // Should not occur, but must be handled
+      throw new RuntimeException(err);
+    }
+	}
+	
 
 	/**
 	 * Versucht einen Eingabe-Stream zu schliessen.<br>

Modified: trunk/src/schmitzm/lang/ResourceProviderOperator.java
===================================================================
--- trunk/src/schmitzm/lang/ResourceProviderOperator.java	2010-11-11 20:30:38 UTC (rev 1263)
+++ trunk/src/schmitzm/lang/ResourceProviderOperator.java	2010-11-11 21:11:08 UTC (rev 1264)
@@ -36,6 +36,7 @@
 import java.io.FileReader;
 import java.io.FileWriter;
 import java.io.IOException;
+import java.io.Writer;
 import java.util.HashSet;
 import java.util.Locale;
 import java.util.MissingResourceException;
@@ -230,8 +231,8 @@
     // store remaining pairs
     File bundleFile = ResourceProvider.getPropertyFile(rp, locale, getResourceBundlesRootFolder());
     String comments = getInitialCommentLines(bundleFile, "#", true);
-//    FileWriter remainingKeysOutput = new FileWriter(bundleFile,false);
-    FileWriter remainingKeysOutput = new FileWriter( IOUtil.changeFileExt(bundleFile, "remainingKeys"), false );
+//    Writer remainingKeysOutput = IOUtil.createUTF8FileWriter(bundleFile,false);
+    Writer remainingKeysOutput = IOUtil.createUTF8FileWriter( IOUtil.changeFileExt(bundleFile, "remainingKeys"), false );
     remainingKeysOutput.write(comments); // write comments as their were
     if ( !"".equals(comments) )
       remainingKeysOutput.write("\n");
@@ -243,7 +244,7 @@
       
     // store removed pairs
     File removedKeysFile = IOUtil.changeFileExt(bundleFile, "removedKeys");
-    FileWriter removedKeysOutput = new FileWriter(removedKeysFile,true);
+    Writer removedKeysOutput = IOUtil.createUTF8FileWriter(removedKeysFile,true);
     removedKeys.store(
         removedKeysOutput,
         "======================================================\n" +



More information about the Schmitzm-commits mailing list