Repository: incubator-juneau Updated Branches: refs/heads/master 6f41a0523 -> c7c802cdf
Allow reuse of SerializerSession and ParserSession objects. Project: http://git-wip-us.apache.org/repos/asf/incubator-juneau/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-juneau/commit/c7c802cd Tree: http://git-wip-us.apache.org/repos/asf/incubator-juneau/tree/c7c802cd Diff: http://git-wip-us.apache.org/repos/asf/incubator-juneau/diff/c7c802cd Branch: refs/heads/master Commit: c7c802cdf13461f4f8d72f82906674c1714d5db4 Parents: 6f41a05 Author: JamesBognar <[email protected]> Authored: Sat Jul 15 10:16:08 2017 -0400 Committer: JamesBognar <[email protected]> Committed: Sat Jul 15 10:16:08 2017 -0400 ---------------------------------------------------------------------- .../org/apache/juneau/parser/ParserSession.java | 123 ++----------------- .../juneau/serializer/SerializerSession.java | 53 +------- 2 files changed, 13 insertions(+), 163 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/c7c802cd/juneau-core/src/main/java/org/apache/juneau/parser/ParserSession.java ---------------------------------------------------------------------- diff --git a/juneau-core/src/main/java/org/apache/juneau/parser/ParserSession.java b/juneau-core/src/main/java/org/apache/juneau/parser/ParserSession.java index 9265799..e5b085e 100644 --- a/juneau-core/src/main/java/org/apache/juneau/parser/ParserSession.java +++ b/juneau-core/src/main/java/org/apache/juneau/parser/ParserSession.java @@ -14,12 +14,9 @@ package org.apache.juneau.parser; import static org.apache.juneau.parser.ParserContext.*; import static org.apache.juneau.internal.ClassUtils.*; -import static org.apache.juneau.internal.IOUtils.*; -import static org.apache.juneau.internal.StringUtils.*; import java.io.*; import java.lang.reflect.*; -import java.nio.charset.*; import java.util.*; import org.apache.juneau.*; @@ -38,10 +35,7 @@ public class ParserSession extends BeanSession { private final Method javaMethod; private final Object outer; - private final Object input; - private String inputString; - private InputStream inputStream; - private Reader reader, noCloseReader; + private final ParserInput input; private BeanPropertyMeta currentProperty; private ClassMeta<?> currentClass; private final ParserListener listener; @@ -103,7 +97,7 @@ public class ParserSession extends BeanSession { fileCharset = op.getString(PARSER_fileCharset, ctx.fileCharset); listenerClass = op.get(Class.class, PARSER_listener, ctx.listener); } - this.input = input; + this.input = new ParserInput(input, isDebug(), strict, fileCharset, inputStreamCharset); this.javaMethod = javaMethod; this.outer = outer; this.listener = newInstance(ParserListener.class, listenerClass); @@ -119,39 +113,7 @@ public class ParserSession extends BeanSession { * @throws ParseException If object could not be converted to an input stream. */ public InputStream getInputStream() throws ParseException { - try { - if (input == null) - return null; - if (input instanceof InputStream) { - if (isDebug()) { - byte[] b = readBytes((InputStream)input, 1024); - inputString = toHex(b); - return new ByteArrayInputStream(b); - } - return (InputStream)input; - } - if (input instanceof byte[]) { - if (isDebug()) - inputString = toHex((byte[])input); - return new ByteArrayInputStream((byte[])input); - } - if (input instanceof String) { - inputString = (String)input; - return new ByteArrayInputStream(fromHex((String)input)); - } - if (input instanceof File) { - if (isDebug()) { - byte[] b = readBytes((File)input); - inputString = toHex(b); - return new ByteArrayInputStream(b); - } - inputStream = new FileInputStream((File)input); - return inputStream; - } - } catch (IOException e) { - throw new ParseException(e); - } - throw new ParseException("Cannot convert object of type {0} to an InputStream.", input.getClass().getName()); + return input.getInputStream(); } @@ -165,71 +127,7 @@ public class ParserSession extends BeanSession { * @throws Exception If object could not be converted to a reader. */ public Reader getReader() throws Exception { - if (input == null) - return null; - if (input instanceof Reader) { - if (isDebug()) { - inputString = read((Reader)input); - return new StringReader(inputString); - } - return (Reader)input; - } - if (input instanceof CharSequence) { - inputString = input.toString(); - if (reader == null) - reader = new ParserReader((CharSequence)input); - return reader; - } - if (input instanceof InputStream || input instanceof byte[]) { - InputStream is = ( - input instanceof InputStream - ? (InputStream)input - : new ByteArrayInputStream((byte[])input) - ); - if (noCloseReader == null) { - CharsetDecoder cd = ( - "default".equalsIgnoreCase(inputStreamCharset) - ? Charset.defaultCharset() - : Charset.forName(inputStreamCharset) - ).newDecoder(); - if (strict) { - cd.onMalformedInput(CodingErrorAction.REPORT); - cd.onUnmappableCharacter(CodingErrorAction.REPORT); - } else { - cd.onMalformedInput(CodingErrorAction.REPLACE); - cd.onUnmappableCharacter(CodingErrorAction.REPLACE); - } - noCloseReader = new InputStreamReader(is, cd); - } - if (isDebug()) { - inputString = read(noCloseReader); - return new StringReader(inputString); - } - return noCloseReader; - } - if (input instanceof File) { - if (reader == null) { - CharsetDecoder cd = ( - "default".equalsIgnoreCase(fileCharset) - ? Charset.defaultCharset() - : Charset.forName(fileCharset) - ).newDecoder(); - if (strict) { - cd.onMalformedInput(CodingErrorAction.REPORT); - cd.onUnmappableCharacter(CodingErrorAction.REPORT); - } else { - cd.onMalformedInput(CodingErrorAction.REPLACE); - cd.onUnmappableCharacter(CodingErrorAction.REPLACE); - } - reader = new InputStreamReader(new FileInputStream((File)input), cd); - } - if (isDebug()) { - inputString = read(reader); - return new StringReader(inputString); - } - return reader; - } - throw new ParseException("Cannot convert object of type {0} to a Reader.", input.getClass().getName()); + return input.getReader(); } /** @@ -252,7 +150,7 @@ public class ParserSession extends BeanSession { * @return The raw input object passed into this session. */ protected Object getInput() { - return input; + return input.getRawInput(); } /** @@ -445,7 +343,7 @@ public class ParserSession extends BeanSession { * @return The input as a string, or <jk>null</jk> if debug mode not enabled. */ public String getInputAsString() { - return inputString; + return input.getInputAsString(); } /** @@ -454,14 +352,7 @@ public class ParserSession extends BeanSession { @Override public boolean close() { if (super.close()) { - try { - if (inputStream != null) - inputStream.close(); - if (reader != null) - reader.close(); - } catch (IOException e) { - throw new BeanRuntimeException(e); - } + input.close(); return true; } return false; http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/c7c802cd/juneau-core/src/main/java/org/apache/juneau/serializer/SerializerSession.java ---------------------------------------------------------------------- diff --git a/juneau-core/src/main/java/org/apache/juneau/serializer/SerializerSession.java b/juneau-core/src/main/java/org/apache/juneau/serializer/SerializerSession.java index 66ff3e1..6da8b9d 100644 --- a/juneau-core/src/main/java/org/apache/juneau/serializer/SerializerSession.java +++ b/juneau-core/src/main/java/org/apache/juneau/serializer/SerializerSession.java @@ -13,7 +13,6 @@ package org.apache.juneau.serializer; import static org.apache.juneau.internal.ClassUtils.*; -import static org.apache.juneau.internal.IOUtils.*; import static org.apache.juneau.internal.StringUtils.*; import static org.apache.juneau.serializer.SerializerContext.*; @@ -24,7 +23,6 @@ import java.util.*; import org.apache.juneau.*; import org.apache.juneau.http.*; -import org.apache.juneau.internal.*; import org.apache.juneau.transform.*; /** @@ -71,9 +69,7 @@ public class SerializerSession extends BeanSession { private final LinkedList<StackElement> stack = new LinkedList<StackElement>(); // Contains the current objects in the current branch of the model. private boolean isBottom; // If 'true', then we're at a leaf in the model (i.e. a String, Number, Boolean, or null). private final Method javaMethod; // Java method that invoked this serializer. - private final Object output; - private OutputStream outputStream; - private Writer writer, flushOnlyWriter; + private SerializerOutput output; private BeanPropertyMeta currentProperty; private ClassMeta<?> currentClass; private final SerializerListener listener; @@ -117,7 +113,7 @@ public class SerializerSession extends BeanSession { TimeZone timeZone, MediaType mediaType, UriContext uriContext) { super(ctx, op, locale, timeZone, mediaType); this.javaMethod = javaMethod; - this.output = output; + this.output = new SerializerOutput(output); UriResolution uriResolution; UriRelativity uriRelativity; Class<?> listenerClass; @@ -190,16 +186,7 @@ public class SerializerSession extends BeanSession { * @throws Exception If object could not be converted to an output stream. */ public OutputStream getOutputStream() throws Exception { - if (output == null) - throw new SerializeException("Output cannot be null."); - if (output instanceof OutputStream) - return (OutputStream)output; - if (output instanceof File) { - if (outputStream == null) - outputStream = new BufferedOutputStream(new FileOutputStream((File)output)); - return outputStream; - } - throw new SerializeException("Cannot convert object of type {0} to an OutputStream.", output.getClass().getName()); + return output.getOutputStream(); } @@ -221,26 +208,7 @@ public class SerializerSession extends BeanSession { * @throws Exception If object could not be converted to a writer. */ public Writer getWriter() throws Exception { - if (output == null) - throw new SerializeException("Output cannot be null."); - if (output instanceof Writer) - return (Writer)output; - if (output instanceof OutputStream) { - if (flushOnlyWriter == null) - flushOnlyWriter = new OutputStreamWriter((OutputStream)output, UTF8); - return flushOnlyWriter; - } - if (output instanceof File) { - if (writer == null) - writer = new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream((File)output))); - return writer; - } - if (output instanceof StringBuilder) { - if (writer == null) - writer = new StringBuilderWriter((StringBuilder)output); - return writer; - } - throw new SerializeException("Cannot convert object of type {0} to a Writer.", output.getClass().getName()); + return output.getWriter(); } /** @@ -249,7 +217,7 @@ public class SerializerSession extends BeanSession { * @return The raw output object passed into this session. */ protected Object getOutput() { - return output; + return output.getRawOutput(); } /** @@ -715,16 +683,7 @@ public class SerializerSession extends BeanSession { @Override public boolean close() { if (super.close()) { - try { - if (outputStream != null) - outputStream.close(); - if (flushOnlyWriter != null) - flushOnlyWriter.flush(); - if (writer != null) - writer.close(); - } catch (IOException e) { - throw new BeanRuntimeException(e); - } + output.close(); return true; } return false;
