- /**
- * Get the default system encoding using a writer
- * @param inWriter writer object
- * @return string defining encoding
- */
- private static String getEncoding(OutputStreamWriter inWriter)
- {
- String encoding = inWriter.getEncoding();
- try {
- encoding = Charset.forName(encoding).name();
- }
- catch (Exception e) {} // ignore failure to find encoding
- // Hack to fix bugs with Mac OSX (which reports MacRoman but is actually UTF-8)
- if (encoding == null || encoding.toLowerCase().startsWith("macroman")) {
- encoding = "UTF-8";
- }
- return encoding;
- }
-
-
- /**
- * Use a temporary file to obtain the name of the default system encoding
- * @return name of default system encoding, or null if write failed
- */
- private static String getSystemEncoding()
- {
- File tempFile = null;
- String encoding = null;
- try
- {
- tempFile = File.createTempFile("prune", null);
- OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(tempFile));
- encoding = getEncoding(writer);
- writer.close();
- }
- catch (IOException e) {} // value stays null
- // Delete temp file
- if (tempFile != null && tempFile.exists()) {
- if (!tempFile.delete()) {
- System.err.println("Cannot delete temp file: " + tempFile.getAbsolutePath());
- }
- }
- // If writing failed (eg permissions) then just ask system for default
- if (encoding == null) encoding = Charset.defaultCharset().name();
- return encoding;
- }
-
- /**
- * Creates temp file if necessary to check system encoding
- * @return true if system uses UTF-8 by default
- */
- private static boolean isSystemUtf8()
- {
- if (_systemEncoding == null) _systemEncoding = getSystemEncoding();
- return (_systemEncoding != null && _systemEncoding.toUpperCase().equals("UTF-8"));
- }
-