summaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit
diff options
context:
space:
mode:
authorMarc Strapetz <marc.strapetz@syntevo.com>2012-09-19 16:57:50 +0200
committerMarc Strapetz <marc.strapetz@syntevo.com>2012-09-24 14:28:15 +0200
commit8f706db56a081efce3b4d245b16a7fc7a0768c71 (patch)
tree48cef93c9b7ce8913507f50aca0eae3994bd4b7e /org.eclipse.jgit
parent4b3c0f8aba3f0df8a3f907c3dbc4e4694d06f8ef (diff)
downloadjgit-8f706db56a081efce3b4d245b16a7fc7a0768c71.tar.gz
jgit-8f706db56a081efce3b4d245b16a7fc7a0768c71.zip
FileBasedConfig supports UTF-8 byte order marker
Change-Id: I1f5dc07182dbf6bba2a9f4807fdd25b475da4ead
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;