|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 Oracle and/or its affiliates. All rights reserved. |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under the |
| 5 | + * terms of the Eclipse Public License v. 2.0, which is available at |
| 6 | + * http://www.eclipse.org/legal/epl-2.0. |
| 7 | + * |
| 8 | + * This Source Code may also be made available under the following Secondary |
| 9 | + * Licenses when the conditions for such availability set forth in the |
| 10 | + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, |
| 11 | + * version 2 with the GNU Classpath Exception, which is available at |
| 12 | + * https://www.gnu.org/software/classpath/license.html. |
| 13 | + * |
| 14 | + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 |
| 15 | + */ |
| 16 | + |
| 17 | +package org.glassfish.jersey.servlet.internal; |
| 18 | + |
| 19 | +import jakarta.servlet.ReadListener; |
| 20 | +import jakarta.servlet.ServletInputStream; |
| 21 | +import org.glassfish.jersey.message.internal.EntityInputStream; |
| 22 | +import org.glassfish.jersey.message.internal.EntityInputStreamListener; |
| 23 | + |
| 24 | +import java.io.IOException; |
| 25 | +import java.io.UncheckedIOException; |
| 26 | + |
| 27 | +public abstract class ServletEntityInputStream extends ServletInputStream { |
| 28 | + |
| 29 | + protected EntityInputStream wrappedStream; |
| 30 | + |
| 31 | + |
| 32 | + protected abstract ServletInputStream getServletInputStream(); |
| 33 | + |
| 34 | + @Override |
| 35 | + public boolean isFinished() { |
| 36 | + return getServletInputStream().isFinished(); |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public boolean isReady() { |
| 41 | + return getServletInputStream().isReady(); |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public void setReadListener(ReadListener readListener) { |
| 46 | + getServletInputStream().setReadListener(readListener); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public int read() throws IOException { |
| 51 | + return getServletInputStream().read(); |
| 52 | + } |
| 53 | + |
| 54 | + public EntityInputStream getWrappedStream() { |
| 55 | + if (wrappedStream == null) { |
| 56 | + wrappedStream = new EntityInputStream(getServletInputStream()); |
| 57 | + wrappedStream.setListener(new EntityInputStreamListener() { |
| 58 | + @Override |
| 59 | + public boolean isEmpty() { |
| 60 | + try { |
| 61 | + return getServletInputStream().available() == 0 |
| 62 | + || getServletInputStream().isFinished(); |
| 63 | + } catch (IOException e) { |
| 64 | + throw new UncheckedIOException(e); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public boolean isReady() { |
| 70 | + return getServletInputStream().isReady(); |
| 71 | + } |
| 72 | + }); |
| 73 | + } |
| 74 | + |
| 75 | + return wrappedStream; |
| 76 | + } |
| 77 | + |
| 78 | + |
| 79 | +} |
0 commit comments