public Writer toWriter() {
		return new Writer() {
			@Override
			public Writer append(char c) {
				consumer.accept(new String(new char[]{c}));
				return this;
			}

			@Override
			public Writer append(CharSequence csq) {
				if (csq instanceof String) {
					consumer.accept((String) csq);
				} else {
					consumer.accept(toStringSafely(csq));
				}
				return this;
			}

			@Override
			public Writer append(CharSequence csq, int start, int end) {
				if (csq instanceof String) {
					consumer.accept(((String) csq).substring(start, end));
				} else {
					consumer.accept(toStringSafely(csq.subSequence(start, end)));
				}
				return this;
			}

			private String toStringSafely(CharSequence csq) {
				String asString = csq.toString();
				if (asString.length() == csq.length()) {
					return asString;
				} else {
					// It's pretty easy to implement CharSequence.toString() incorrectly 
					// http://stackoverflow.com/a/15870428/1153071
					// but for String, we know we won't have them, thus the fast-path above
					Errors.log().accept(new IllegalArgumentException(csq.getClass() + " did not implement toString() correctly."));
					char[] chars = new char[csq.length()];
					for (int i = 0; i < chars.length; ++i) {
						chars[i] = csq.charAt(i);
					}
					return new String(chars);
				}
			}

			@Override
			public void close() throws IOException {}

			@Override
			public void flush() throws IOException {}

			@Override
			public void write(char[] cbuf, int off, int len) throws IOException {
				consumer.accept(new String(cbuf, off, len));
			}

			@Override
			public void write(String str) {
				consumer.accept(str);
			}

			@Override
			public void write(String str, int off, int len) {
				consumer.accept(str.substring(off, off + len));
			}
		};
	}
public Writer toWriter ( ) { return new Writer ( ) { @ Override public Writer append ( char c ) { consumer . accept ( new String ( new char [ ] { c } ) ) ; return this ; } @ Override public Writer append ( CharSequence csq ) { if ( csq instanceof String ) { consumer . accept ( ( String ) csq ) ; } else { consumer . accept ( toStringSafely ( csq ) ) ; } return this ; } @ Override public Writer append ( CharSequence csq , int start , int end ) { if ( csq instanceof String ) { consumer . accept ( ( ( String ) csq ) . substring ( start , end ) ) ; } else { consumer . accept ( toStringSafely ( csq . subSequence ( start , end ) ) ) ; } return this ; } private String toStringSafely ( CharSequence csq ) { String asString = csq . toString ( ) ; if ( asString . length ( ) == csq . length ( ) ) { return asString ; } else { Errors . log ( ) . accept ( new IllegalArgumentException ( csq . getClass ( ) + " did not implement toString() correctly." ) ) ; char [ ] chars = new char [ csq . length ( ) ] ; for ( int i = 0 ; i < chars . length ; ++ i ) { chars [ i ] = csq . charAt ( i ) ; } return new String ( chars ) ; } } @ Override public void close ( ) throws IOException { } @ Override public void flush ( ) throws IOException { } @ Override public void write ( char [ ] cbuf , int off , int len ) throws IOException { consumer . accept ( new String ( cbuf , off , len ) ) ; } @ Override public void write ( String str ) { consumer . accept ( str ) ; } @ Override public void write ( String str , int off , int len ) { consumer . accept ( str . substring ( off , off + len ) ) ; } } ; }
Creates a Writer which passes its content to this StringPrinter.