Skip to content

Commit

Permalink
The MessageBodyWriter implementation should not close the output stre…
Browse files Browse the repository at this point in the history
…am, re #55
  • Loading branch information
safris committed Jan 10, 2024
1 parent e5eeef7 commit aab7824
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
13 changes: 6 additions & 7 deletions jaxrs/src/main/java/org/jsonx/JxObjectProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,19 +142,18 @@ public long getSize(final Object t, final Class<?> rawType, final Type genericTy

private boolean marshal(final OutputStream entityStream, final Charset charset, final Object t, final Annotation[] annotations) throws IOException {
if (t instanceof JxObject) {
try (final OutputStreamWriter writer = new OutputStreamWriter(entityStream, charset)) {
getEncoder().toStream(writer, (JxObject)t);
}
final OutputStreamWriter out = new OutputStreamWriter(entityStream, charset);
getEncoder().toStream(out, (JxObject)t);
out.flush();
}
else if (t instanceof List) {
final JxEncoder encoder = getEncoder();
for (final Annotation annotation : annotations) { // [A]
final Class<? extends Annotation> annotationType = annotation.annotationType();
if (ArrayProperty.class.equals(annotationType) || annotationType.getDeclaredAnnotation(ArrayType.class) != null) {
try (final OutputStreamWriter writer = new OutputStreamWriter(entityStream, charset)) {
encoder.toStream(writer, (List<?>)t, annotationType);
}

final OutputStreamWriter out = new OutputStreamWriter(entityStream, charset);
encoder.toStream(out, (List<?>)t, annotationType);
out.flush();
break;
}
}
Expand Down
18 changes: 9 additions & 9 deletions jaxrs/src/test/java/org/jsonx/JxObjectProviderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ public void testObject() throws IOException, ClassNotFoundException {
final JxObject jxObject = (JxObject)provider.readFrom((Class<Object>)Class.forName(Message.class.getName()), null, null, null, null, new ByteArrayInputStream(data.getBytes()));

assertTrue(provider.isWriteable(Message.class, Message.class, null, null));
final ByteArrayOutputStream out = new ByteArrayOutputStream();
assertEquals(-1, provider.getSize(jxObject, Message.class, Message.class, null, null));
provider.writeTo(jxObject, Message.class, Message.class, null, null, new MultivaluedHashMap<>(), out);

assertEquals(data, new String(out.toByteArray()));
try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
assertEquals(-1, provider.getSize(jxObject, Message.class, Message.class, null, null));
provider.writeTo(jxObject, Message.class, Message.class, null, null, new MultivaluedHashMap<>(), out);
assertEquals(data, new String(out.toByteArray()));
}
}

private static boolean isBadRequestException(final RuntimeException e) {
Expand Down Expand Up @@ -112,9 +112,9 @@ public void testArrayType() throws IOException, ClassNotFoundException, NoSuchFi
final List<?> jxObject = (List<?>)provider.readFrom((Class<Object>)Class.forName(List.class.getName()), null, annotations, null, null, new ByteArrayInputStream(data.getBytes()));

assertTrue(provider.isWriteable(List.class, Message.class, annotations, null));
final ByteArrayOutputStream out = new ByteArrayOutputStream();
provider.writeTo(jxObject, List.class, List.class, annotations, null, new MultivaluedHashMap<>(), out);

assertEquals(data, new String(out.toByteArray()));
try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
provider.writeTo(jxObject, List.class, List.class, annotations, null, new MultivaluedHashMap<>(), out);
assertEquals(data, new String(out.toByteArray()));
}
}
}

0 comments on commit aab7824

Please sign in to comment.