Yi EungJun 2015-02-15
Fix reliance on default encoding
For the Yobi's default encoding, use the encoding from
Config.getCharset(), which returns always "UTF-8", instead of
Charset.defaultCharset() which depends on the platform.
@ac83c23715cd4632ed43faca3250a037bebc89ae
app/Global.java
--- app/Global.java
+++ app/Global.java
@@ -162,14 +162,14 @@
             }
 
             private void replaceSiteSecretKey(String seed) throws IOException {
-                SecureRandom random = new SecureRandom(seed.getBytes());
+                SecureRandom random = new SecureRandom(seed.getBytes(Config.getCharset()));
                 String secret = new BigInteger(130, random).toString(32);
 
                 Path path = Paths.get("conf/application.conf");
                 byte[] bytes = Files.readAllBytes(path);
-                String config = new String(bytes);
+                String config = new String(bytes, Config.getCharset());
                 config = config.replace(DEFAULT_SECRET, secret);
-                Files.write(path, config.getBytes());
+                Files.write(path, config.getBytes(Config.getCharset()));
             }
 
             private boolean hasError(Form<User> newUserForm) {
app/playRepository/BareCommit.java
--- app/playRepository/BareCommit.java
+++ app/playRepository/BareCommit.java
@@ -26,6 +26,7 @@
 import org.eclipse.jgit.revwalk.RevCommit;
 import org.eclipse.jgit.revwalk.RevWalk;
 import org.eclipse.jgit.treewalk.CanonicalTreeParser;
+import utils.Config;
 
 import java.io.File;
 import java.io.IOException;
@@ -36,8 +37,9 @@
 
 import static org.eclipse.jgit.lib.Constants.HEAD;
 import static org.eclipse.jgit.lib.Constants.OBJ_BLOB;
-import static utils.LineEnding.*;
-import static playRepository.BareRepository.*;
+import static playRepository.BareRepository.findFileLineEnding;
+import static utils.LineEnding.addEOL;
+import static utils.LineEnding.changeLineEnding;
 
 public class BareCommit {
     private PersonIdent personIdent;
@@ -138,7 +140,7 @@
 
         boolean isInsertedInTree = false;
         while(!treeParser.eof()){
-            String entryName = new String(treeParser.getEntryPathBuffer(), 0, treeParser.getEntryPathLength());
+            String entryName = new String(treeParser.getEntryPathBuffer(), 0, treeParser.getEntryPathLength(), Config.getCharset());
             String nameForComparison = entryName;
 
             if (treeParser.getEntryFileMode() == FileMode.TREE){
@@ -149,12 +151,12 @@
                 isInsertedInTree = true;
             } else if (nameForComparison.compareTo(fileName) > 0 && isInsertedInTree == false) {
                 formatter.append(fileName, FileMode.REGULAR_FILE, fileObjectId);
-                formatter.append(entryName.getBytes()
+                formatter.append(entryName.getBytes(Config.getCharset())
                         , treeParser.getEntryFileMode()
                         , treeParser.getEntryObjectId());
                 isInsertedInTree = true;
             } else {
-                formatter.append(entryName.getBytes()
+                formatter.append(entryName.getBytes(Config.getCharset())
                         , treeParser.getEntryFileMode()
                         , treeParser.getEntryObjectId());
             }
@@ -174,7 +176,8 @@
     }
 
     private ObjectId createGitObjectWithText(String contents) throws IOException {
-        return objectInserter.insert(OBJ_BLOB, contents.getBytes(), 0, contents.getBytes().length);
+        byte[] bytes = contents.getBytes(Config.getCharset());
+        return objectInserter.insert(OBJ_BLOB, bytes, 0, bytes.length);
     }
 
     private void refUpdate(ObjectId commitId, String refName) throws IOException {
app/playRepository/BareRepository.java
--- app/playRepository/BareRepository.java
+++ app/playRepository/BareRepository.java
@@ -36,7 +36,7 @@
 import java.io.IOException;
 
 import static org.eclipse.jgit.lib.Constants.HEAD;
-import static utils.LineEnding.*;
+import static utils.LineEnding.findLineEnding;
 
 public class BareRepository {
     /**
@@ -58,7 +58,7 @@
         if (loader == null) {
             return null;
         }
-        return new String(loader.getCachedBytes());
+        return new String(loader.getCachedBytes(), utils.Config.getCharset());
     }
 
     public static Repository getRepository(Project project){
@@ -125,7 +125,7 @@
         if(oldObjectId.equals(ObjectId.zeroId())){
             return EndingType.UNDEFINED;
         } else {
-            String fileContents = new String(repository.open(oldObjectId).getBytes());
+            String fileContents = new String(repository.open(oldObjectId).getBytes(), utils.Config.getCharset());
             return findLineEnding(fileContents);
         }
     }
app/playRepository/SVNRepository.java
--- app/playRepository/SVNRepository.java
+++ app/playRepository/SVNRepository.java
@@ -40,13 +40,11 @@
 import org.tmatesoft.svn.core.wc.SVNDiffClient;
 import org.tmatesoft.svn.core.wc.SVNRevision;
 import play.libs.Json;
+import utils.Config;
 import utils.FileUtil;
 import utils.GravatarUtil;
 
-import java.io.ByteArrayOutputStream;
-import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.IOException;
+import java.io.*;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
@@ -215,17 +213,17 @@
     }
 
     @Override
-    public String getPatch(String commitId) throws SVNException {
+    public String getPatch(String commitId) throws SVNException, UnsupportedEncodingException {
         long rev = Integer.parseInt(commitId);
         return getPatch(rev - 1, rev);
     }
 
     @Override
-    public String getPatch(String revA, String revB) throws SVNException {
+    public String getPatch(String revA, String revB) throws SVNException, UnsupportedEncodingException {
         return getPatch(Long.parseLong(revA), Long.parseLong(revB));
     }
 
-    private String getPatch(long revA, long revB) throws SVNException {
+    private String getPatch(long revA, long revB) throws SVNException, UnsupportedEncodingException {
         // Prepare required arguments.
         SVNURL svnURL = SVNURL.fromFile(new File(getRepoPrefix() + ownerName + "/" + projectName));
 
@@ -239,7 +237,7 @@
         diffClient.doDiff(svnURL, null, SVNRevision.create(revA), SVNRevision.create(revB),
                 SVNDepth.INFINITY, true, byteArrayOutputStream);
 
-        return byteArrayOutputStream.toString();
+        return byteArrayOutputStream.toString(Config.getCharset().name());
     }
 
     @Override
app/utils/BasicAuthAction.java
--- app/utils/BasicAuthAction.java
+++ app/utils/BasicAuthAction.java
@@ -62,7 +62,7 @@
         String userpassBase64 = credentials.substring(6);
         byte[] userpassBytes;
 
-        userpassBytes = Base64.decodeBase64(userpassBase64.getBytes());
+        userpassBytes = Base64.decodeBase64(userpassBase64);
 
         // Use ISO-8859-1 only and not others, even if in RFC 2616, Section 2.2 "Basic Rules" allows
         // TEXT to be encoded according to the rules of RFC 2047.
app/utils/Config.java
--- app/utils/Config.java
+++ app/utils/Config.java
@@ -26,6 +26,7 @@
 import play.mvc.Http;
 
 import java.net.*;
+import java.nio.charset.Charset;
 import java.util.Enumeration;
 
 public class Config {
@@ -271,4 +272,8 @@
             return user + "@" + config.getString(prefix + ".domain", getHostname());
         }
     }
+
+    public static Charset getCharset() {
+        return Charset.forName("UTF-8");
+    }
 }
app/utils/Markdown.java
--- app/utils/Markdown.java
+++ app/utils/Markdown.java
@@ -34,6 +34,7 @@
 import java.io.Reader;
 import java.net.URI;
 import java.net.URISyntaxException;
+import java.nio.charset.Charset;
 
 public class Markdown {
 
@@ -50,15 +51,15 @@
 
         try {
             is = Thread.currentThread().getContextClassLoader().getResourceAsStream(XSS_JS_FILE);
-            reader = new InputStreamReader(is);
+            reader = new InputStreamReader(is, Config.getCharset());
             _engine.eval(reader);
 
             is = Thread.currentThread().getContextClassLoader().getResourceAsStream(MARKED_JS_FILE);
-            reader = new InputStreamReader(is);
+            reader = new InputStreamReader(is, Config.getCharset());
             _engine.eval(reader);
 
             is = Thread.currentThread().getContextClassLoader().getResourceAsStream(HIGHLIGHT_JS_FILE);
-            reader = new InputStreamReader(is);
+            reader = new InputStreamReader(is, Config.getCharset());
             _engine.eval(reader);
         } catch (Exception ex) {
             throw new RuntimeException(ex);
app/utils/MomentUtil.java
--- app/utils/MomentUtil.java
+++ app/utils/MomentUtil.java
@@ -46,7 +46,7 @@
 
         try {
             is = Thread.currentThread().getContextClassLoader().getResourceAsStream(MOMENT_JS_FILE);
-            reader = new InputStreamReader(is);
+            reader = new InputStreamReader(is, Config.getCharset());
             _engine.eval(reader);
         } catch (Exception ex) {
             throw new RuntimeException(ex);
app/utils/PlayServletResponse.java
--- app/utils/PlayServletResponse.java
+++ app/utils/PlayServletResponse.java
@@ -25,6 +25,7 @@
 import javax.servlet.*;
 import javax.servlet.http.*;
 import java.io.*;
+import java.nio.charset.Charset;
 import java.util.*;
 
 public class PlayServletResponse implements HttpServletResponse {
@@ -116,7 +117,9 @@
         this.statusLock = new Object();
         this.inputStream = new PipedInputStream();
         this.outputStream = new ChunkedOutputStream(new PipedOutputStream(this.inputStream));
-        this.pw = new PrintWriter(this.outputStream);
+        this.pw = new PrintWriter(
+                new BufferedWriter(new OutputStreamWriter(this.outputStream, Config.getCharset())),
+                false);
     }
 
     @Override
Add a comment
List