public static String encodeComponent(UIComponent component, FacesContext context) {
        FastStringWriter output = new FastStringWriter();
        ResponseWriter originalWriter = context.getResponseWriter();

        if (originalWriter != null) {
            context.setResponseWriter(originalWriter.cloneWithWriter(output));
        }
        else {
            context.setResponseWriter(RendererUtils.getRenderKit(context).createResponseWriter(output, "text/html", "UTF-8"));
        }

        try {
            component.encodeAll(context);
        }
        catch (IOException e) {
            throw new UncheckedIOException(e);
        }
        finally {
            if (originalWriter != null) {
                context.setResponseWriter(originalWriter);
            }
        }

        return output.toString();
    }
Encodes the given component locally as HTML.
@param component The component to capture HTML output for.
@param context The current FacesContext.
@return The encoded HTML output of the given component.
@throws UncheckedIOException Whenever something fails at I/O level. This would be quite unexpected as it happens locally.
primefaces/src/main/java/org/primefaces/util/ComponentUtils.java