001/*
002 * Copyright 2024 Revetware LLC.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.soklet.servlet.javax;
018
019import javax.annotation.Nonnull;
020import javax.annotation.concurrent.NotThreadSafe;
021import javax.servlet.ReadListener;
022import javax.servlet.ServletInputStream;
023import java.io.IOException;
024import java.io.InputStream;
025
026import static java.lang.String.format;
027import static java.util.Objects.requireNonNull;
028
029/**
030 * @author <a href="https://www.revetkn.com">Mark Allen</a>
031 */
032@NotThreadSafe
033public class SokletServletInputStream extends ServletInputStream {
034        @Nonnull
035        private final InputStream inputStream;
036        @Nonnull
037        private Boolean finished;
038
039        public SokletServletInputStream(@Nonnull InputStream inputStream) {
040                super();
041                requireNonNull(inputStream);
042
043                this.inputStream = inputStream;
044                this.finished = false;
045        }
046
047        @Nonnull
048        protected InputStream getInputStream() {
049                return this.inputStream;
050        }
051
052        @Nonnull
053        protected Boolean getFinished() {
054                return this.finished;
055        }
056
057        protected void setFinished(@Nonnull Boolean finished) {
058                requireNonNull(finished);
059                this.finished = finished;
060        }
061
062        // Implementation of ServletInputStream methods below:
063
064        @Override
065        public boolean isFinished() {
066                return getFinished();
067        }
068
069        @Override
070        public boolean isReady() {
071                return true;
072        }
073
074        @Override
075        public int available() throws IOException {
076                return getInputStream().available();
077        }
078
079        @Override
080        public void close() throws IOException {
081                super.close();
082                getInputStream().close();
083        }
084
085        @Override
086        public void setReadListener(@Nonnull ReadListener readListener) {
087                requireNonNull(readListener);
088                throw new IllegalStateException(format("%s functionality is not supported", ReadListener.class.getSimpleName()));
089        }
090
091        @Override
092        public int read() throws IOException {
093                int data = getInputStream().read();
094
095                if (data == -1)
096                        setFinished(true);
097
098                return data;
099        }
100}