From 8f1061a8b232e8bc40f2115f9c32110c847a2711 Mon Sep 17 00:00:00 2001 From: Jeremie Panzer Date: Tue, 26 Mar 2013 10:20:23 +0100 Subject: [PATCH] stream response, bypassing the string step --- .../rest/JSONMessageBodyReaderWriter.java | 42 +++++++++++++++---- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/commafeed/frontend/rest/JSONMessageBodyReaderWriter.java b/src/main/java/com/commafeed/frontend/rest/JSONMessageBodyReaderWriter.java index 12f67cd4..121b589b 100644 --- a/src/main/java/com/commafeed/frontend/rest/JSONMessageBodyReaderWriter.java +++ b/src/main/java/com/commafeed/frontend/rest/JSONMessageBodyReaderWriter.java @@ -2,7 +2,9 @@ package com.commafeed.frontend.rest; import java.io.IOException; import java.io.InputStream; +import java.io.InputStreamReader; import java.io.OutputStream; +import java.io.OutputStreamWriter; import java.lang.annotation.Annotation; import java.lang.reflect.Type; import java.text.SimpleDateFormat; @@ -19,7 +21,6 @@ import javax.ws.rs.ext.MessageBodyReader; import javax.ws.rs.ext.MessageBodyWriter; import javax.ws.rs.ext.Provider; -import org.apache.commons.io.IOUtils; import org.apache.commons.lang.time.DateUtils; import com.google.gson.Gson; @@ -35,6 +36,10 @@ import com.google.gson.JsonSerializer; public class JSONMessageBodyReaderWriter implements MessageBodyWriter, MessageBodyReader { + private static final String UTF_8 = "UTF-8"; + + private Gson gson; + @Override public boolean isWriteable(Class type, Type genericType, Annotation[] annotations, MediaType mediaType) { @@ -61,8 +66,18 @@ public class JSONMessageBodyReaderWriter implements MessageBodyWriter, WebApplicationException { httpHeaders.putSingle(HttpHeaders.CONTENT_TYPE, mediaType.toString() + ";charset=UTF-8"); - IOUtils.write(getGson().toJson(t), entityStream, "UTF-8"); - + OutputStreamWriter writer = new OutputStreamWriter(entityStream, UTF_8); + try { + Type jsonType; + if (type.equals(genericType)) { + jsonType = type; + } else { + jsonType = genericType; + } + getGson().toJson(t, jsonType, writer); + } finally { + writer.close(); + } } @Override @@ -70,13 +85,26 @@ public class JSONMessageBodyReaderWriter implements MessageBodyWriter, Annotation[] annotations, MediaType mediaType, MultivaluedMap httpHeaders, InputStream entityStream) throws IOException, WebApplicationException { - String json = IOUtils.toString(entityStream, "UTF-8"); - return getGson().fromJson(json, type); + InputStreamReader streamReader = new InputStreamReader(entityStream, + UTF_8); + try { + Type jsonType; + if (type.equals(genericType)) { + jsonType = type; + } else { + jsonType = genericType; + } + return getGson().fromJson(streamReader, jsonType); + } finally { + streamReader.close(); + } } private Gson getGson() { - Gson gson = new GsonBuilder().registerTypeAdapter(Date.class, - new DateSerializer()).create(); + if (gson == null) { + gson = new GsonBuilder().registerTypeAdapter(Date.class, + new DateSerializer()).create(); + } return gson; }