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