+ * The method is customized to not throw an {@link IOException} if the reset operation fails. Instead, + * a runtime {@link javax.ws.rs.ProcessingException} is thrown. + *
+ * + * @throws javax.ws.rs.ProcessingException in case the reset operation on the underlying entity input stream failed. + */ + @Override + public void reset() { + try { + input.reset(); + } catch (IOException ex) { + throw new ProcessingException(LocalizationMessages.MESSAGE_CONTENT_BUFFER_RESET_FAILED(), ex); + } + } + + /** + * {@inheritDoc} + *+ * The method is customized to not throw an {@link IOException} if the close operation fails. Instead, + * a warning message is logged. + *
+ */ + @Override + public void close() throws ProcessingException { + final InputStream in = input; + if (in == null) { + return; + } + if (!closed) { + try { + in.close(); + } catch (IOException ex) { + // This e.g. means that the underlying socket stream got closed by other thread somehow... + throw new ProcessingException(LocalizationMessages.MESSAGE_CONTENT_INPUT_STREAM_CLOSE_FAILED(), ex); + } finally { + closed = true; + } + } + } + + /** + * Check if the underlying entity stream is empty. + *+ * Note that the operation may need to block until a first byte (or EOF) is available in the stream. + *
+ * + * @return {@code true} if the entity stream is empty, {@code false} otherwise. + */ + public boolean isEmpty() { + ensureNotClosed(); + + final InputStream in = input; + if (in == null) { + return true; + } + + try { + // Try #markSupported first - #available on WLS waits until socked timeout is reached when chunked encoding is used. + if (in.markSupported()) { + in.mark(1); + int i = in.read(); + in.reset(); + return i == -1; + } else { + try { + if (in.available() > 0) { + return false; + } + } catch (IOException ioe) { + // NOOP. Try other approaches as this can fail on WLS. + } + + int b = in.read(); + if (b == -1) { + return true; + } + + PushbackInputStream pbis; + if (in instanceof PushbackInputStream) { + pbis = (PushbackInputStream) in; + } else { + pbis = new PushbackInputStream(in, 1); + input = pbis; + } + pbis.unread(b); + + return false; + } + } catch (IOException ex) { + throw new ProcessingException(ex); + } + } + + /** + * Check that the entity input stream has not been closed yet. + * + * @throws IllegalStateException in case the entity input stream has been closed. + */ + public void ensureNotClosed() throws IllegalStateException { + if (closed) { + throw new IllegalStateException(LocalizationMessages.ERROR_ENTITY_STREAM_CLOSED()); + } + } + + /** + * Get the closed status of this input stream. + * + * @return {@code true} if the stream has been closed, {@code false} otherwise. + */ + public boolean isClosed() { + return closed; + } + + /** + * Get the wrapped input stream instance. + * + * @return wrapped input stream instance. + */ + public final InputStream getWrappedStream() { + return input; + } + + /** + * Set the wrapped input stream instance. + * + * @param wrapped new input stream instance to be wrapped. + */ + public final void setWrappedStream(InputStream wrapped) { + input = wrapped; + } +} diff --git a/core-common/src/main/java/org/glassfish/jersey/message/internal/EntityInputStream.java b/core-common/src/main/java8/org/glassfish/jersey/message/internal/EntityInputStream.java similarity index 99% rename from core-common/src/main/java/org/glassfish/jersey/message/internal/EntityInputStream.java rename to core-common/src/main/java8/org/glassfish/jersey/message/internal/EntityInputStream.java index 06678b16fe..6ba27b462e 100644 --- a/core-common/src/main/java/org/glassfish/jersey/message/internal/EntityInputStream.java +++ b/core-common/src/main/java8/org/glassfish/jersey/message/internal/EntityInputStream.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012, 2019 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 2024 Oracle and/or its affiliates. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v. 2.0, which is available at