Skip to content

Commit

Permalink
use EntityInputStream to check if stream is empty
Browse files Browse the repository at this point in the history
Signed-off-by: Maxim Nesen <[email protected]>
  • Loading branch information
senivam authored and jansupol committed Oct 2, 2019
1 parent b17b3ea commit 1c3b8d5
Showing 1 changed file with 5 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PushbackInputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

Expand All @@ -41,6 +40,7 @@

import org.glassfish.jersey.jsonb.LocalizationMessages;
import org.glassfish.jersey.message.internal.AbstractMessageReaderWriterProvider;
import org.glassfish.jersey.message.internal.EntityInputStream;

/**
* Entity provider (reader and writer) for JSONB.
Expand Down Expand Up @@ -72,20 +72,10 @@ public Object readFrom(Class<Object> type, Type genericType,
MediaType mediaType,
MultivaluedMap<String, String> httpHeaders,
InputStream entityStream) throws IOException, WebApplicationException {
if (entityStream.markSupported()) {
entityStream.mark(1);
if (entityStream.read() == -1) {
throw new NoContentException(LocalizationMessages.ERROR_JSONB_EMPTYSTREAM());
}
entityStream.reset();
} else {
final PushbackInputStream buffer = new PushbackInputStream(entityStream);
final int firstByte = buffer.read();
if (firstByte == -1) {
throw new NoContentException(LocalizationMessages.ERROR_JSONB_EMPTYSTREAM());
}
buffer.unread(firstByte);
entityStream = buffer;
final EntityInputStream entityInputStream = new EntityInputStream(entityStream);
entityStream = entityInputStream;
if (entityInputStream.isEmpty()) {
throw new NoContentException(LocalizationMessages.ERROR_JSONB_EMPTYSTREAM());
}

Jsonb jsonb = getJsonb(type);
Expand Down

0 comments on commit 1c3b8d5

Please sign in to comment.