summaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit
diff options
context:
space:
mode:
authorRobin Rosenberg <robin.rosenberg@dewire.com>2012-09-24 14:35:22 -0400
committerGerrit Code Review @ Eclipse.org <gerrit@eclipse.org>2012-09-24 14:35:22 -0400
commit2e1d391f62579f77d97d7e1ec6ff53bb9146644d (patch)
tree32118ba7c8f6aad39b15e0c1f6ec3c36564420c5 /org.eclipse.jgit
parent13df55a464e89fd961ef97a9c951baba38d3f5d9 (diff)
parent8f706db56a081efce3b4d245b16a7fc7a0768c71 (diff)
downloadjgit-2e1d391f62579f77d97d7e1ec6ff53bb9146644d.tar.gz
jgit-2e1d391f62579f77d97d7e1ec6ff53bb9146644d.zip
Merge "FileBasedConfig supports UTF-8 byte order marker"
Diffstat (limited to 'org.eclipse.jgit')
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/storage/file/FileBasedConfig.java31
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/util/RawParseUtils.java5
2 files changed, 34 insertions, 2 deletions
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/FileBasedConfig.java b/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/FileBasedConfig.java
index f30253675c..7ae491b38b 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/FileBasedConfig.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/FileBasedConfig.java
@@ -49,6 +49,7 @@
package org.eclipse.jgit.storage.file;
+import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
@@ -70,8 +71,13 @@ import org.eclipse.jgit.util.RawParseUtils;
*/
public class FileBasedConfig extends StoredConfig {
private final File configFile;
+
private final FS fs;
+
+ private boolean utf8Bom;
+
private volatile FileSnapshot snapshot;
+
private volatile ObjectId hash;
/**
@@ -141,7 +147,16 @@ public class FileBasedConfig extends StoredConfig {
else
snapshot = newSnapshot;
} else {
- fromText(RawParseUtils.decode(in));
+ final String decoded;
+ if (in.length >= 3 && in[0] == (byte) 0xEF
+ && in[1] == (byte) 0xBB && in[2] == (byte) 0xBF) {
+ decoded = RawParseUtils.decode(RawParseUtils.UTF8_CHARSET,
+ in, 3, in.length);
+ utf8Bom = true;
+ } else {
+ decoded = RawParseUtils.decode(in);
+ }
+ fromText(decoded);
snapshot = newSnapshot;
hash = newHash;
}
@@ -170,7 +185,19 @@ public class FileBasedConfig extends StoredConfig {
* the file could not be written.
*/
public void save() throws IOException {
- final byte[] out = Constants.encode(toText());
+ final byte[] out;
+ final String text = toText();
+ if (utf8Bom) {
+ final ByteArrayOutputStream bos = new ByteArrayOutputStream();
+ bos.write(0xEF);
+ bos.write(0xBB);
+ bos.write(0xBF);
+ bos.write(text.getBytes(RawParseUtils.UTF8_CHARSET));
+ out = bos.toByteArray();
+ } else {
+ out = Constants.encode(text);
+ }
+
final LockFile lf = new LockFile(getFile(), fs);
if (!lf.lock())
throw new LockFailedException(getFile());
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/util/RawParseUtils.java b/org.eclipse.jgit/src/org/eclipse/jgit/util/RawParseUtils.java
index 91184bad61..89fbeed890 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/util/RawParseUtils.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/util/RawParseUtils.java
@@ -65,6 +65,11 @@ import org.eclipse.jgit.lib.PersonIdent;
/** Handy utility functions to parse raw object contents. */
public final class RawParseUtils {
+ /**
+ * UTF-8 charset constant.
+ */
+ public static final Charset UTF8_CHARSET = Charset.forName("UTF-8");
+
private static final byte[] digits10;
private static final byte[] digits16;