001/*
002 * Licensed under the Apache License, Version 2.0 (the "License");
003 * you may not use this file except in compliance with the License.
004 * You may obtain a copy of the License at
005 *
006 *     http://www.apache.org/licenses/LICENSE-2.0
007 *
008 * Unless required by applicable law or agreed to in writing, software
009 * distributed under the License is distributed on an "AS IS" BASIS,
010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011 * See the License for the specific language governing permissions and
012 * limitations under the License.
013 */
014package org.gbif.ws.server;
015
016import java.io.IOException;
017import java.io.InputStream;
018
019import javax.servlet.ReadListener;
020import javax.servlet.ServletInputStream;
021
022public class DelegatingServletInputStream extends ServletInputStream {
023
024  private final InputStream sourceStream;
025
026  private boolean finished = false;
027
028  public DelegatingServletInputStream(InputStream sourceStream) {
029    this.sourceStream = sourceStream;
030  }
031
032  public final InputStream getSourceStream() {
033    return this.sourceStream;
034  }
035
036  @Override
037  public int read() throws IOException {
038    int data = this.sourceStream.read();
039    if (data == -1) {
040      this.finished = true;
041    }
042    return data;
043  }
044
045  @Override
046  public int read(byte[] b, int off, int len) throws IOException {
047    int data = this.sourceStream.read(b, off, len);
048    if (data == -1) {
049      this.finished = true;
050    }
051    return data;
052  }
053
054  @Override
055  public int available() throws IOException {
056    return this.sourceStream.available();
057  }
058
059  @Override
060  public void close() throws IOException {
061    super.close();
062    this.sourceStream.close();
063  }
064
065  @Override
066  public boolean isFinished() {
067    return this.finished;
068  }
069
070  @Override
071  public boolean isReady() {
072    return true;
073  }
074
075  @Override
076  public void setReadListener(ReadListener readListener) {
077    throw new UnsupportedOperationException();
078  }
079}