
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
... | ... | @@ -162,14 +162,14 @@ |
162 | 162 |
} |
163 | 163 |
|
164 | 164 |
private void replaceSiteSecretKey(String seed) throws IOException { |
165 |
- SecureRandom random = new SecureRandom(seed.getBytes()); |
|
165 |
+ SecureRandom random = new SecureRandom(seed.getBytes(Config.getCharset())); |
|
166 | 166 |
String secret = new BigInteger(130, random).toString(32); |
167 | 167 |
|
168 | 168 |
Path path = Paths.get("conf/application.conf"); |
169 | 169 |
byte[] bytes = Files.readAllBytes(path); |
170 |
- String config = new String(bytes); |
|
170 |
+ String config = new String(bytes, Config.getCharset()); |
|
171 | 171 |
config = config.replace(DEFAULT_SECRET, secret); |
172 |
- Files.write(path, config.getBytes()); |
|
172 |
+ Files.write(path, config.getBytes(Config.getCharset())); |
|
173 | 173 |
} |
174 | 174 |
|
175 | 175 |
private boolean hasError(Form<User> newUserForm) { |
--- app/playRepository/BareCommit.java
+++ app/playRepository/BareCommit.java
... | ... | @@ -26,6 +26,7 @@ |
26 | 26 |
import org.eclipse.jgit.revwalk.RevCommit; |
27 | 27 |
import org.eclipse.jgit.revwalk.RevWalk; |
28 | 28 |
import org.eclipse.jgit.treewalk.CanonicalTreeParser; |
29 |
+import utils.Config; |
|
29 | 30 |
|
30 | 31 |
import java.io.File; |
31 | 32 |
import java.io.IOException; |
... | ... | @@ -36,8 +37,9 @@ |
36 | 37 |
|
37 | 38 |
import static org.eclipse.jgit.lib.Constants.HEAD; |
38 | 39 |
import static org.eclipse.jgit.lib.Constants.OBJ_BLOB; |
39 |
-import static utils.LineEnding.*; |
|
40 |
-import static playRepository.BareRepository.*; |
|
40 |
+import static playRepository.BareRepository.findFileLineEnding; |
|
41 |
+import static utils.LineEnding.addEOL; |
|
42 |
+import static utils.LineEnding.changeLineEnding; |
|
41 | 43 |
|
42 | 44 |
public class BareCommit { |
43 | 45 |
private PersonIdent personIdent; |
... | ... | @@ -138,7 +140,7 @@ |
138 | 140 |
|
139 | 141 |
boolean isInsertedInTree = false; |
140 | 142 |
while(!treeParser.eof()){ |
141 |
- String entryName = new String(treeParser.getEntryPathBuffer(), 0, treeParser.getEntryPathLength()); |
|
143 |
+ String entryName = new String(treeParser.getEntryPathBuffer(), 0, treeParser.getEntryPathLength(), Config.getCharset()); |
|
142 | 144 |
String nameForComparison = entryName; |
143 | 145 |
|
144 | 146 |
if (treeParser.getEntryFileMode() == FileMode.TREE){ |
... | ... | @@ -149,12 +151,12 @@ |
149 | 151 |
isInsertedInTree = true; |
150 | 152 |
} else if (nameForComparison.compareTo(fileName) > 0 && isInsertedInTree == false) { |
151 | 153 |
formatter.append(fileName, FileMode.REGULAR_FILE, fileObjectId); |
152 |
- formatter.append(entryName.getBytes() |
|
154 |
+ formatter.append(entryName.getBytes(Config.getCharset()) |
|
153 | 155 |
, treeParser.getEntryFileMode() |
154 | 156 |
, treeParser.getEntryObjectId()); |
155 | 157 |
isInsertedInTree = true; |
156 | 158 |
} else { |
157 |
- formatter.append(entryName.getBytes() |
|
159 |
+ formatter.append(entryName.getBytes(Config.getCharset()) |
|
158 | 160 |
, treeParser.getEntryFileMode() |
159 | 161 |
, treeParser.getEntryObjectId()); |
160 | 162 |
} |
... | ... | @@ -174,7 +176,8 @@ |
174 | 176 |
} |
175 | 177 |
|
176 | 178 |
private ObjectId createGitObjectWithText(String contents) throws IOException { |
177 |
- return objectInserter.insert(OBJ_BLOB, contents.getBytes(), 0, contents.getBytes().length); |
|
179 |
+ byte[] bytes = contents.getBytes(Config.getCharset()); |
|
180 |
+ return objectInserter.insert(OBJ_BLOB, bytes, 0, bytes.length); |
|
178 | 181 |
} |
179 | 182 |
|
180 | 183 |
private void refUpdate(ObjectId commitId, String refName) throws IOException { |
--- app/playRepository/BareRepository.java
+++ app/playRepository/BareRepository.java
... | ... | @@ -36,7 +36,7 @@ |
36 | 36 |
import java.io.IOException; |
37 | 37 |
|
38 | 38 |
import static org.eclipse.jgit.lib.Constants.HEAD; |
39 |
-import static utils.LineEnding.*; |
|
39 |
+import static utils.LineEnding.findLineEnding; |
|
40 | 40 |
|
41 | 41 |
public class BareRepository { |
42 | 42 |
/** |
... | ... | @@ -58,7 +58,7 @@ |
58 | 58 |
if (loader == null) { |
59 | 59 |
return null; |
60 | 60 |
} |
61 |
- return new String(loader.getCachedBytes()); |
|
61 |
+ return new String(loader.getCachedBytes(), utils.Config.getCharset()); |
|
62 | 62 |
} |
63 | 63 |
|
64 | 64 |
public static Repository getRepository(Project project){ |
... | ... | @@ -125,7 +125,7 @@ |
125 | 125 |
if(oldObjectId.equals(ObjectId.zeroId())){ |
126 | 126 |
return EndingType.UNDEFINED; |
127 | 127 |
} else { |
128 |
- String fileContents = new String(repository.open(oldObjectId).getBytes()); |
|
128 |
+ String fileContents = new String(repository.open(oldObjectId).getBytes(), utils.Config.getCharset()); |
|
129 | 129 |
return findLineEnding(fileContents); |
130 | 130 |
} |
131 | 131 |
} |
--- app/playRepository/SVNRepository.java
+++ app/playRepository/SVNRepository.java
... | ... | @@ -40,13 +40,11 @@ |
40 | 40 |
import org.tmatesoft.svn.core.wc.SVNDiffClient; |
41 | 41 |
import org.tmatesoft.svn.core.wc.SVNRevision; |
42 | 42 |
import play.libs.Json; |
43 |
+import utils.Config; |
|
43 | 44 |
import utils.FileUtil; |
44 | 45 |
import utils.GravatarUtil; |
45 | 46 |
|
46 |
-import java.io.ByteArrayOutputStream; |
|
47 |
-import java.io.File; |
|
48 |
-import java.io.FileNotFoundException; |
|
49 |
-import java.io.IOException; |
|
47 |
+import java.io.*; |
|
50 | 48 |
import java.util.ArrayList; |
51 | 49 |
import java.util.Collection; |
52 | 50 |
import java.util.List; |
... | ... | @@ -215,17 +213,17 @@ |
215 | 213 |
} |
216 | 214 |
|
217 | 215 |
@Override |
218 |
- public String getPatch(String commitId) throws SVNException { |
|
216 |
+ public String getPatch(String commitId) throws SVNException, UnsupportedEncodingException { |
|
219 | 217 |
long rev = Integer.parseInt(commitId); |
220 | 218 |
return getPatch(rev - 1, rev); |
221 | 219 |
} |
222 | 220 |
|
223 | 221 |
@Override |
224 |
- public String getPatch(String revA, String revB) throws SVNException { |
|
222 |
+ public String getPatch(String revA, String revB) throws SVNException, UnsupportedEncodingException { |
|
225 | 223 |
return getPatch(Long.parseLong(revA), Long.parseLong(revB)); |
226 | 224 |
} |
227 | 225 |
|
228 |
- private String getPatch(long revA, long revB) throws SVNException { |
|
226 |
+ private String getPatch(long revA, long revB) throws SVNException, UnsupportedEncodingException { |
|
229 | 227 |
// Prepare required arguments. |
230 | 228 |
SVNURL svnURL = SVNURL.fromFile(new File(getRepoPrefix() + ownerName + "/" + projectName)); |
231 | 229 |
|
... | ... | @@ -239,7 +237,7 @@ |
239 | 237 |
diffClient.doDiff(svnURL, null, SVNRevision.create(revA), SVNRevision.create(revB), |
240 | 238 |
SVNDepth.INFINITY, true, byteArrayOutputStream); |
241 | 239 |
|
242 |
- return byteArrayOutputStream.toString(); |
|
240 |
+ return byteArrayOutputStream.toString(Config.getCharset().name()); |
|
243 | 241 |
} |
244 | 242 |
|
245 | 243 |
@Override |
--- app/utils/BasicAuthAction.java
+++ app/utils/BasicAuthAction.java
... | ... | @@ -62,7 +62,7 @@ |
62 | 62 |
String userpassBase64 = credentials.substring(6); |
63 | 63 |
byte[] userpassBytes; |
64 | 64 |
|
65 |
- userpassBytes = Base64.decodeBase64(userpassBase64.getBytes()); |
|
65 |
+ userpassBytes = Base64.decodeBase64(userpassBase64); |
|
66 | 66 |
|
67 | 67 |
// Use ISO-8859-1 only and not others, even if in RFC 2616, Section 2.2 "Basic Rules" allows |
68 | 68 |
// TEXT to be encoded according to the rules of RFC 2047. |
--- app/utils/Config.java
+++ app/utils/Config.java
... | ... | @@ -26,6 +26,7 @@ |
26 | 26 |
import play.mvc.Http; |
27 | 27 |
|
28 | 28 |
import java.net.*; |
29 |
+import java.nio.charset.Charset; |
|
29 | 30 |
import java.util.Enumeration; |
30 | 31 |
|
31 | 32 |
public class Config { |
... | ... | @@ -271,4 +272,8 @@ |
271 | 272 |
return user + "@" + config.getString(prefix + ".domain", getHostname()); |
272 | 273 |
} |
273 | 274 |
} |
275 |
+ |
|
276 |
+ public static Charset getCharset() { |
|
277 |
+ return Charset.forName("UTF-8"); |
|
278 |
+ } |
|
274 | 279 |
} |
--- app/utils/Markdown.java
+++ app/utils/Markdown.java
... | ... | @@ -34,6 +34,7 @@ |
34 | 34 |
import java.io.Reader; |
35 | 35 |
import java.net.URI; |
36 | 36 |
import java.net.URISyntaxException; |
37 |
+import java.nio.charset.Charset; |
|
37 | 38 |
|
38 | 39 |
public class Markdown { |
39 | 40 |
|
... | ... | @@ -50,15 +51,15 @@ |
50 | 51 |
|
51 | 52 |
try { |
52 | 53 |
is = Thread.currentThread().getContextClassLoader().getResourceAsStream(XSS_JS_FILE); |
53 |
- reader = new InputStreamReader(is); |
|
54 |
+ reader = new InputStreamReader(is, Config.getCharset()); |
|
54 | 55 |
_engine.eval(reader); |
55 | 56 |
|
56 | 57 |
is = Thread.currentThread().getContextClassLoader().getResourceAsStream(MARKED_JS_FILE); |
57 |
- reader = new InputStreamReader(is); |
|
58 |
+ reader = new InputStreamReader(is, Config.getCharset()); |
|
58 | 59 |
_engine.eval(reader); |
59 | 60 |
|
60 | 61 |
is = Thread.currentThread().getContextClassLoader().getResourceAsStream(HIGHLIGHT_JS_FILE); |
61 |
- reader = new InputStreamReader(is); |
|
62 |
+ reader = new InputStreamReader(is, Config.getCharset()); |
|
62 | 63 |
_engine.eval(reader); |
63 | 64 |
} catch (Exception ex) { |
64 | 65 |
throw new RuntimeException(ex); |
--- app/utils/MomentUtil.java
+++ app/utils/MomentUtil.java
... | ... | @@ -46,7 +46,7 @@ |
46 | 46 |
|
47 | 47 |
try { |
48 | 48 |
is = Thread.currentThread().getContextClassLoader().getResourceAsStream(MOMENT_JS_FILE); |
49 |
- reader = new InputStreamReader(is); |
|
49 |
+ reader = new InputStreamReader(is, Config.getCharset()); |
|
50 | 50 |
_engine.eval(reader); |
51 | 51 |
} catch (Exception ex) { |
52 | 52 |
throw new RuntimeException(ex); |
--- app/utils/PlayServletResponse.java
+++ app/utils/PlayServletResponse.java
... | ... | @@ -25,6 +25,7 @@ |
25 | 25 |
import javax.servlet.*; |
26 | 26 |
import javax.servlet.http.*; |
27 | 27 |
import java.io.*; |
28 |
+import java.nio.charset.Charset; |
|
28 | 29 |
import java.util.*; |
29 | 30 |
|
30 | 31 |
public class PlayServletResponse implements HttpServletResponse { |
... | ... | @@ -116,7 +117,9 @@ |
116 | 117 |
this.statusLock = new Object(); |
117 | 118 |
this.inputStream = new PipedInputStream(); |
118 | 119 |
this.outputStream = new ChunkedOutputStream(new PipedOutputStream(this.inputStream)); |
119 |
- this.pw = new PrintWriter(this.outputStream); |
|
120 |
+ this.pw = new PrintWriter( |
|
121 |
+ new BufferedWriter(new OutputStreamWriter(this.outputStream, Config.getCharset())), |
|
122 |
+ false); |
|
120 | 123 |
} |
121 | 124 |
|
122 | 125 |
@Override |
Add a comment
Delete comment
Once you delete this comment, you won't be able to recover it. Are you sure you want to delete this comment?