use of java.nio.charset.UnsupportedCharsetException in project j2objc by google.
the class IOSCharsetDecoder method decodeImpl.
private String decodeImpl(ByteBuffer in) {
Charset cs = charset();
if (!(cs instanceof IOSCharset)) {
throw new UnsupportedCharsetException(cs.name());
}
byte[] bytes;
int i;
if (inBuffer != null) {
i = inBuffer.length;
bytes = new byte[i + in.remaining()];
System.arraycopy(inBuffer, 0, bytes, 0, inBuffer.length);
inBuffer = null;
} else {
i = 0;
bytes = new byte[in.remaining()];
}
in.get(bytes, i, bytes.length - i);
String s = decode(bytes, nsEncoding);
if (s.isEmpty()) {
inBuffer = bytes;
} else {
inBuffer = null;
}
return s;
}
use of java.nio.charset.UnsupportedCharsetException in project robovm by robovm.
the class Properties method storeToXML.
/**
* Writes all properties stored in this instance into the {@code
* OutputStream} in XML representation. The DOCTYPE is
*
* <pre>
* <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
* </pre>
*
* If the comment is null, no comment is added to the output. The parameter
* {@code encoding} defines which encoding should be used. The {@code
* OutputStream} is not closed at the end.
*
* @param os the {@code OutputStream} to write to.
* @param comment the comment to add. If null, no comment is added.
* @param encoding the code identifying the encoding that should be used to
* write into the {@code OutputStream}.
* @throws IOException if an error occurs during writing to the output.
*/
public synchronized void storeToXML(OutputStream os, String comment, String encoding) throws IOException {
if (os == null) {
throw new NullPointerException("os == null");
} else if (encoding == null) {
throw new NullPointerException("encoding == null");
}
/*
* We can write to XML file using encoding parameter but note that some
* aliases for encodings are not supported by the XML parser. Thus we
* have to know canonical name for encoding used to store data in XML
* since the XML parser must recognize encoding name used to store data.
*/
String encodingCanonicalName;
try {
encodingCanonicalName = Charset.forName(encoding).name();
} catch (IllegalCharsetNameException e) {
System.out.println("Warning: encoding name " + encoding + " is illegal, using UTF-8 as default encoding");
encodingCanonicalName = "UTF-8";
} catch (UnsupportedCharsetException e) {
System.out.println("Warning: encoding " + encoding + " is not supported, using UTF-8 as default encoding");
encodingCanonicalName = "UTF-8";
}
PrintStream printStream = new PrintStream(os, false, encodingCanonicalName);
printStream.print("<?xml version=\"1.0\" encoding=\"");
printStream.print(encodingCanonicalName);
printStream.println("\"?>");
printStream.print("<!DOCTYPE properties SYSTEM \"");
printStream.print(PROP_DTD_NAME);
printStream.println("\">");
printStream.println("<properties>");
if (comment != null) {
printStream.print("<comment>");
printStream.print(substitutePredefinedEntries(comment));
printStream.println("</comment>");
}
for (Map.Entry<Object, Object> entry : entrySet()) {
String keyValue = (String) entry.getKey();
String entryValue = (String) entry.getValue();
printStream.print("<entry key=\"");
printStream.print(substitutePredefinedEntries(keyValue));
printStream.print("\">");
printStream.print(substitutePredefinedEntries(entryValue));
printStream.println("</entry>");
}
printStream.println("</properties>");
printStream.flush();
}
use of java.nio.charset.UnsupportedCharsetException in project android by JetBrains.
the class EclipseProject method initEncoding.
private void initEncoding() throws IOException {
File propertyFile = new File(myDir, ".settings" + separator + "org.eclipse.core.resources.prefs");
if (propertyFile.exists()) {
Properties properties = PropertiesFiles.getProperties(propertyFile);
if (properties != null) {
Enumeration<?> enumeration = properties.propertyNames();
String prefix = "encoding/";
while (enumeration.hasMoreElements()) {
Object next = enumeration.nextElement();
if (next instanceof String) {
String key = (String) next;
if (key.startsWith(prefix)) {
String path = key.substring(prefix.length());
String encoding = properties.getProperty(key);
if (encoding != null && !encoding.isEmpty()) {
try {
Charset charset = Charset.forName(encoding);
if ("<project>".equals(path)) {
myDefaultCharset = charset;
} else {
if (myFileCharsets == null) {
myFileCharsets = Maps.newHashMap();
}
File file = resolveVariableExpression(path);
if (file != null) {
myFileCharsets.put(file, charset);
} else {
myFileCharsets.put(new File(path), charset);
}
}
} catch (UnsupportedCharsetException uce) {
myImporter.reportWarning(this, propertyFile, "Unknown charset " + encoding);
}
}
}
}
}
}
}
}
use of java.nio.charset.UnsupportedCharsetException in project android by JetBrains.
the class GradleImportTest method testEncoding.
@SuppressWarnings("ResultOfMethodCallIgnored")
public void testEncoding() throws Exception {
// Checks that we properly convert source files from other encodings to UTF-8.
// The following scenarios are tested:
// - For files that have a specific encoding associated with that file, use it
// - For XML files that specify an encoding in the prologue, use it
// - For files that have a specific encoding specified by a BOM, use it
// - For files that are in a project where there is a project-specific encoding, use it
// - For all other files, use the default encoding specified in the workspace
File root = Files.createTempDir();
File app = createLibrary(root, "test.lib2.pkg", false);
// Write some source files where encoding matters
// The Project App will have a default project encoding of MacRoman
// The workspace will have a default encoding of windows1252
// Some individual files will specify a file encoding of iso-8859-1
Charset macRoman;
Charset windows1252;
Charset utf32;
Charset iso8859 = Charsets.ISO_8859_1;
try {
macRoman = Charset.forName("MacRoman");
windows1252 = Charset.forName("windows-1252");
utf32 = Charset.forName("UTF_32");
} catch (UnsupportedCharsetException uce) {
System.err.println("This test machine does not have all the charsets we need: " + uce.getCharsetName() + ": skipping test");
return;
}
String java = "" + "package test.pkg;\n" + "\n" + "public class Text {\n" + "\tpublic static final String TEXT_1 = \"This is plain\";\n" + "\tpublic static final String TEXT_2 = \"æØå\";\n" + "\tpublic static final String TEXT_3 = \"10£\";\n" + "}\n";
String xml = "" + "<?xml version=\"1.0\" encoding=\"" + windows1252.name() + "\"?>\n" + "<LinearLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" + " android:layout_width=\"match_parent\"\n" + " android:layout_height=\"match_parent\"\n" + " android:orientation=\"vertical\" >\n" + "<!-- £ -->\n" + "</LinearLayout>";
File appFile = new File(root, "App/src/test/pkg/Text.java".replace('/', separatorChar));
File lib1File = new File(root, "Lib1/src/test/pkg/Text.java".replace('/', separatorChar));
File lib2File = new File(root, "Lib2/src/test/pkg/Text.java".replace('/', separatorChar));
File xmlFile = new File(root, "App/res/layout/foo.xml".replace('/', separatorChar));
appFile.getParentFile().mkdirs();
lib1File.getParentFile().mkdirs();
lib2File.getParentFile().mkdirs();
xmlFile.getParentFile().mkdirs();
Files.write(java, appFile, iso8859);
Files.write(java, lib1File, macRoman);
Files.write(java, lib2File, windows1252);
Files.write(xml, xmlFile, windows1252);
assertEquals(java, Files.toString(appFile, iso8859));
assertEquals(java, Files.toString(lib1File, macRoman));
assertEquals(java, Files.toString(lib2File, windows1252));
assertEquals(xml, Files.toString(xmlFile, windows1252));
// Make sure that these contents don't happen to be the same regardless of encoding
assertFalse(java.equals(Files.toString(appFile, UTF_8)));
assertFalse(java.equals(Files.toString(lib1File, UTF_8)));
assertFalse(java.equals(Files.toString(lib2File, UTF_8)));
assertFalse(xml.equals(Files.toString(xmlFile, UTF_8)));
// Write App project specific encoding, and file specific encoding
File file = new File(root, "App" + separator + ".settings" + separator + "org.eclipse.core.resources.prefs");
file.getParentFile().mkdirs();
Files.write("" + "eclipse.preferences.version=1\n" + "encoding//src/test/pkg/Text.java=" + iso8859.name() + "\n" + "encoding/<project>=" + macRoman.name(), file, Charsets.US_ASCII);
// Write Lib1 project specific encoding
file = new File(root, "Lib1" + separator + ".settings" + separator + "org.eclipse.core.resources.prefs");
file.getParentFile().mkdirs();
Files.write("" + "eclipse.preferences.version=1\n" + "encoding/<project>=" + macRoman.name(), file, Charsets.US_ASCII);
// Write workspace default encoding, used for the Lib2 file
final File workspace = new File(root, "workspace");
workspace.mkdirs();
File metadata = new File(workspace, ".metadata");
metadata.mkdirs();
new File(metadata, "version.ini").createNewFile();
assertTrue(isEclipseWorkspaceDir(workspace));
File resourceFile = new File(workspace, (".metadata/.plugins/org.eclipse.core.runtime/" + ".settings/org.eclipse.core.resources.prefs").replace('/', separatorChar));
resourceFile.getParentFile().mkdirs();
Files.write("" + "eclipse.preferences.version=1\n" + "encoding=" + windows1252.name() + "\n" + "version=1", resourceFile, Charsets.US_ASCII);
File imported = checkProject(app, "" + MSG_HEADER + MSG_MANIFEST + MSG_UNHANDLED + "From App:\n" + "* .gitignore\n" + "From JavaLib:\n" + "* .gitignore\n" + MSG_FOLDER_STRUCTURE + "In JavaLib:\n" + "* src/ => javaLib/src/main/java/\n" + "In Lib1:\n" + "* AndroidManifest.xml => lib1/src/main/AndroidManifest.xml\n" + "* src/ => lib1/src/main/java/\n" + "In Lib2:\n" + "* AndroidManifest.xml => lib2/src/main/AndroidManifest.xml\n" + "* src/ => lib2/src/main/java/\n" + "In App:\n" + "* AndroidManifest.xml => app/src/main/AndroidManifest.xml\n" + "* res/ => app/src/main/res/\n" + "* src/ => app/src/main/java/\n" + MSG_FOOTER, false, /* checkBuild */
importer -> importer.setEclipseWorkspace(workspace));
// Read back text files *as UTF-8* and make sure it's correct
File newAppFile = new File(imported, "app/src/main/java/test/pkg/Text.java".replace('/', separatorChar));
File newLib1File = new File(imported, "lib1/src/main/java/test/pkg/Text.java".replace('/', separatorChar));
File newLib2File = new File(imported, "lib2/src/main/java/test/pkg/Text.java".replace('/', separatorChar));
File newXmlFile = new File(imported, "app/src/main/res/layout/foo.xml".replace('/', separatorChar));
assertTrue(newAppFile.exists());
assertTrue(newLib1File.exists());
assertTrue(newLib2File.exists());
assertTrue(newXmlFile.exists());
assertEquals(java, Files.toString(newAppFile, UTF_8));
assertEquals(java, Files.toString(newLib1File, UTF_8));
assertEquals(java, Files.toString(newLib2File, UTF_8));
// references old encoding
assertFalse(xml.equals(Files.toString(newXmlFile, UTF_8)));
assertEquals(xml.replace(windows1252.name(), "utf-8"), Files.toString(newXmlFile, UTF_8));
deleteDir(root);
deleteDir(imported);
}
use of java.nio.charset.UnsupportedCharsetException in project XobotOS by xamarin.
the class Properties method storeToXML.
/**
* Writes all properties stored in this instance into the {@code
* OutputStream} in XML representation. The DOCTYPE is
*
* <pre>
* <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
* </pre>
*
* If the comment is null, no comment is added to the output. The parameter
* {@code encoding} defines which encoding should be used. The {@code
* OutputStream} is not closed at the end.
*
* @param os the {@code OutputStream} to write to.
* @param comment the comment to add. If null, no comment is added.
* @param encoding the code identifying the encoding that should be used to
* write into the {@code OutputStream}.
* @throws IOException if an error occurs during writing to the output.
*/
public synchronized void storeToXML(OutputStream os, String comment, String encoding) throws IOException {
if (os == null || encoding == null) {
throw new NullPointerException();
}
/*
* We can write to XML file using encoding parameter but note that some
* aliases for encodings are not supported by the XML parser. Thus we
* have to know canonical name for encoding used to store data in XML
* since the XML parser must recognize encoding name used to store data.
*/
String encodingCanonicalName;
try {
encodingCanonicalName = Charset.forName(encoding).name();
} catch (IllegalCharsetNameException e) {
System.out.println("Warning: encoding name " + encoding + " is illegal, using UTF-8 as default encoding");
encodingCanonicalName = "UTF-8";
} catch (UnsupportedCharsetException e) {
System.out.println("Warning: encoding " + encoding + " is not supported, using UTF-8 as default encoding");
encodingCanonicalName = "UTF-8";
}
PrintStream printStream = new PrintStream(os, false, encodingCanonicalName);
printStream.print("<?xml version=\"1.0\" encoding=\"");
printStream.print(encodingCanonicalName);
printStream.println("\"?>");
printStream.print("<!DOCTYPE properties SYSTEM \"");
printStream.print(PROP_DTD_NAME);
printStream.println("\">");
printStream.println("<properties>");
if (comment != null) {
printStream.print("<comment>");
printStream.print(substitutePredefinedEntries(comment));
printStream.println("</comment>");
}
for (Map.Entry<Object, Object> entry : entrySet()) {
String keyValue = (String) entry.getKey();
String entryValue = (String) entry.getValue();
printStream.print("<entry key=\"");
printStream.print(substitutePredefinedEntries(keyValue));
printStream.print("\">");
printStream.print(substitutePredefinedEntries(entryValue));
printStream.println("</entry>");
}
printStream.println("</properties>");
printStream.flush();
}
Aggregations