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.internal; 014 015import java.io.*; 016 017/** 018 * Wraps an existing {@link Writer} where the {@link #close()} method is a no-op. 019 * 020 * <p> 021 * Useful in cases where you're working with streams that should not be implicitly closed. 022 * 023 * <h5 class='section'>See Also:</h5><ul> 024 * </ul> 025 */ 026public final class NoCloseWriter extends Writer { 027 028 private final Writer w; 029 030 /** 031 * Constructor. 032 * 033 * @param w The writer to wrap. 034 */ 035 public NoCloseWriter(Writer w) { 036 this.w = w; 037 } 038 039 @Override /* Writer */ 040 public Writer append(char c) throws IOException { 041 return w.append(c); 042 } 043 044 @Override /* Writer */ 045 public Writer append(CharSequence csq) throws IOException { 046 return w.append(csq); 047 } 048 049 @Override /* Writer */ 050 public Writer append(CharSequence csq, int start, int end) throws IOException { 051 return w.append(csq, start, end); 052 } 053 054 @Override /* Writer */ 055 public void close() throws IOException { 056 w.flush(); 057 } 058 059 @Override /* Writer */ 060 public void flush() throws IOException { 061 w.flush(); 062 } 063 064 @Override /* Writer */ 065 public void write(char[] cbuf) throws IOException { 066 w.write(cbuf); 067 } 068 069 @Override /* Writer */ 070 public void write(char[] cbuf, int off, int len) throws IOException { 071 w.write(cbuf, off, len); 072 } 073 074 @Override /* Writer */ 075 public void write(int c) throws IOException { 076 w.write(c); 077 } 078 079 @Override /* Writer */ 080 public void write(String str) throws IOException { 081 w.write(str); 082 } 083 084 @Override /* Writer */ 085 public void write(String str, int off, int len) throws IOException { 086 w.write(str, off, len); 087 } 088 089 @Override /* Object */ 090 public String toString() { 091 return w.toString(); 092 } 093}