blob: 2396dd34a2a02b17efe2978c71f4f6deb47ebce3 [file] [log] [blame]
/*
* 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();
}
}