You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by GitBox <gi...@apache.org> on 2018/05/24 11:35:43 UTC

[GitHub] rhtyd commented on a change in pull request #2674: Create unit test cases for 'ConfigDriveBuilder' class

rhtyd commented on a change in pull request #2674: Create unit test cases for 'ConfigDriveBuilder' class
URL: https://github.com/apache/cloudstack/pull/2674#discussion_r190553845
 
 

 ##########
 File path: engine/storage/configdrive/src/org/apache/cloudstack/storage/configdrive/ConfigDriveBuilder.java
 ##########
 @@ -51,30 +47,45 @@
 
     public static final Logger LOG = Logger.getLogger(ConfigDriveBuilder.class);
 
-    private static void writeFile(final File folder, final String file, final String content) {
-        if (folder == null || Strings.isNullOrEmpty(file)) {
-            return;
-        }
-        final File vendorDataFile = new File(folder, file);
-        try (final FileWriter fw = new FileWriter(vendorDataFile); final BufferedWriter bw = new BufferedWriter(fw)) {
-            bw.write(content);
+    /**
+     * Writes a content {@link String} to a file that is going to be created in a folder. We will not append to the file if it already exists. Therefore, its content will be overwritten.
+     * Moreover, the charset used is {@link com.cloud.utils.StringUtils#getPreferredCharset()}.
+     *
+     * We expect the folder object and the file not to be null/empty.
+     */
+    static void writeFile(File folder, String file, String content) {
+        try {
+            FileUtils.write(new File(folder, file), content, com.cloud.utils.StringUtils.getPreferredCharset(), false);
         } catch (IOException ex) {
             throw new CloudRuntimeException("Failed to create config drive file " + file, ex);
         }
     }
 
-    public static String fileToBase64String(final File isoFile) throws IOException {
+    /**
+     *  Read the content of a {@link File} and convert it to a String in base 64.
+     *  We expect the content of the file to be encoded using {@link com.cloud.utils.StringUtils#getPreferredCharset()}
+     */
+    public static String fileToBase64String(File isoFile) throws IOException {
         byte[] encoded = Base64.encodeBase64(FileUtils.readFileToByteArray(isoFile));
-        return new String(encoded, StandardCharsets.US_ASCII);
+        return new String(encoded, com.cloud.utils.StringUtils.getPreferredCharset());
 
 Review comment:
   @rafaelweingartner for base64 encoding should n't we keep it to US_ASCII?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services