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.server.util; 018 019import static org.apache.juneau.commons.utils.Shorts.*; 020 021import java.io.*; 022 023import org.apache.juneau.marshall.encoders.*; 024 025import jakarta.servlet.*; 026 027/** 028 * A wrapped {@link ServletOutputStream} with an added <c>finish()</c> method. 029 * 030 */ 031@SuppressWarnings({ 032 "resource" // os and sos are servlet-container-managed streams passed in by the caller; closed by the container 033}) 034public class FinishableServletOutputStream extends ServletOutputStream implements Finishable { 035 036 final OutputStream os; 037 final ServletOutputStream sos; 038 final Finishable f; 039 040 /** 041 * Constructor. 042 * 043 * @param os The wrapped output stream. Must not be <jk>null</jk>. 044 */ 045 public FinishableServletOutputStream(OutputStream os) { 046 this.os = os; 047 this.sos = (os instanceof ServletOutputStream os2 ? os2 : null); 048 this.f = (os instanceof Finishable os2 ? os2 : null); 049 } 050 051 @Override /* Overridden from OutputStream */ 052 public final void close() throws IOException { 053 os.close(); 054 } 055 056 /** 057 * Calls {@link Finishable#finish()} on the underlying output stream. 058 * 059 * <p> 060 * A no-op if the underlying output stream does not implement the {@link Finishable} interface. 061 */ 062 @Override /* Overridden from Finishable */ 063 public void finish() throws IOException { 064 if (nn(f)) 065 f.finish(); 066 } 067 068 @Override /* Overridden from OutputStream */ 069 public final void flush() throws IOException { 070 os.flush(); 071 } 072 073 @Override /* Overridden from ServletOutputStream */ 074 public boolean isReady() { return sos == null || sos.isReady(); } 075 076 @Override /* Overridden from ServletOutputStream */ 077 public void setWriteListener(WriteListener arg0) { 078 if (nn(sos)) 079 sos.setWriteListener(arg0); 080 } 081 082 @Override /* Overridden from OutputStream */ 083 public final void write(byte[] b, int off, int len) throws IOException { 084 os.write(b, off, len); 085 } 086 087 @Override /* Overridden from OutputStream */ 088 public final void write(int b) throws IOException { 089 os.write(b); 090 } 091}