diff --git a/.github/scripts/generate-quality-report.py b/.github/scripts/generate-quality-report.py index d9381a299e..1f008d0dae 100755 --- a/.github/scripts/generate-quality-report.py +++ b/.github/scripts/generate-quality-report.py @@ -815,6 +815,7 @@ def main() -> None: cross_project_rules = { "DE_MIGHT_IGNORE", "BC_IMPOSSIBLE_CAST", + "DM_DEFAULT_ENCODING" } cross_project_violations: List[Tuple[str, Finding]] = [] for label, report in spotbugs_reports.items(): diff --git a/CodenameOne/src/com/codename1/io/Util.java b/CodenameOne/src/com/codename1/io/Util.java index 2cd109de41..677cbb44a8 100644 --- a/CodenameOne/src/com/codename1/io/Util.java +++ b/CodenameOne/src/com/codename1/io/Util.java @@ -2617,6 +2617,9 @@ public static Date toDateValue(Object o) { public static String xorDecode(String s) { try { byte[] dat = Base64.decode(s.getBytes("UTF-8")); + if (dat == null) { + return ""; + } for (int iter = 0; iter < dat.length; iter++) { dat[iter] = (byte) (dat[iter] ^ (iter % 254 + 1)); } diff --git a/CodenameOne/src/com/codename1/ui/Image.java b/CodenameOne/src/com/codename1/ui/Image.java index a8838289b1..0f4532de9b 100644 --- a/CodenameOne/src/com/codename1/ui/Image.java +++ b/CodenameOne/src/com/codename1/ui/Image.java @@ -426,25 +426,25 @@ public static Image createImage(byte[] bytes, int offset, int len) { /// SuccessCallback callback = (String capturedPhoto) -> { /// String rotatedPhoto = FileSystemStorage.getInstance().getAppHomePath() + "rotatedPhoto.jpg"; /// if (capturedPhoto != null) { - /// try { - /// // note: we set a maxSize to perform a faster rotation - /// int maxSize = CN.convertToPixels(50); - /// Image img = Image.exifRotation(capturedPhoto, rotatedPhoto, maxSize); - /// imageLbl.setIcon(img); - /// hi.revalidate(); - /// } catch (IOException ex) { - /// Log.e(ex); - /// } + /// try { + /// // note: we set a maxSize to perform a faster rotation + /// int maxSize = CN.convertToPixels(50); + /// Image img = Image.exifRotation(capturedPhoto, rotatedPhoto, maxSize); + /// imageLbl.setIcon(img); + /// hi.revalidate(); + /// } catch (IOException ex) { + /// Log.e(ex); + /// } /// }; /// }; /// cameraBtn.addActionListener(a -> Capture.capturePhoto(l -> { /// if (l != null && l.getSource() != null) { - /// callback.onSucess((String) l.getSource()); + /// callback.onSucess((String) l.getSource()); /// } /// })); /// galleryBtn.addActionListener(a -> CN.openGallery(l -> { /// if (l != null && l.getSource() != null) { - /// callback.onSucess((String) l.getSource()); + /// callback.onSucess((String) l.getSource()); /// } /// }, CN.GALLERY_IMAGE)); /// ``` @@ -492,25 +492,25 @@ public static Image exifRotation(String capturedImage) throws IOException { /// SuccessCallback callback = (String capturedPhoto) -> { /// String rotatedPhoto = FileSystemStorage.getInstance().getAppHomePath() + "rotatedPhoto.jpg"; /// if (capturedPhoto != null) { - /// try { - /// // note: we set a maxSize to perform a faster rotation - /// int maxSize = CN.convertToPixels(50); - /// Image img = Image.exifRotation(capturedPhoto, rotatedPhoto, maxSize); - /// imageLbl.setIcon(img); - /// hi.revalidate(); - /// } catch (IOException ex) { - /// Log.e(ex); - /// } + /// try { + /// // note: we set a maxSize to perform a faster rotation + /// int maxSize = CN.convertToPixels(50); + /// Image img = Image.exifRotation(capturedPhoto, rotatedPhoto, maxSize); + /// imageLbl.setIcon(img); + /// hi.revalidate(); + /// } catch (IOException ex) { + /// Log.e(ex); + /// } /// }; /// }; /// cameraBtn.addActionListener(a -> Capture.capturePhoto(l -> { /// if (l != null && l.getSource() != null) { - /// callback.onSucess((String) l.getSource()); + /// callback.onSucess((String) l.getSource()); /// } /// })); /// galleryBtn.addActionListener(a -> CN.openGallery(l -> { /// if (l != null && l.getSource() != null) { - /// callback.onSucess((String) l.getSource()); + /// callback.onSucess((String) l.getSource()); /// } /// }, CN.GALLERY_IMAGE)); /// ``` @@ -567,25 +567,25 @@ public static Image exifRotation(String capturedImage, String rotatedImage) thro /// SuccessCallback callback = (String capturedPhoto) -> { /// String rotatedPhoto = FileSystemStorage.getInstance().getAppHomePath() + "rotatedPhoto.jpg"; /// if (capturedPhoto != null) { - /// try { - /// // note: we set a maxSize to perform a faster rotation - /// int maxSize = CN.convertToPixels(50); - /// Image img = Image.exifRotation(capturedPhoto, rotatedPhoto, maxSize); - /// imageLbl.setIcon(img); - /// hi.revalidate(); - /// } catch (IOException ex) { - /// Log.e(ex); - /// } + /// try { + /// // note: we set a maxSize to perform a faster rotation + /// int maxSize = CN.convertToPixels(50); + /// Image img = Image.exifRotation(capturedPhoto, rotatedPhoto, maxSize); + /// imageLbl.setIcon(img); + /// hi.revalidate(); + /// } catch (IOException ex) { + /// Log.e(ex); + /// } /// }; /// }; /// cameraBtn.addActionListener(a -> Capture.capturePhoto(l -> { /// if (l != null && l.getSource() != null) { - /// callback.onSucess((String) l.getSource()); + /// callback.onSucess((String) l.getSource()); /// } /// })); /// galleryBtn.addActionListener(a -> CN.openGallery(l -> { /// if (l != null && l.getSource() != null) { - /// callback.onSucess((String) l.getSource()); + /// callback.onSucess((String) l.getSource()); /// } /// }, CN.GALLERY_IMAGE)); /// ``` diff --git a/Ports/Android/src/com/codename1/impl/android/AndroidImplementation.java b/Ports/Android/src/com/codename1/impl/android/AndroidImplementation.java index 8fdad9c2c8..201f19b0ad 100644 --- a/Ports/Android/src/com/codename1/impl/android/AndroidImplementation.java +++ b/Ports/Android/src/com/codename1/impl/android/AndroidImplementation.java @@ -81,6 +81,7 @@ import java.lang.ref.SoftReference; import java.lang.reflect.Method; import java.net.URISyntaxException; +import java.nio.charset.StandardCharsets; import java.util.Vector; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; @@ -563,11 +564,11 @@ private static Map splitQuery(String urlencodeQueryString) { public String getStackTrace(Thread parentThread, Throwable t) { System.out.println("CN1SS:ERR:Invoking getStackTrace in AndroidImplementation"); ByteArrayOutputStream bos = new ByteArrayOutputStream(); - PrintWriter w = new PrintWriter(bos); + PrintWriter w = new PrintWriter(new OutputStreamWriter(bos, StandardCharsets.UTF_8)); t.printStackTrace(w); w.close(); System.out.println("CN1SS:ERR:AndroidImplementation getStackTrace completed"); - return bos.toString(); + return new String(bos.toByteArray(), StandardCharsets.UTF_8); } public static void initPushContent(String message, String image, String messageType, String category, Context context) { @@ -6538,7 +6539,7 @@ private String[] getStorageDirectories() { BufferedReader bufReader = null; try { - bufReader = new BufferedReader(new FileReader("/proc/mounts")); + bufReader = new BufferedReader(new InputStreamReader(new FileInputStream("/proc/mounts"), StandardCharsets.UTF_8)); ArrayList list = new ArrayList(); String line; diff --git a/Ports/Android/src/com/codename1/impl/android/FridaDetectionUtil.java b/Ports/Android/src/com/codename1/impl/android/FridaDetectionUtil.java index 25cd9db6b7..bf455acac5 100644 --- a/Ports/Android/src/com/codename1/impl/android/FridaDetectionUtil.java +++ b/Ports/Android/src/com/codename1/impl/android/FridaDetectionUtil.java @@ -8,6 +8,7 @@ import java.io.FileInputStream; import java.io.InputStreamReader; import java.lang.reflect.Method; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; @@ -32,7 +33,7 @@ public class FridaDetectionUtil { public static boolean isFridaProcessRunning() { try { Process process = Runtime.getRuntime().exec("ps"); - BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); + BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8)); String line; while ((line = reader.readLine()) != null) { for (String processName : FRIDA_PROCESSES) { @@ -54,7 +55,7 @@ public static boolean isFridaLibraryLoaded() { BufferedReader reader = null; try { FileInputStream fis = new FileInputStream(new File("/proc/self/maps")); - reader = new BufferedReader(new InputStreamReader(fis)); + reader = new BufferedReader(new InputStreamReader(fis, StandardCharsets.UTF_8)); String line; while ((line = reader.readLine()) != null) { for (String lib : FRIDA_LIBRARIES) { diff --git a/Ports/Android/src/com/codename1/impl/android/util/Base64.java b/Ports/Android/src/com/codename1/impl/android/util/Base64.java index e9ae76ced6..19ba95f53b 100644 --- a/Ports/Android/src/com/codename1/impl/android/util/Base64.java +++ b/Ports/Android/src/com/codename1/impl/android/util/Base64.java @@ -31,6 +31,8 @@ * @version 1.3 */ +import java.nio.charset.StandardCharsets; + /** * Base64 converter class. This code is not a complete MIME encoder; * it simply converts binary data to base64 data and back. @@ -283,7 +285,7 @@ public static String encode(byte[] source, int off, int len, byte[] alphabet, outLen -= 1; } - return new String(outBuff, 0, outLen); + return new String(outBuff, 0, outLen, StandardCharsets.UTF_8); } /** @@ -416,7 +418,7 @@ private static int decode4to3(byte[] source, int srcOffset, * @since 1.4 */ public static byte[] decode(String s) throws Base64DecoderException { - byte[] bytes = s.getBytes(); + byte[] bytes = s.getBytes(StandardCharsets.UTF_8); return decode(bytes, 0, bytes.length); } @@ -428,7 +430,7 @@ public static byte[] decode(String s) throws Base64DecoderException { * @return the decoded data */ public static byte[] decodeWebSafe(String s) throws Base64DecoderException { - byte[] bytes = s.getBytes(); + byte[] bytes = s.getBytes(StandardCharsets.UTF_8); return decodeWebSafe(bytes, 0, bytes.length); } diff --git a/maven/codenameone-maven-plugin/src/main/java/com/codename1/builders/AndroidGradleBuilder.java b/maven/codenameone-maven-plugin/src/main/java/com/codename1/builders/AndroidGradleBuilder.java index 7cea0473d6..1d4166f01d 100644 --- a/maven/codenameone-maven-plugin/src/main/java/com/codename1/builders/AndroidGradleBuilder.java +++ b/maven/codenameone-maven-plugin/src/main/java/com/codename1/builders/AndroidGradleBuilder.java @@ -45,7 +45,9 @@ import java.net.URL; import java.net.URLClassLoader; import java.nio.channels.FileChannel; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.util.ArrayList; import java.util.HashMap; @@ -79,8 +81,6 @@ public class AndroidGradleBuilder extends Executor { private float MIN_GRADLE_VERSION=6; - private float MIN_JDK_VERSION=8; - private String gradleDistributionUrl = "https://services.gradle.org/distributions/gradle-6.8.3-bin.zip"; private String gradle8DistributionUrl = "https://services.gradle.org/distributions/gradle-8.1-bin.zip"; @@ -404,7 +404,7 @@ public File getGradleProjectDirectory() { private boolean shouldIncludeGoogleImpl; static { - isMac = System.getProperty("os.name").toLowerCase().indexOf("mac") > -1; + isMac = System.getProperty("os.name").toLowerCase().contains("mac"); } public void setAndroidPortSrcJar(File androidPortSrcJar) { @@ -429,8 +429,7 @@ protected long getTimeoutValue() { } private String getGradleVersion(String gradleExe) throws Exception { - Map env = defaultEnvironment; - env.put("JAVA_HOME", getGradleJavaHome()); + defaultEnvironment.put("JAVA_HOME", getGradleJavaHome()); String result = execString(new File(System.getProperty("user.dir")), gradleExe, "--version"); Scanner scanner = new Scanner(result); @@ -689,7 +688,7 @@ public boolean build(File sourceZip, final BuildRequest request) throws BuildExc buildToolsVersionInt = maxBuildToolsVersionInt; - this.buildToolsVersion = request.getArg("android.buildToolsVersion", ""+maxBuildToolsVersion); + this.buildToolsVersion = request.getArg("android.buildToolsVersion", maxBuildToolsVersion); String buildToolsVersionIntStr = this.buildToolsVersion; if (buildToolsVersionIntStr.indexOf(".") > 1) { buildToolsVersionIntStr = buildToolsVersionIntStr.substring(0, buildToolsVersionIntStr.indexOf(".")); @@ -1549,7 +1548,7 @@ public void usesClassMethod(String cls, String method) { + " }\n" + "}"; try { - createFile(headphonesFile, stubSourceCode.getBytes()); + createFile(headphonesFile, stubSourceCode.getBytes(StandardCharsets.UTF_8)); } catch (IOException ex) { throw new BuildException("Failed to create HeadSetReceiver class", ex); } @@ -1817,19 +1816,18 @@ public void usesClassMethod(String cls, String method) { + ""; try { - OutputStream stringsSourceStream = new FileOutputStream(stringsFile); - stringsSourceStream.write(stringsFileContent.getBytes()); + OutputStream stringsSourceStream = Files.newOutputStream(stringsFile.toPath()); + stringsSourceStream.write(stringsFileContent.getBytes(StandardCharsets.UTF_8)); stringsSourceStream.close(); - String locales = request.getArg("android.locales", null); if (locales != null && locales.length() > 0) { for (String loc : locales.split(";")) { File currentValuesDir = new File(valsDir.getParent(), "values-" + loc); currentValuesDir.mkdirs(); File currentStringsFile = new File(currentValuesDir, "strings.xml"); - stringsSourceStream = new FileOutputStream(currentStringsFile); - stringsSourceStream.write(stringsFileContent.getBytes()); + stringsSourceStream = Files.newOutputStream(currentStringsFile.toPath()); + stringsSourceStream.write(stringsFileContent.getBytes(StandardCharsets.UTF_8)); stringsSourceStream.close(); } } @@ -1880,11 +1878,11 @@ public void usesClassMethod(String cls, String method) { try { OutputStream stylesSourceStream = new FileOutputStream(stylesFile); - stylesSourceStream.write(stylesFileContent.getBytes()); + stylesSourceStream.write(stylesFileContent.getBytes(StandardCharsets.UTF_8)); stylesSourceStream.close(); String theme = request.getArg("android.theme", "Light"); - if (theme.length() > 0 && theme.equalsIgnoreCase("Dark")) { + if (theme.equalsIgnoreCase("Dark")) { theme = ""; } else { theme = "." + theme; @@ -1905,7 +1903,7 @@ public void usesClassMethod(String cls, String method) { OutputStream styles11SourceStream = new FileOutputStream(styles11File); - styles11SourceStream.write(styles11FileContent.getBytes()); + styles11SourceStream.write(styles11FileContent.getBytes(StandardCharsets.UTF_8)); styles11SourceStream.close(); File styles21File = new File(vals21Dir, "styles.xml"); @@ -1924,7 +1922,7 @@ public void usesClassMethod(String cls, String method) { OutputStream styles21SourceStream = new FileOutputStream(styles21File); - styles21SourceStream.write(styles21FileContent.getBytes()); + styles21SourceStream.write(styles21FileContent.getBytes(StandardCharsets.UTF_8)); styles21SourceStream.close(); } catch (IOException ex) { error("Failed to generate style files", ex); @@ -1942,7 +1940,7 @@ public void usesClassMethod(String cls, String method) { + mopubBannerXML + "\n"; OutputStream layoutSourceStream = new FileOutputStream(layoutFile); - layoutSourceStream.write(layoutFileContent.getBytes()); + layoutSourceStream.write(layoutFileContent.getBytes(StandardCharsets.UTF_8)); layoutSourceStream.close(); String customLayout = request.getArg("android.cusom_layout1", null); @@ -1950,7 +1948,7 @@ public void usesClassMethod(String cls, String method) { while (customLayout != null) { File customFile = new File(layoutDir, "cusom_layout" + counter + ".xml"); layoutSourceStream = new FileOutputStream(customFile); - layoutSourceStream.write(customLayout.getBytes()); + layoutSourceStream.write(customLayout.getBytes(StandardCharsets.UTF_8)); layoutSourceStream.close(); counter++; @@ -1979,7 +1977,7 @@ public void usesClassMethod(String cls, String method) { try { JSONParser parser = new JSONParser(); - Map parsedJson = parser.parseJSON(new FileReader(googleServicesJson)); + Map parsedJson = parser.parseJSON(new InputStreamReader(new FileInputStream(googleServicesJson), StandardCharsets.UTF_8)); Map projectInfo = (Map) parsedJson.get("project_info"); gcmSenderId = (String) projectInfo.get("project_number"); @@ -2279,7 +2277,7 @@ public void usesClassMethod(String cls, String method) { try { OutputStream filePathsStream = new FileOutputStream(filePathsFile); - filePathsStream.write(filePathsContent.getBytes()); + filePathsStream.write(filePathsContent.getBytes(StandardCharsets.UTF_8)); filePathsStream.close(); } catch (IOException ex) { throw new BuildException("Failed to write file path providers file", ex); @@ -2384,7 +2382,7 @@ public void usesClassMethod(String cls, String method) { + "\n"; try { OutputStream manifestSourceStream = new FileOutputStream(manifestFile); - manifestSourceStream.write(manifestSource.getBytes()); + manifestSourceStream.write(manifestSource.getBytes(StandardCharsets.UTF_8)); manifestSourceStream.close(); } catch (IOException ex) { throw new BuildException("Failed to write manifest file", ex); @@ -2629,7 +2627,7 @@ public void usesClassMethod(String cls, String method) { + "\n\n" + "public class " + request.getMainClass() + "Stub extends " + request.getArg("android.customActivity", "CodenameOneActivity") + "{\n"; stubSourceCode += decodeFunction(); - stubSourceCode += " public static final String BUILD_KEY = \"" + xorEncode(getBuildKey()) + "\";\n" + stubSourceCode += " public static final String BUILD_KEY = \"LOCAL_BUILD\";\n" + " public static final String PACKAGE_NAME = \"" + request.getPackageName() + "\";\n" + " public static final String BUILT_BY_USER = \"" + xorEncode(request.getUserName()) + "\";\n" + " public static final String LICENSE_KEY = \"" + xorEncode(licenseKey) + "\";\n" @@ -2973,7 +2971,7 @@ public void usesClassMethod(String cls, String method) { + " public static final String C2DM_MESSAGE_EXTRA = \"message\";\n" + " public static final String C2DM_MESSAGE_IMAGE = \"image\";\n" + " public static final String C2DM_MESSAGE_CATEGORY = \"category\";\n" - + " public static final String BUILD_KEY = \"" + xorEncode(getBuildKey()) + "\"\n;" + + " public static final String BUILD_KEY = \"LOCAL_BUILD\"\n;" + " public static final String PACKAGE_NAME = \"" + request.getPackageName() + "\"\n;" + " public static final String BUILT_BY_USER = \"" + xorEncode(request.getUserName()) + "\"\n;" + " private static String KEY = \"c2dmPref\";\n" @@ -3210,11 +3208,11 @@ public void usesClassMethod(String cls, String method) { if (pushPermission) { try { OutputStream pushSourceStream = new FileOutputStream(pushFileSourceFile); - pushSourceStream.write(pushReceiverSourceCode.getBytes()); + pushSourceStream.write(pushReceiverSourceCode.getBytes(StandardCharsets.UTF_8)); pushSourceStream.close(); OutputStream pushServiceSourceStream = new FileOutputStream(pushServiceFileSourceFile); - pushServiceSourceStream.write(pushServiceSourceCode.getBytes()); + pushServiceSourceStream.write(pushServiceSourceCode.getBytes(StandardCharsets.UTF_8)); pushServiceSourceStream.close(); } catch (IOException ex) { throw new BuildException("Failed to generate push file", ex); @@ -3260,7 +3258,7 @@ public void usesClassMethod(String cls, String method) { } try { OutputStream stubSourceStream = new FileOutputStream(stubFileSourceFile); - stubSourceStream.write(stubSourceCode.getBytes()); + stubSourceStream.write(stubSourceCode.getBytes(StandardCharsets.UTF_8)); stubSourceStream.close(); } catch (IOException ex) { throw new BuildException("Failed to write stub source file", ex); @@ -3374,7 +3372,7 @@ public void usesClassMethod(String cls, String method) { proguardConfigOverrideFile.delete(); if (request.getArg("android.enableProguard", "true").equals("true")) { try { - createFile(proguardConfigOverrideFile, proguardConfigOverride.getBytes()); + createFile(proguardConfigOverrideFile, proguardConfigOverride.getBytes(StandardCharsets.UTF_8)); } catch (IOException ex) { throw new BuildException("Failed to create proguard config file", ex); } @@ -3748,7 +3746,7 @@ public void usesClassMethod(String cls, String method) { try { OutputStream gradleStream = new FileOutputStream(gradleFile); - gradleStream.write(gradleProps.getBytes()); + gradleStream.write(gradleProps.getBytes(StandardCharsets.UTF_8)); gradleStream.close(); } catch (IOException ex) { throw new BuildException("Failed to write gradle properties to "+gradleFile, ex); @@ -3782,7 +3780,7 @@ public void usesClassMethod(String cls, String method) { File rootGradleFile = new File(studioProjectDir, "build.gradle"); try { OutputStream gradleStream = new FileOutputStream(rootGradleFile); - gradleStream.write(rootGradleProps.getBytes()); + gradleStream.write(rootGradleProps.getBytes(StandardCharsets.UTF_8)); gradleStream.close(); } catch (IOException ex) { throw new BuildException("Failed to write root gradle properties to "+rootGradleFile, ex); @@ -3986,7 +3984,7 @@ public void extract(InputStream source, File dir, String sdkPath) throws IOExcep destFile = new File(d, "local.properties"); destFile.getParentFile().mkdirs(); FileOutputStream fos = new FileOutputStream(destFile); - fos.write(sdkPathProperties.getBytes()); + fos.write(sdkPathProperties.getBytes(StandardCharsets.UTF_8)); fos.close(); addedSDKDir = true; } @@ -4002,7 +4000,7 @@ public void extract(InputStream source, File dir, String sdkPath) throws IOExcep destFile = new File(dir, entry.getName()); destFile.getParentFile().mkdirs(); FileOutputStream fos = new FileOutputStream(destFile); - fos.write(sdkPathProperties.getBytes()); + fos.write(sdkPathProperties.getBytes(StandardCharsets.UTF_8)); fos.close(); continue; } @@ -4053,7 +4051,7 @@ public void extractAAR(InputStream source, File dir, String sdkPath) throws IOEx FileOutputStream projectProps = new FileOutputStream(new File(dir, "project.properties")); String props = "android.library=true\n" + "target=android-14"; - projectProps.write(props.getBytes()); + projectProps.write(props.getBytes(StandardCharsets.UTF_8)); projectProps.close(); } catch (Exception e) { @@ -4432,10 +4430,9 @@ private void replaceInTree(File root, Map replacements, FilenameF private void replaceInFile(File file, Map replacements) throws IOException { String contents = readFileToString(file); contents = replace(contents, replacements); - FileWriter fios = new FileWriter(file); + Writer fios = new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8); fios.write(contents); fios.close(); - } private Map androidXArtifactMapping; private Map loadAndroidXArtifactMapping() throws IOException { diff --git a/maven/codenameone-maven-plugin/src/main/java/com/codename1/builders/Base64.java b/maven/codenameone-maven-plugin/src/main/java/com/codename1/builders/Base64.java index b23ee7e43b..faece2814e 100644 --- a/maven/codenameone-maven-plugin/src/main/java/com/codename1/builders/Base64.java +++ b/maven/codenameone-maven-plugin/src/main/java/com/codename1/builders/Base64.java @@ -15,15 +15,14 @@ * limitations under the License. */ -/** -* @author Alexander Y. Kleymenov -*/ - package com.codename1.builders; +import java.nio.charset.StandardCharsets; + /** * This class implements Base64 encoding/decoding functionality * as specified in RFC 2045 (http://www.ietf.org/rfc/rfc2045.txt). + * @author Alexander Y. Kleymenov */ public class Base64 { @@ -63,7 +62,7 @@ public static byte[] decode(byte[] in, int len) { // index in the input array int in_index = 0; // holds the value of the input character - int bits = 0; + int bits; // holds the value of the input quantum int quantum = 0; for (int i=0; i[] nativeInterfaces; private boolean unitTestMode; - private String platform; static boolean IS_MAC; protected final Map defaultEnvironment = new HashMap(); @@ -129,15 +119,6 @@ public File getBuildDirectory() { } public void setId(String buildId) { - this.buildId = buildId; - } - - public void setPlatform(String p) { - this.platform = p; - } - - public static void disableDelete() { - disableDelete = true; } public void cleanup() { @@ -170,21 +151,19 @@ public void replaceInFile(File sourceFile, String marker, String newValue) throw byte[] data = new byte[(int) sourceFile.length()]; dis.readFully(data); dis.close(); - FileWriter fios = new FileWriter(sourceFile); - String str = new String(data); - str = str.replace(marker, newValue); - fios.write(str); - fios.close(); + try(Writer fios = new OutputStreamWriter(Files.newOutputStream(sourceFile.toPath()), StandardCharsets.UTF_8)) { + String str = new String(data, StandardCharsets.UTF_8); + str = str.replace(marker, newValue); + fios.write(str); + } } public String readFileToString(File sourceFile) throws IOException { - DataInputStream dis = new DataInputStream(new FileInputStream(sourceFile)); + DataInputStream dis = new DataInputStream(Files.newInputStream(sourceFile.toPath())); byte[] data = new byte[(int) sourceFile.length()]; dis.readFully(data); dis.close(); - //FileWriter fios = new FileWriter(sourceFile); - String str = new String(data); - return str; + return new String(data, StandardCharsets.UTF_8); } public boolean findInFile(File sourceFile, String marker) throws IOException { @@ -192,12 +171,8 @@ public boolean findInFile(File sourceFile, String marker) throws IOException { byte[] data = new byte[(int) sourceFile.length()]; dis.readFully(data); dis.close(); - //FileWriter fios = new FileWriter(sourceFile); - String str = new String(data); + String str = new String(data, StandardCharsets.UTF_8); return str.contains(marker); - //str = str.replace(marker, newValue); - //fios.write(str); - //fios.close(); } public void replaceAllInFile(File sourceFile, String marker, String newValue) throws IOException { @@ -205,11 +180,11 @@ public void replaceAllInFile(File sourceFile, String marker, String newValue) th byte[] data = new byte[(int) sourceFile.length()]; dis.readFully(data); dis.close(); - FileWriter fios = new FileWriter(sourceFile); - String str = new String(data); - str = str.replaceAll(marker, newValue); - fios.write(str); - fios.close(); + try(Writer fios = new OutputStreamWriter(Files.newOutputStream(sourceFile.toPath()), StandardCharsets.UTF_8)) { + String str = new String(data, StandardCharsets.UTF_8); + str = str.replaceAll(marker, newValue); + fios.write(str); + } } File includeSources(BuildRequest request) throws Exception { @@ -748,7 +723,7 @@ protected String registerNativeImplementationsAndCreateStubs(ClassLoader parentC javaImplSourceFile += "}\n"; FileOutputStream out = new FileOutputStream(javaFile); - out.write(javaImplSourceFile.getBytes()); + out.write(javaImplSourceFile.getBytes(StandardCharsets.UTF_8)); out.close(); } } @@ -1436,13 +1411,9 @@ protected File placeXMLFile(ZipEntry entry, File xmlDir, File resDir) { name = name.substring(0, name.lastIndexOf("/")); if (name.contains("/")) { name = name.substring(name.lastIndexOf("/")); - if (name.equalsIgnoreCase("xml")) { - putInXMLDir = true; - } - } else { - if (name.equalsIgnoreCase("xml")) { - putInXMLDir = true; - } + } + if (name.equalsIgnoreCase("xml")) { + putInXMLDir = true; } name = entry.getName(); name = name.substring(name.lastIndexOf("/") + 1, name.length()); @@ -1622,7 +1593,7 @@ public void run() { byte[] buffer = new byte[8192]; int i = stream.read(buffer); while (i > -1) { - String str = new String(buffer, 0, i); + String str = new String(buffer, 0, i, StandardCharsets.UTF_8); log(str, false); outputMessage.append(str); i = stream.read(buffer); @@ -1757,32 +1728,19 @@ static void addDir(File baseDir, File dirObj, ZipOutputStream out) throws IOExce static void addDir(File baseDir, File dirObj, ZipOutputStream out, String... exclude) throws IOException { File[] files = dirObj.listFiles(); + if(files == null) { + return; + } byte[] tmpBuf = new byte[8192]; for (int i = 0; i < files.length; i++) { if (files[i].isDirectory()) { - boolean found = false; - if (exclude != null) { - List excludeNames = new ArrayList(); - - for (String ex : exclude) { - if (ex.indexOf("/") > -1) { - // We only check for excludes at this level - } else { - excludeNames.add(ex); - } - } - for (String ex : excludeNames) { - if (files[i].getName().equalsIgnoreCase(ex)) { - found = true; - } - } - } + boolean found = isFound(exclude, files, i); if (!found) { - List newExcludes = new ArrayList(); + List newExcludes = new ArrayList<>(); if (exclude != null) { for (String ex : exclude) { - if (ex.indexOf("/") > -1) { + if (ex.contains("/")) { newExcludes.add(ex.substring(ex.indexOf("/")+1)); } } @@ -1803,6 +1761,25 @@ static void addDir(File baseDir, File dirObj, ZipOutputStream out, String... exc } } + private static boolean isFound(String[] exclude, File[] files, int i) { + if (exclude != null) { + List excludeNames = new ArrayList<>(); + + for (String ex : exclude) { + if (!ex.contains("/")) { + // We only check for excludes at this level + excludeNames.add(ex); + } + } + for (String ex : excludeNames) { + if (files[i].getName().equalsIgnoreCase(ex)) { + return true; + } + } + } + return false; + } + protected BufferedImage getScaledUnevenInstance(BufferedImage img, int targetWidth, int targetHeight) { @@ -1841,7 +1818,7 @@ protected BufferedImage getScaledUnevenInstance(BufferedImage img, protected BufferedImage getScaledInstance(BufferedImage img, int targetWidth, int targetHeight) { - BufferedImage ret = (BufferedImage) img; + BufferedImage ret = img; int w, h; // Use multi-step technique: start with original size, then // scale down in multiple passes with drawImage() @@ -1885,83 +1862,6 @@ protected BufferedImage getScaledInstance(BufferedImage img, return ret; } - /** - * @return the buildKey - */ - public String getBuildKey() { - return buildKey; - } - - /** - * @param buildKey the buildKey to set - */ - public void setBuildKey(String buildKey) { - this.buildKey = buildKey; - } - - - - protected boolean deriveGlobalInstrumentClasspath() { - return false; - } - - private void instrument(String classpath, File directory, File root, List methodNames) throws Exception { - for (File f : directory.listFiles()) { - if (f.isDirectory()) { - instrument(classpath, f, root, methodNames); - continue; - } - if (f.getName().endsWith(".class") && !f.getName().endsWith("CodenameOneThread.class")) { - ClassPool pool = new ClassPool(deriveGlobalInstrumentClasspath()); - pool.appendClassPath(root.getAbsolutePath()); - if (classpath != null) { - pool.appendClassPath(classpath); - } - String name = f.getAbsolutePath().substring(root.getAbsolutePath().length() + 1); - name = name.substring(0, name.length() - 6); - name = name.replace(File.separatorChar, '.');//.replace('$', '.'); - DataInputStream fi = new DataInputStream(new FileInputStream(f)); - CtClass cls = pool.makeClass(fi);//pool.get(name); - fi.close(); - CtClass runtimeException = pool.get("java.lang.RuntimeException"); - - methodNames.add(name); - CtMethod[] mtds = cls.getDeclaredMethods(); - for (CtMethod mtd : mtds) { - if (!mtd.isEmpty()) { - if (mtd.getMethodInfo().getCodeAttribute() != null) { - methodNames.add(mtd.getName()); - int mid = methodNames.size(); - mtd.insertBefore("{ com.codename1.impl.CodenameOneThread.push(" + mid + "); }"); - mtd.insertAfter("{ com.codename1.impl.CodenameOneThread.pop(); }", true); - mtd.addCatch("{ com.codename1.impl.CodenameOneThread.storeStack($e, " + mid + "); throw $e; }", runtimeException); - for (CtClass ex : mtd.getExceptionTypes()) { - mtd.addCatch("{ com.codename1.impl.CodenameOneThread.storeStack($e, " + mid + "); throw $e; }", ex); - } - } - } - } - CtConstructor[] cons = cls.getDeclaredConstructors(); - for (CtConstructor con : cons) { - if (!con.isEmpty()) { - if (con.getMethodInfo().getCodeAttribute() != null) { - methodNames.add(con.getName()); - int mid = methodNames.size(); - con.insertBefore("{ com.codename1.impl.CodenameOneThread.push(" + mid + "); }"); - con.insertAfter("{ com.codename1.impl.CodenameOneThread.pop(); }", true); - con.addCatch("{ com.codename1.impl.CodenameOneThread.storeStack($e, " + mid + "); throw $e; }", runtimeException); - for (CtClass ex : con.getExceptionTypes()) { - con.addCatch("{ com.codename1.impl.CodenameOneThread.storeStack($e, " + mid + "); throw $e; }", ex); - } - } - } - } - FileOutputStream fo = new FileOutputStream(f); - fo.write(cls.toBytecode()); - fo.close(); - } - } - } public File getResourceAsFile(String res, String extension) throws IOException { @@ -1982,20 +1882,7 @@ public File getResourceAsFile(String res, String extension) throws IOException { public InputStream getResourceAsStream(String res) { - InputStream s = Executor.class.getResourceAsStream(res); - if(s != null) { - return s; - } - - return null; - } - - /** - * Gets a potentially versioned file - */ - public File getFileObject(File f) { - return f; - + return Executor.class.getResourceAsStream(res); } protected boolean isUnitTestMode() { @@ -2046,7 +1933,7 @@ protected void generateUnitTestFiles(BuildRequest req, File stubDir) throws IOEx + " }\n\n\n" + " public void destroy() {\n" + " }\n\n\n" - + "}\n").getBytes()); + + "}\n").getBytes(StandardCharsets.UTF_8)); fo.close(); } } @@ -2060,34 +1947,19 @@ public String decodeFunction() { } public String xorEncode(String s) { - try { - if(s == null) { - return null; - } - byte[] dat = s.getBytes("UTF-8"); - for(int iter = 0 ; iter < dat.length ; iter++) { - dat[iter] = (byte)(dat[iter] ^ (iter % 254 + 1)); - } - return Base64.encodeNoNewline(dat); - } catch(UnsupportedEncodingException err) { - // will never happen damn stupid exception - err.printStackTrace(); + if(s == null) { return null; } - } - - private ClassLoader getCodenameOneJarClassLoader() throws IOException { - if (codenameOneJar == null) { - throw new IllegalStateException("Must set codenameOneJar in Executor"); + byte[] dat = s.getBytes(StandardCharsets.UTF_8); + for(int iter = 0 ; iter < dat.length ; iter++) { + dat[iter] = (byte)(dat[iter] ^ (iter % 254 + 1)); } - return new URLClassLoader(new URL[]{codenameOneJar.toURI().toURL()}); + return Base64.encodeNoNewline(dat); } /** * Loads global local builder properties from user's home directory. - * @return - * @throws IOException */ protected Properties getLocalBuilderProperties() { if (localBuilderProperties == null) { @@ -2101,11 +1973,8 @@ protected Properties getLocalBuilderProperties() { } try { - FileInputStream fis = new FileInputStream(propertiesFile); - try { + try (FileInputStream fis = new FileInputStream(propertiesFile)) { localBuilderProperties.load(fis); - } finally { - fis.close(); } } catch (IOException ex) { throw new RuntimeException("Failed to load local properties", ex); diff --git a/maven/codenameone-maven-plugin/src/main/java/com/codename1/builders/IPhoneBuilder.java b/maven/codenameone-maven-plugin/src/main/java/com/codename1/builders/IPhoneBuilder.java index a583b5afdc..1e7787d769 100644 --- a/maven/codenameone-maven-plugin/src/main/java/com/codename1/builders/IPhoneBuilder.java +++ b/maven/codenameone-maven-plugin/src/main/java/com/codename1/builders/IPhoneBuilder.java @@ -35,6 +35,7 @@ import java.lang.reflect.Method; import java.net.URL; import java.net.URLClassLoader; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.StandardCopyOption; import java.util.*; @@ -141,7 +142,7 @@ private boolean validateLC_MIN_VERSION(File file) throws IOException { ProcessBuilder pb = new ProcessBuilder("otool", "-lv", file.getAbsolutePath()); Process p = pb.start(); InputStream is = p.getInputStream(); - Scanner scanner = new Scanner(is); + Scanner scanner = new Scanner(is, "UTF-8"); while (scanner.hasNextLine()) { String line = scanner.nextLine(); if (line.contains("LC_VERSION_MIN_")) { @@ -1021,7 +1022,7 @@ public void usesClassMethod(String cls, String method) { + " }\n" + "}\n"; - stubSourceStream.write(stubSourceCode.getBytes()); + stubSourceStream.write(stubSourceCode.getBytes(StandardCharsets.UTF_8)); } catch (IOException ex) { throw new BuildException("Failed to write stub source", ex); } @@ -1163,16 +1164,14 @@ public void usesClassMethod(String cls, String method) { try (FileOutputStream out = new FileOutputStream(javaFile)) { - out.write(javaImplSourceFile.getBytes()); - out.close(); + out.write(javaImplSourceFile.getBytes(StandardCharsets.UTF_8)); } catch (IOException ex) { throw new BuildException("Error while generating native interface stub for "+currentNative, ex); } File mFile = new File(resDir, "native_" + currentNative.getName().replace('.', '_') + "ImplCodenameOne.m"); try (FileOutputStream out = new FileOutputStream(mFile)) { - out.write(mSourceFile.getBytes()); - out.close(); + out.write(mSourceFile.getBytes(StandardCharsets.UTF_8)); } catch (IOException ex) { throw new BuildException("Error while generating native interface stub for "+currentNative, ex); } @@ -1229,22 +1228,22 @@ public void usesClassMethod(String cls, String method) { byte[] data = new byte[(int) appDelH.length()]; dis.readFully(data); dis.close(); - FileWriter fios = new FileWriter(appDelH); - String str = new String(data); - str = str.replace("//#define CN1_INCLUDE_NOTIFICATIONS", "#define CN1_INCLUDE_NOTIFICATIONS"); - fios.write(str); - fios.close(); + try(Writer fios = new OutputStreamWriter(Files.newOutputStream(appDelH.toPath()), StandardCharsets.UTF_8)) { + String str = new String(data, StandardCharsets.UTF_8); + str = str.replace("//#define CN1_INCLUDE_NOTIFICATIONS", "#define CN1_INCLUDE_NOTIFICATIONS"); + fios.write(str); + } File iosNative = new File(buildinRes, "IOSNative.m"); - dis = new DataInputStream(new FileInputStream(iosNative)); + dis = new DataInputStream(Files.newInputStream(iosNative.toPath())); data = new byte[(int) iosNative.length()]; dis.readFully(data); dis.close(); - fios = new FileWriter(iosNative); - str = new String(data); - str = str.replace("//#define CN1_INCLUDE_NOTIFICATIONS2", "#define CN1_INCLUDE_NOTIFICATIONS2"); - fios.write(str); - fios.close(); + try (Writer fios = new OutputStreamWriter(Files.newOutputStream(iosNative.toPath()), StandardCharsets.UTF_8)) { + String str = new String(data, StandardCharsets.UTF_8); + str = str.replace("//#define CN1_INCLUDE_NOTIFICATIONS2", "#define CN1_INCLUDE_NOTIFICATIONS2"); + fios.write(str); + } } catch (IOException ex) { log("Failed to Update Objective-C source files to activate notifications flag"); throw new BuildException("Failed to update Objective-C source files to activate notifications flag", ex); @@ -1255,26 +1254,27 @@ public void usesClassMethod(String cls, String method) { try { // special workaround for issue Apple is having with push notification missing from // the entitlements - DataInputStream dis = new DataInputStream(new FileInputStream(glAppDelegate)); byte[] data = new byte[(int) glAppDelegate.length()]; - dis.readFully(data); - dis.close(); - FileWriter fios = new FileWriter(glAppDelegate); - String str = new String(data); - str = str.replace("#define INCLUDE_CN1_PUSH", ""); - fios.write(str); - fios.close(); + try(DataInputStream dis = new DataInputStream(Files.newInputStream(glAppDelegate.toPath()))) { + dis.readFully(data); + } + + try(Writer fios = new OutputStreamWriter(Files.newOutputStream(glAppDelegate.toPath()), StandardCharsets.UTF_8)) { + String str = new String(data, StandardCharsets.UTF_8); + str = str.replace("#define INCLUDE_CN1_PUSH", ""); + fios.write(str); + } File iosNative = new File(buildinRes, "IOSNative.m"); - dis = new DataInputStream(new FileInputStream(iosNative)); - data = new byte[(int) iosNative.length()]; - dis.readFully(data); - dis.close(); - fios = new FileWriter(iosNative); - str = new String(data); - str = str.replace("#define INCLUDE_CN1_PUSH2", "//#define INCLUDE_CN1_PUSH2"); - fios.write(str); - fios.close(); + try(DataInputStream dis = new DataInputStream(Files.newInputStream(iosNative.toPath()))) { + data = new byte[(int) iosNative.length()]; + dis.readFully(data); + } + try (Writer fios = new OutputStreamWriter(Files.newOutputStream(iosNative.toPath()), StandardCharsets.UTF_8)) { + String str = new String(data, StandardCharsets.UTF_8); + str = str.replace("#define INCLUDE_CN1_PUSH2", "//#define INCLUDE_CN1_PUSH2"); + fios.write(str); + } } catch (IOException ex) { throw new BuildException("Failed to update Objective-C source files to activate push notification flag", ex); } @@ -2423,7 +2423,8 @@ public boolean accept(File file, String string) { } } - BufferedReader infoReader = new BufferedReader(new FileReader(infoPlist)); + BufferedReader infoReader = new BufferedReader(new InputStreamReader( + Files.newInputStream(infoPlist.toPath()), StandardCharsets.UTF_8)); StringBuilder b = new StringBuilder(); String line = infoReader.readLine(); while(line != null) { @@ -2544,9 +2545,9 @@ public boolean accept(File file, String string) { } infoReader.close(); - FileOutputStream fo = new FileOutputStream(infoPlist); - fo.write(b.toString().getBytes()); - fo.close(); + try(FileOutputStream fo = new FileOutputStream(infoPlist)) { + fo.write(b.toString().getBytes(StandardCharsets.UTF_8)); + } } /** diff --git a/maven/codenameone-maven-plugin/src/main/java/com/codename1/maven/CN1BuildMojo.java b/maven/codenameone-maven-plugin/src/main/java/com/codename1/maven/CN1BuildMojo.java index 24036a5452..eac92a37e9 100644 --- a/maven/codenameone-maven-plugin/src/main/java/com/codename1/maven/CN1BuildMojo.java +++ b/maven/codenameone-maven-plugin/src/main/java/com/codename1/maven/CN1BuildMojo.java @@ -19,6 +19,7 @@ import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.*; +import java.nio.charset.StandardCharsets; import java.util.*; @@ -538,7 +539,7 @@ private void createAntProject() throws IOException, LibraryPropertiesException, int no = out.available(); if (no > 0) { int n = out.read(buffer, 0, Math.min(no, buffer.length)); - getLog().info(new String(buffer, 0, n)); + getLog().info(new String(buffer, 0, n, StandardCharsets.UTF_8)); } @@ -674,7 +675,7 @@ private String generateCertificate(String password, String alias, String fullNam Process p = pb.start(); int res = p.waitFor(); - //error occured + //error occurred if(res > 0){ StringBuilder msg = new StringBuilder(); final InputStream input = p.getInputStream(); @@ -683,20 +684,19 @@ private String generateCertificate(String password, String alias, String fullNam byte[] buffer = new byte[8192]; int i = input.read(buffer); while (i > -1) { - String str = new String(buffer, 0, i); + String str = new String(buffer, 0, i, StandardCharsets.UTF_8); System.out.print(str); msg.append(str); i = stream.read(buffer); } i = stream.read(buffer); while (i > -1) { - String str = new String(buffer, 0, i); + String str = new String(buffer, 0, i, StandardCharsets.UTF_8); System.out.print(str); msg.append(str); i = stream.read(buffer); } - return null; } @@ -746,8 +746,6 @@ private void doAndroidLocalBuild(File tmpProjectDir, Properties props, File dist e.setCodenameOneJar(codenameOneJar); - e.setPlatform("android"); - BuildRequest r = new BuildRequest(); r.setDisplayName(props.getProperty("codename1.displayName")); r.setPackageName(props.getProperty("codename1.packageName")); @@ -968,8 +966,6 @@ private void doIOSLocalBuild(File tmpProjectDir, Properties props, File distJar) e.setCodenameOneJar(codenameOneJar); - e.setPlatform("ios"); - BuildRequest r = new BuildRequest(); r.setAppid(props.getProperty("codename1.ios.appid")); r.setDisplayName(props.getProperty("codename1.displayName")); diff --git a/maven/codenameone-maven-plugin/src/main/java/com/codename1/maven/GenerateArchetypeFromTemplateMojo.java b/maven/codenameone-maven-plugin/src/main/java/com/codename1/maven/GenerateArchetypeFromTemplateMojo.java index adcab82b64..462379f07f 100644 --- a/maven/codenameone-maven-plugin/src/main/java/com/codename1/maven/GenerateArchetypeFromTemplateMojo.java +++ b/maven/codenameone-maven-plugin/src/main/java/com/codename1/maven/GenerateArchetypeFromTemplateMojo.java @@ -14,6 +14,8 @@ import java.io.*; import java.net.URL; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.util.*; /** @@ -275,16 +277,19 @@ private void processString(String contents, File projectDir) throws MojoExecutio props.load(new StringReader(properties)); - if (codenameoneSettingsProperties == null || !codenameoneSettingsProperties.exists()) { + if (!codenameoneSettingsProperties.exists()) { throw new MojoExecutionException("Cannot find codenameone_settings.properties"); } SortedProperties cn1Props = new SortedProperties(); - cn1Props.load(new FileReader(codenameoneSettingsProperties)); + try(Reader t = new InputStreamReader(Files.newInputStream(codenameoneSettingsProperties.toPath()), StandardCharsets.UTF_8)) { + cn1Props.load(t); + } cn1Props.putAll(props); getLog().info("Injecting properties:\n" + props+"\n into "+codenameoneSettingsProperties); - cn1Props.store(new FileWriter(codenameoneSettingsProperties), "Injected properties from template"); - + try(Writer w = new OutputStreamWriter(Files.newOutputStream(codenameoneSettingsProperties.toPath()), StandardCharsets.UTF_8)) { + cn1Props.store(w, "Injected properties from template"); + } } String css = extractCSS(contents); diff --git a/maven/codenameone-maven-plugin/src/main/java/com/codename1/maven/OpenGuiBuilderMojo.java b/maven/codenameone-maven-plugin/src/main/java/com/codename1/maven/OpenGuiBuilderMojo.java index 36a208d186..1e15f2b5fc 100644 --- a/maven/codenameone-maven-plugin/src/main/java/com/codename1/maven/OpenGuiBuilderMojo.java +++ b/maven/codenameone-maven-plugin/src/main/java/com/codename1/maven/OpenGuiBuilderMojo.java @@ -10,6 +10,7 @@ import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; +import java.nio.charset.StandardCharsets; import java.util.Collections; import java.util.UUID; import org.apache.maven.plugin.MojoExecutionException; @@ -186,7 +187,7 @@ public void openInGuiBuilder(String projectName, final File javaSourceFile, File "\" resFile=\"" + xmlize(projectResourceFile.toURI().toURL().toExternalForm()) + "\" outputFile=\"" + xmlize(outputFile.toURI().toURL().toExternalForm()) + "\" running=\"" + xmlize(runningFile.toURI().toURL().toExternalForm()) + - "\" />\n").getBytes()); + "\" />\n").getBytes(StandardCharsets.UTF_8)); fos.close(); launchGuiBuilderApp(); @@ -204,7 +205,7 @@ public void run() { fis.read(data); fis.close(); lastModified = outputFile.lastModified(); - String d = new String(data); + String d = new String(data, StandardCharsets.UTF_8); if(d.endsWith("DataChangeEvent")) { gotoSourceFileLine(javaSourceFile, "void " + d, "\n public void " + d + "(com.codename1.ui.Component cmp, int type, int index) {\n }\n"); } else { diff --git a/maven/codenameone-maven-plugin/src/main/java/com/codename1/maven/StubGenerator.java b/maven/codenameone-maven-plugin/src/main/java/com/codename1/maven/StubGenerator.java index bbe313d60a..3b67977378 100644 --- a/maven/codenameone-maven-plugin/src/main/java/com/codename1/maven/StubGenerator.java +++ b/maven/codenameone-maven-plugin/src/main/java/com/codename1/maven/StubGenerator.java @@ -29,6 +29,7 @@ import java.io.IOException; import java.lang.reflect.Method; import java.lang.reflect.Modifier; +import java.nio.charset.StandardCharsets; import static com.codename1.maven.PathUtil.path; @@ -263,10 +264,10 @@ private void generateIOSFiles() throws IOException { iosHFile.getParentFile().mkdirs(); FileOutputStream fo = new FileOutputStream(iosHFile); - fo.write(h.getBytes()); + fo.write(h.getBytes(StandardCharsets.UTF_8)); fo.close(); fo = new FileOutputStream(iosMFile); - fo.write(m.getBytes()); + fo.write(m.getBytes(StandardCharsets.UTF_8)); fo.close(); } @@ -434,9 +435,9 @@ private void generateJavaFile(File dest, String peerComponentType, boolean impl) } t += "}\n"; dest.getParentFile().mkdirs(); - FileOutputStream fo = new FileOutputStream(dest); - fo.write(t.getBytes()); - fo.close(); + try(FileOutputStream fo = new FileOutputStream(dest)) { + fo.write(t.getBytes(StandardCharsets.UTF_8)); + } } private void generateCSFile(File dest, String peerComponentType) throws IOException { @@ -502,7 +503,7 @@ private void generateCSFile(File dest, String peerComponentType) throws IOExcept t += "}\r\n}\r\n"; dest.getParentFile().mkdirs(); FileOutputStream fo = new FileOutputStream(dest); - fo.write(t.getBytes()); + fo.write(t.getBytes(StandardCharsets.UTF_8)); fo.close(); } @@ -585,7 +586,7 @@ private void generateJavaScriptFile() throws IOException { t += "= o;\n\n" + "})(cn1_get_native_interfaces());\n"; FileOutputStream fo = new FileOutputStream(jsFile); - fo.write(t.getBytes()); + fo.write(t.getBytes(StandardCharsets.UTF_8)); fo.close(); } } \ No newline at end of file diff --git a/vm/ByteCodeTranslator/src/com/codename1/tools/translator/ByteCodeTranslator.java b/vm/ByteCodeTranslator/src/com/codename1/tools/translator/ByteCodeTranslator.java index cf283d2e95..2750bcdbe5 100644 --- a/vm/ByteCodeTranslator/src/com/codename1/tools/translator/ByteCodeTranslator.java +++ b/vm/ByteCodeTranslator/src/com/codename1/tools/translator/ByteCodeTranslator.java @@ -25,11 +25,12 @@ import java.io.DataInputStream; import java.io.File; -import java.io.FileInputStream; -import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.io.Writer; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.util.ArrayList; import java.util.Arrays; @@ -522,7 +523,7 @@ private static void handleIosOutput(ByteCodeTranslator b, File[] sources, File d private static void writeCmakeProject(File projectRoot, File srcRoot, String appName) throws IOException { File cmakeLists = new File(projectRoot, "CMakeLists.txt"); String srcRootPath = srcRoot.getAbsolutePath(); - try (FileWriter writer = new FileWriter(cmakeLists)) { + try (Writer writer = new OutputStreamWriter(Files.newOutputStream(cmakeLists.toPath()), StandardCharsets.UTF_8)) { writer.append("cmake_minimum_required(VERSION 3.10)\n"); writer.append("project(").append(appName).append(" LANGUAGES C)\n"); writer.append("set(CMAKE_C_STANDARD 99)\n"); @@ -596,14 +597,14 @@ private static String getFileType(String s) { // private static StringBuilder readFileAsStringBuilder(File sourceFile) throws IOException { - DataInputStream dis = new DataInputStream(Files.newInputStream(sourceFile.toPath())); - byte[] data = new byte[(int)sourceFile.length()]; - dis.readFully(data); - dis.close(); - return new StringBuilder(new String(data)); + try(DataInputStream dis = new DataInputStream(Files.newInputStream(sourceFile.toPath()))) { + byte[] data = new byte[(int) sourceFile.length()]; + dis.readFully(data); + return new StringBuilder(new String(data, StandardCharsets.UTF_8)); + } } // - // rewrite 4/2017 by ddyer to use more appropriate data + // Use more appropriate data // structures, minimizing gc thrashing. This avoids a big // spike in memory and gc usage (and corresponding build // failures due to OutOfMemoryError) at the very end of the build @@ -612,6 +613,7 @@ private static StringBuilder readFileAsStringBuilder(File sourceFile) throws IOE private static void replaceInFile(File sourceFile, String... values) throws IOException { StringBuilder str = readFileAsStringBuilder(sourceFile); int totchanges = 0; + // perform the mutations on stringbuilder, which ought to implement // these operations efficiently. for (int iter = 0; iter < values.length; iter += 2) { @@ -625,13 +627,14 @@ private static void replaceInFile(File sourceFile, String... values) throws IOEx totchanges++; } } + // // don't start the output file until all the processing is done // System.out.println("Rewrite " + sourceFile + " with " + totchanges + " changes"); - FileWriter fios = new FileWriter(sourceFile); - fios.write(str.toString()); - fios.close(); + try(Writer fios = new OutputStreamWriter(Files.newOutputStream(sourceFile.toPath()), StandardCharsets.UTF_8)) { + fios.write(str.toString()); + } } diff --git a/vm/ByteCodeTranslator/src/com/codename1/tools/translator/Parser.java b/vm/ByteCodeTranslator/src/com/codename1/tools/translator/Parser.java index b91079bb8e..a66cbc7f25 100644 --- a/vm/ByteCodeTranslator/src/com/codename1/tools/translator/Parser.java +++ b/vm/ByteCodeTranslator/src/com/codename1/tools/translator/Parser.java @@ -613,17 +613,17 @@ private static void writeFile(ByteCodeClass cls, File outputDir, ConcatenatingFi ((ConcatenatingFileOutputStream)outMain).beginNextFile(cls.getClsName()); } if(ByteCodeTranslator.output == ByteCodeTranslator.OutputType.OUTPUT_TYPE_CSHARP) { - outMain.write(cls.generateCSharpCode().getBytes()); + outMain.write(cls.generateCSharpCode().getBytes(StandardCharsets.UTF_8)); outMain.close(); } else { - outMain.write(cls.generateCCode(classes).getBytes()); + outMain.write(cls.generateCCode(classes).getBytes(StandardCharsets.UTF_8)); outMain.close(); // we also need to write the header file for C outputs String headerName = cls.getClsName() + ".h"; - FileOutputStream outHeader = new FileOutputStream(new File(outputDir, headerName)); - outHeader.write(cls.generateCHeader().getBytes()); - outHeader.close(); + try(FileOutputStream outHeader = new FileOutputStream(new File(outputDir, headerName))) { + outHeader.write(cls.generateCHeader().getBytes(StandardCharsets.UTF_8)); + } } }