aboutsummaryrefslogtreecommitdiff
path: root/caliper/src/main/java/com/google/caliper/ResultsReader.java
diff options
context:
space:
mode:
Diffstat (limited to 'caliper/src/main/java/com/google/caliper/ResultsReader.java')
-rw-r--r--caliper/src/main/java/com/google/caliper/ResultsReader.java64
1 files changed, 64 insertions, 0 deletions
diff --git a/caliper/src/main/java/com/google/caliper/ResultsReader.java b/caliper/src/main/java/com/google/caliper/ResultsReader.java
new file mode 100644
index 0000000..2396dd3
--- /dev/null
+++ b/caliper/src/main/java/com/google/caliper/ResultsReader.java
@@ -0,0 +1,64 @@
+/*
+ * Copyright (C) 2010 Google Inc.
+ *
+ * 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.google.caliper;
+
+import com.google.gson.JsonParseException;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+
+/**
+ * Helps with deserialization of results, given uncertainty about the format (xml or json) they
+ * are in.
+ */
+public final class ResultsReader {
+ public Result getResult(InputStream in) throws IOException {
+ // save input into a byte array since we may need to read it twice
+ byte[] postedData = readAllBytes(in);
+ Result result;
+ InputStreamReader baisJsonReader = new InputStreamReader(new ByteArrayInputStream(postedData));
+ try {
+ result = Json.getGsonInstance().fromJson(baisJsonReader, Result.class);
+ } catch (JsonParseException e) {
+ // probably an old client is trying to send data, so try to parse it as XML instead.
+ ByteArrayInputStream baisXml = new ByteArrayInputStream(postedData);
+ try {
+ result = Xml.resultFromXml(baisXml);
+ } catch (Exception e2) {
+ throw new RuntimeException(e);
+ } finally {
+ baisXml.close();
+ }
+ } finally {
+ baisJsonReader.close();
+ }
+ return result;
+ }
+
+ private byte[] readAllBytes(InputStream in) throws IOException {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ byte[] buf = new byte[4096];
+ int read;
+ while ((read = in.read(buf)) != -1) {
+ baos.write(buf, 0, read);
+ }
+ return baos.toByteArray();
+ }
+}