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