001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.rest.util;
014
015import java.io.*;
016
017import jakarta.servlet.*;
018
019import org.apache.juneau.encoders.*;
020
021/**
022 * A wrapped {@link ServletOutputStream} with an added <c>finish()</c> method.
023 *
024 * <h5 class='section'>See Also:</h5><ul>
025 * </ul>
026 */
027public class FinishableServletOutputStream extends ServletOutputStream implements Finishable {
028
029   final OutputStream os;
030   final ServletOutputStream sos;
031   final Finishable f;
032
033   /**
034    * Constructor.
035    *
036    * @param os The wrapped output stream.
037    */
038   public FinishableServletOutputStream(OutputStream os) {
039      this.os = os;
040      this.sos = (os instanceof ServletOutputStream ? (ServletOutputStream)os : null);
041      this.f = (os instanceof Finishable ? (Finishable)os : null);
042   }
043
044   @Override /* OutputStream */
045   public final void write(byte[] b, int off, int len) throws IOException {
046      os.write(b, off, len);
047   }
048
049   @Override /* OutputStream */
050   public final void write(int b) throws IOException {
051      os.write(b);
052   }
053
054   @Override /* OutputStream */
055   public final void flush() throws IOException {
056      os.flush();
057   }
058
059   @Override /* OutputStream */
060   public final void close() throws IOException {
061      os.close();
062   }
063
064   @Override /* ServletOutputStream */
065   public boolean isReady() {
066      return sos == null ? true : sos.isReady();
067   }
068
069   @Override /* ServletOutputStream */
070   public void setWriteListener(WriteListener arg0) {
071      if (sos != null)
072         sos.setWriteListener(arg0);
073   }
074
075   /**
076    * Calls {@link Finishable#finish()} on the underlying output stream.
077    *
078    * <p>
079    * A no-op if the underlying output stream does not implement the {@link Finishable} interface.
080    */
081   @Override /* Finishable */
082   public void finish() throws IOException {
083      if (f != null)
084         f.finish();
085   }
086}