001/* 002 * The contents of this file are subject to the terms of the Common Development and 003 * Distribution License (the License). You may not use this file except in compliance with the 004 * License. 005 * 006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the 007 * specific language governing permission and limitations under the License. 008 * 009 * When distributing Covered Software, include this CDDL Header Notice in each file and include 010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL 011 * Header, with the fields enclosed by brackets [] replaced by your own identifying 012 * information: "Portions copyright [year] [name of copyright owner]". 013 * 014 * Copyright 2015 ForgeRock AS. 015 */ 016package org.forgerock.audit.events.handlers.writers; 017 018import java.io.IOException; 019import java.io.Writer; 020 021/** 022 * Wraps a {@link TextWriter} in a {@link Writer}. 023 */ 024public class TextWriterAdapter extends Writer implements TextWriter { 025 026 private final TextWriter delegate; 027 028 /** 029 * Creates the writer. 030 * 031 * @param delegate 032 * Delegate writer. 033 */ 034 public TextWriterAdapter(TextWriter delegate) { 035 this.delegate = delegate; 036 } 037 038 @Override 039 public void write(char[] cbuf, int off, int len) throws IOException { 040 write(new String(cbuf, off, len)); 041 } 042 043 @Override 044 public void write(int c) throws IOException { 045 write(String.valueOf((char) c)); 046 } 047 048 @Override 049 public void write(char[] cbuf) throws IOException { 050 write(String.valueOf(cbuf)); 051 } 052 053 @Override 054 public void write(String str) throws IOException { 055 delegate.write(str); 056 } 057 058 @Override 059 public void write(String str, int off, int len) throws IOException { 060 write(str.substring(off, off+len)); 061 } 062 063 @Override 064 public void flush() throws IOException { 065 delegate.flush(); 066 } 067 068 @Override 069 public void close() throws IOException { 070 shutdown(); 071 } 072 073 @Override 074 public void shutdown() { 075 delegate.shutdown(); 076 } 077 078 @Override 079 public long getBytesWritten() { 080 return delegate.getBytesWritten(); 081 } 082}