/*
 * Copyright (C) 2014 Francis Galiegue <fgaliegue@gmail.com>
 *
 * 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 com.github.parboiled1.grappa.misc;

import com.google.common.io.CharSink;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;

/**
 * A {@link CharSink} over {@code System.out}
 */
public final class SystemOutCharSink
    extends CharSink
{
    public static final CharSink INSTANCE = new SystemOutCharSink();

    private SystemOutCharSink()
    {
    }

    /**
     * Opens a new {@link Writer} for writing to this sink. This method
     * should return a new, independent writer each time it is called.
     *
     * <p>The caller is responsible for ensuring that the returned writer is
     * closed.
     *
     * @throws IOException if an I/O error occurs in the process of opening
     * the writer
     */
    @Override
    public Writer openStream()
        throws IOException
    {
        return new OutputStreamWriter(System.out);
    }
}

@@ -699,16 +699,75 @@
 rSink;%0A%0A
+import javax.annotation.Nonnull;%0Aimport java.io.Closeable;%0A
 import j
@@ -801,28 +801,16 @@
 java.io.
-OutputStream
 Writer;%0A
@@ -817,31 +817,36 @@
 import java.
+n
 io.
-Writ
+CharBuff
 er;%0A%0A/**%0A * 
@@ -1523,43 +1523,1349 @@
 urn 
-new OutputStreamWriter(System.out);
+SystemOutWriter.INSTANCE;%0A    %7D%0A%0A    /**%0A     * Writer returned by %7B@link CharSink#openStream()%7D%0A     *%0A     * %3Cp%3EIt appears that this method opens a new writer for %3Cem%3Eeach%3C/em%3E of%0A     * its operations; not sure whether this is a bug but on the other hand the%0A     * base class does not implement %7B@link Closeable%7D, so maybe that explains%0A     * part of it. But not all.%3C/p%3E%0A     *%0A     * %3Cp%3EThis is therefore the %22new, independent writer%22 returned by this%0A     * method which is in fact always the same; it just delegates to %7B@code%0A     * System.out%7D and only overwrites the base method which all %7B@code Writer%7Ds%0A     * must implement.%3C/p%3E%0A     */%0A    private static final class SystemOutWriter%0A        extends Writer%0A    %7B%0A        @SuppressWarnings(%22IOResourceOpenedButNotSafelyClosed%22)%0A        private static final Writer INSTANCE = new SystemOutWriter();%0A%0A        @Override%0A        public void write(@Nonnull final char%5B%5D cbuf, final int off,%0A            final int len)%0A            throws IOException%0A        %7B%0A            System.out.append(CharBuffer.wrap(cbuf, off, len));%0A        %7D%0A%0A        @Override%0A        public void flush()%0A            throws IOException%0A        %7B%0A            System.out.flush();%0A        %7D%0A%0A        @Override%0A        public void close()%0A            throws IOException%0A        %7B%0A            // Nothing%0A        %7D
 %0A   

implement a custom Writer for System.out