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 */ 023public final class NoCloseWriter extends Writer { 024 025 private final Writer w; 026 027 /** 028 * Constructor. 029 * 030 * @param w The writer to wrap. 031 */ 032 public NoCloseWriter(Writer w) { 033 this.w = w; 034 } 035 036 @Override /* Writer */ 037 public Writer append(char c) throws IOException { 038 return w.append(c); 039 } 040 041 @Override /* Writer */ 042 public Writer append(CharSequence csq) throws IOException { 043 return w.append(csq); 044 } 045 046 @Override /* Writer */ 047 public Writer append(CharSequence csq, int start, int end) throws IOException { 048 return w.append(csq, start, end); 049 } 050 051 @Override /* Writer */ 052 public void close() throws IOException { 053 w.flush(); 054 } 055 056 @Override /* Writer */ 057 public void flush() throws IOException { 058 w.flush(); 059 } 060 061 @Override /* Writer */ 062 public void write(char[] cbuf) throws IOException { 063 w.write(cbuf); 064 } 065 066 @Override /* Writer */ 067 public void write(char[] cbuf, int off, int len) throws IOException { 068 w.write(cbuf, off, len); 069 } 070 071 @Override /* Writer */ 072 public void write(int c) throws IOException { 073 w.write(c); 074 } 075 076 @Override /* Writer */ 077 public void write(String str) throws IOException { 078 w.write(str); 079 } 080 081 @Override /* Writer */ 082 public void write(String str, int off, int len) throws IOException { 083 w.write(str, off, len); 084 } 085 086 @Override /* Object */ 087 public String toString() { 088 return w.toString(); 089 } 090}