diff --git a/avaje-jex/src/main/java/io/avaje/jex/core/json/JacksonJsonService.java b/avaje-jex/src/main/java/io/avaje/jex/core/json/JacksonJsonService.java index 3c9ace0f..6cd3d31b 100644 --- a/avaje-jex/src/main/java/io/avaje/jex/core/json/JacksonJsonService.java +++ b/avaje-jex/src/main/java/io/avaje/jex/core/json/JacksonJsonService.java @@ -49,15 +49,13 @@ public T fromJson(Type type, InputStream is) { @Override public void toJson(Object bean, OutputStream os) { try { - try (JsonGenerator generator = mapper.createGenerator(os)) { + try (var generator = mapper.createGenerator(os)) { // only flush to underlying OutputStream on success generator.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET); generator.disable(JsonGenerator.Feature.FLUSH_PASSED_TO_STREAM); generator.disable(JsonGenerator.Feature.AUTO_CLOSE_JSON_CONTENT); mapper.writeValue(generator, bean); - generator.flush(); } - os.flush(); os.close(); } catch (IOException e) { throw new UncheckedIOException(e); @@ -75,17 +73,10 @@ public String toJsonString(Object bean) { @Override public void toJsonStream(Iterator iterator, OutputStream os) { - final JsonGenerator generator; - try { - generator = mapper.createGenerator(os); + try (var generator = mapper.createGenerator(os)) { generator.setPrettyPrinter(null); - try { - while (iterator.hasNext()) { - write(iterator, generator); - } - } finally { - generator.flush(); - generator.close(); + while (iterator.hasNext()) { + write(iterator, generator); } } catch (IOException e) { throw new UncheckedIOException(e); diff --git a/avaje-jex/src/main/java/io/avaje/jex/core/json/JsonbJsonService.java b/avaje-jex/src/main/java/io/avaje/jex/core/json/JsonbJsonService.java index 7094fd45..ee03d7c7 100644 --- a/avaje-jex/src/main/java/io/avaje/jex/core/json/JsonbJsonService.java +++ b/avaje-jex/src/main/java/io/avaje/jex/core/json/JsonbJsonService.java @@ -32,7 +32,7 @@ public T fromJson(Type clazz, InputStream is) { @Override public void toJson(Object bean, OutputStream os) { - jsonb.toJson(bean, os); + jsonb.toJson(bean, new NoFlushJsonOutput(os)); } @Override diff --git a/avaje-jex/src/main/java/io/avaje/jex/core/json/JsonbOutput.java b/avaje-jex/src/main/java/io/avaje/jex/core/json/JsonbOutput.java index 49623f15..a977ea17 100644 --- a/avaje-jex/src/main/java/io/avaje/jex/core/json/JsonbOutput.java +++ b/avaje-jex/src/main/java/io/avaje/jex/core/json/JsonbOutput.java @@ -1,11 +1,11 @@ package io.avaje.jex.core.json; -import io.avaje.jex.http.Context; -import io.avaje.json.stream.JsonOutput; - import java.io.IOException; import java.io.OutputStream; +import io.avaje.jex.http.Context; +import io.avaje.json.stream.JsonOutput; + /** * avaje-jsonb output that allows for writing fixed length content * straight from the avaje-jsonb buffer, avoiding the jex side buffer. @@ -43,10 +43,8 @@ public void writeLast(byte[] content, int offset, int length) throws IOException } @Override - public void flush() throws IOException { - if (os != null) { - os.flush(); - } + public void flush() { + // shouldn't manually flush } @Override diff --git a/avaje-jex/src/main/java/io/avaje/jex/core/json/NoFlushJsonOutput.java b/avaje-jex/src/main/java/io/avaje/jex/core/json/NoFlushJsonOutput.java new file mode 100644 index 00000000..9c877b79 --- /dev/null +++ b/avaje-jex/src/main/java/io/avaje/jex/core/json/NoFlushJsonOutput.java @@ -0,0 +1,35 @@ +package io.avaje.jex.core.json; + +import java.io.IOException; +import java.io.OutputStream; + +import io.avaje.json.stream.JsonOutput; + +final class NoFlushJsonOutput implements JsonOutput { + + private final OutputStream outputStream; + + NoFlushJsonOutput(OutputStream outputStream) { + this.outputStream = outputStream; + } + + @Override + public void write(byte[] content, int offset, int length) throws IOException { + outputStream.write(content, offset, length); + } + + @Override + public void flush() throws IOException { + // no flush + } + + @Override + public void close() throws IOException { + outputStream.close(); + } + + @Override + public OutputStream unwrapOutputStream() { + return outputStream; + } +} diff --git a/pom.xml b/pom.xml index 03f6c854..b6406bb2 100644 --- a/pom.xml +++ b/pom.xml @@ -27,7 +27,7 @@ full 4.0 11.2 - 3.0 + 3.1-RC2 3.0 9.4 2.8