All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.myfaces.renderkit.html.util.HtmlBufferResponseWriterWrapper Maven / Gradle / Ivy

Go to download

The MyFaces Commons Subproject provides base classes for usage in both the MyFaces implementation and the MyFaces Tomahawk components. This is also a general set of utility classes for usage in your JSF projects independent of the implementation you might be deciding upon.

The newest version!
/*
 * Copyright 2004 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.myfaces.renderkit.html.util;

import org.apache.myfaces.renderkit.html.HtmlResponseWriterImpl;

import javax.faces.context.ResponseWriter;
import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.io.IOException;

/**
 * @author Sylvain Vieujot (latest modification by $Author: grantsmith $)
 * @version $Revision: 169649 $ $Date: 2005-05-11 17:47:12 +0200 (Wed, 11 May 2005) $
 */
public class HtmlBufferResponseWriterWrapper extends HtmlResponseWriterImpl {

    private ByteArrayOutputStream stream;
    private PrintWriter writer;

    static public HtmlBufferResponseWriterWrapper getInstance(ResponseWriter initialWriter){
        ByteArrayOutputStream instanceStream = new ByteArrayOutputStream();
        PrintWriter instanceWriter = new PrintWriter(instanceStream, true);

        return new HtmlBufferResponseWriterWrapper(initialWriter, instanceStream, instanceWriter);
    }

    private HtmlBufferResponseWriterWrapper(ResponseWriter initialWriter, ByteArrayOutputStream stream, PrintWriter writer){
        super(writer, initialWriter==null?null:initialWriter.getContentType(), initialWriter==null?null:initialWriter.getCharacterEncoding());
        this.stream = stream;
        this.writer = writer;
    }

    public String toString(){
        try{
            stream.flush();
            writer.close();           
            return stream.toString( getCharacterEncoding() );
        }catch(UnsupportedEncodingException e){
            // an attempt to set an invalid character encoding would have caused this exception before
            throw new RuntimeException("ResponseWriter accepted invalid character encoding " + getCharacterEncoding());
        } catch (IOException e) {
            throw new RuntimeException( e );
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy