Make event serialization encoding more explicit

Create a Fs.writeUtf8() method that explicitly writes using UTF8 instead
of relying on the default to be UTF8. Also rename the Fs.readFile()
method to readUtf8() to make it more obvious that it is deserializing
using UTF8 and to make it an obvious counterpart to the new writeUtf8()
method.

Change-Id: Id270a3f1b8b9dfc86d63f21ad1854b00476f3f87
diff --git a/src/main/java/com/googlesource/gerrit/plugins/events/fsstore/FileValue.java b/src/main/java/com/googlesource/gerrit/plugins/events/fsstore/FileValue.java
index 2572c31..9fbad13 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/events/fsstore/FileValue.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/events/fsstore/FileValue.java
@@ -77,7 +77,7 @@
 
   /** The lowest level raw String read of the file */
   protected String read() throws IOException {
-    return Fs.readFile(path);
+    return Fs.readUtf8(path);
   }
 
   /** Serialize object to given tmp file in preparation to call update() */
@@ -92,6 +92,6 @@
 
   /** Low level raw string write to given tmp file in preparation to call update(). */
   protected static void prepare(Path tmp, String s) throws IOException {
-    Files.write(tmp, s.getBytes());
+    Fs.writeUtf8(tmp, s);
   }
 }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/events/fsstore/Fs.java b/src/main/java/com/googlesource/gerrit/plugins/events/fsstore/Fs.java
index 4b162ce..96f7db1 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/events/fsstore/Fs.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/events/fsstore/Fs.java
@@ -154,7 +154,7 @@
   }
 
   /** Read the contents of a UTF_8 encoded file as a String */
-  public static String readFile(Path file) throws IOException {
+  public static String readUtf8(Path file) throws IOException {
     StringBuffer buffer = new StringBuffer();
     for (String line : Files.readAllLines(file, StandardCharsets.UTF_8)) {
       buffer.append(line);
@@ -162,6 +162,11 @@
     return buffer.toString();
   }
 
+  /** Write the contents of a String as a UTF_8 encoded file */
+  public static void writeUtf8(Path file, String s) throws IOException {
+    Files.write(file, s.getBytes(StandardCharsets.UTF_8));
+  }
+
   /** Get the first entry in a directory. */
   public static Path getFirstDirEntry(Path dir) throws IOException {
     try (DirectoryStream<Path> dirEntries = Files.newDirectoryStream(dir)) {
diff --git a/src/main/java/com/googlesource/gerrit/plugins/events/fsstore/FsStore.java b/src/main/java/com/googlesource/gerrit/plugins/events/fsstore/FsStore.java
index a9cec81..67c9ff6 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/events/fsstore/FsStore.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/events/fsstore/FsStore.java
@@ -122,7 +122,7 @@
     if (cachedTail.isLessThanOrEqualTo(num, MAX_GET_SPINS)
         && cachedHead.isGreaterThanOrEqualTo(num, MAX_GET_SPINS)) {
       try {
-        return Fs.readFile(paths.events.path(num));
+        return Fs.readUtf8(paths.events.path(num));
       } catch (NoSuchFileException e) {
       }
     }
diff --git a/src/test/java/com/googlesource/gerrit/plugins/events/fsstore/EventSequenceTest.java b/src/test/java/com/googlesource/gerrit/plugins/events/fsstore/EventSequenceTest.java
index 54618ff..23fe774 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/events/fsstore/EventSequenceTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/events/fsstore/EventSequenceTest.java
@@ -66,6 +66,6 @@
     EventSequence.UniqueUpdate up = seq.spinSubmit(event, maxSpins);
     assertEquals(next, seq.get());
     assertNotNull(up.destination);
-    assertEquals(event, Fs.readFile(up.destination));
+    assertEquals(event, Fs.readUtf8(up.destination));
   }
 }