Remove last remaining guava dependencies.

ajat.jar reduced in size from 2.3MB down to 135KB.

Test: m ahat-test, with HtmlEscaperTest added.
Change-Id: I9a71ea46d1c953380ae3b07f3503ef16e4b97c8d
diff --git a/tools/ahat/Android.mk b/tools/ahat/Android.mk
index ad6011d..f628fe5 100644
--- a/tools/ahat/Android.mk
+++ b/tools/ahat/Android.mk
@@ -22,9 +22,7 @@
 include $(CLEAR_VARS)
 LOCAL_SRC_FILES := $(call all-java-files-under, src)
 LOCAL_JAR_MANIFEST := src/manifest.txt
-LOCAL_JAVA_RESOURCE_FILES := \
-  $(LOCAL_PATH)/src/style.css
-LOCAL_STATIC_JAVA_LIBRARIES := guavalib
+LOCAL_JAVA_RESOURCE_FILES := $(LOCAL_PATH)/src/style.css
 LOCAL_IS_HOST_MODULE := true
 LOCAL_MODULE_TAGS := optional
 LOCAL_MODULE := ahat
diff --git a/tools/ahat/src/DocString.java b/tools/ahat/src/DocString.java
index 7970bf8..76e9e80 100644
--- a/tools/ahat/src/DocString.java
+++ b/tools/ahat/src/DocString.java
@@ -16,7 +16,6 @@
 
 package com.android.ahat;
 
-import com.google.common.html.HtmlEscapers;
 import java.net.URI;
 import java.net.URISyntaxException;
 
@@ -67,7 +66,7 @@
    * Returns this object.
    */
   public DocString append(String text) {
-    mStringBuilder.append(HtmlEscapers.htmlEscaper().escape(text));
+    mStringBuilder.append(HtmlEscaper.escape(text));
     return this;
   }
 
@@ -185,7 +184,7 @@
 
   public DocString appendImage(URI uri, String alt) {
     mStringBuilder.append("<img alt=\"");
-    mStringBuilder.append(HtmlEscapers.htmlEscaper().escape(alt));
+    mStringBuilder.append(HtmlEscaper.escape(alt));
     mStringBuilder.append("\" src=\"");
     mStringBuilder.append(uri.toASCIIString());
     mStringBuilder.append("\" />");
@@ -194,7 +193,7 @@
 
   public DocString appendThumbnail(URI uri, String alt) {
     mStringBuilder.append("<img height=\"16\" alt=\"");
-    mStringBuilder.append(HtmlEscapers.htmlEscaper().escape(alt));
+    mStringBuilder.append(HtmlEscaper.escape(alt));
     mStringBuilder.append("\" src=\"");
     mStringBuilder.append(uri.toASCIIString());
     mStringBuilder.append("\" />");
diff --git a/tools/ahat/src/HtmlEscaper.java b/tools/ahat/src/HtmlEscaper.java
new file mode 100644
index 0000000..75a6827
--- /dev/null
+++ b/tools/ahat/src/HtmlEscaper.java
@@ -0,0 +1,48 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.ahat;
+
+public class HtmlEscaper {
+  /**
+   * Escape html characters in the input string.
+   */
+  public static String escape(String text) {
+    String specials = "&<>\'\"";
+    String[] replacements = new String[]{"&amp;", "&lt;", "&gt;", "&apos;", "&quot;"};
+    StringBuilder sb = null;
+    int low = 0;
+    for (int i = 0; i < text.length(); ++i) {
+      int s = specials.indexOf(text.charAt(i));
+      if (s != -1) {
+        if (sb == null) {
+          sb = new StringBuilder();
+        }
+        sb.append(text.substring(low, i));
+        sb.append(replacements[s]);
+        low = i + 1;
+      }
+    }
+    if (sb == null) {
+      return text;
+    }
+
+    sb.append(text.substring(low));
+    return sb.toString();
+  }
+}
+
+
diff --git a/tools/ahat/src/StaticHandler.java b/tools/ahat/src/StaticHandler.java
index b2805d6..4a68f1c 100644
--- a/tools/ahat/src/StaticHandler.java
+++ b/tools/ahat/src/StaticHandler.java
@@ -16,7 +16,6 @@
 
 package com.android.ahat;
 
-import com.google.common.io.ByteStreams;
 import com.sun.net.httpserver.HttpExchange;
 import com.sun.net.httpserver.HttpHandler;
 import java.io.IOException;
@@ -49,7 +48,12 @@
       exchange.getResponseHeaders().add("Content-Type", mContentType);
       exchange.sendResponseHeaders(200, 0);
       OutputStream os = exchange.getResponseBody();
-      ByteStreams.copy(is, os);
+      int read;
+      byte[] buf = new byte[4096];
+      while ((read = is.read(buf)) >= 0) {
+        os.write(buf, 0, read);
+      }
+      is.close();
       os.close();
     }
   }
diff --git a/tools/ahat/test/HtmlEscaperTest.java b/tools/ahat/test/HtmlEscaperTest.java
new file mode 100644
index 0000000..a36db35
--- /dev/null
+++ b/tools/ahat/test/HtmlEscaperTest.java
@@ -0,0 +1,32 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.ahat;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class HtmlEscaperTest {
+  @Test
+  public void tests() {
+    assertEquals("nothing to escape", HtmlEscaper.escape("nothing to escape"));
+    assertEquals("a&lt;b&gt; &amp; &quot;c&apos;d&quot;e", HtmlEscaper.escape("a<b> & \"c\'d\"e"));
+    assertEquals("adjacent &lt;&lt;&gt;&gt; x", HtmlEscaper.escape("adjacent <<>> x"));
+    assertEquals("&lt; initial", HtmlEscaper.escape("< initial"));
+    assertEquals("ending &gt;", HtmlEscaper.escape("ending >"));
+  }
+}
diff --git a/tools/ahat/test/Tests.java b/tools/ahat/test/Tests.java
index 61d0035..0e70432 100644
--- a/tools/ahat/test/Tests.java
+++ b/tools/ahat/test/Tests.java
@@ -25,6 +25,7 @@
         "com.android.ahat.DiffFieldsTest",
         "com.android.ahat.DiffTest",
         "com.android.ahat.DominatorsTest",
+        "com.android.ahat.HtmlEscaperTest",
         "com.android.ahat.InstanceTest",
         "com.android.ahat.NativeAllocationTest",
         "com.android.ahat.ObjectHandlerTest",