Tests rewritten to new version, just the Writer version 2.0 does not yet implement Appendable
3 import java.io.BufferedWriter;
4 import java.io.IOException;
6 /** Fixing the problem caused by mixing subclassing and delegation in
7 * the <code>java.io.BufferedWriter</code>
9 * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
11 public final class Writer {
12 private final Impl impl;
13 private final ImplSeq seq;
15 private Writer(Impl impl, ImplSeq seq) {
19 public final void write(int c) throws IOException {
21 char[] arr = {(char) c};
22 impl.write(arr, 0, 1);
24 seq.write(new CharSeq(c));
28 public final void write(char cbuf[]) throws IOException {
30 impl.write(cbuf, 0, cbuf.length);
32 seq.write(new CharSeq(cbuf, 0, cbuf.length));
35 public final void write(char cbuf[], int off, int len) throws IOException {
37 impl.write(cbuf, off, len);
39 seq.write(new CharSeq(cbuf, off, len));
42 public final void write(String str) throws IOException {
44 impl.write(str, 0, str.length());
49 public final void write(String str, int off, int len) throws IOException {
51 impl.write(str, off, len);
53 seq.write(str.subSequence(off, off + len));
57 public final void flush() throws IOException {
64 public final void close() throws IOException {
72 public static Writer create(Impl impl) {
73 return new Writer(impl, null);
76 public static Writer create(ImplSeq impl) {
77 return new Writer(null, impl);
80 public static Writer create(final java.io.Writer w) {
81 return new Writer(new Impl() {
82 public void write(String str, int off, int len) throws IOException {
83 w.write(str, off, len);
86 public void write(char[] arr, int off, int len) throws IOException {
87 w.write(arr, off, len);
90 public void close() throws IOException {
94 public void flush() throws IOException {
100 public static Writer createBuffered(final Writer out) {
101 class Delegate extends java.io.Writer {
103 public void write(char[] cbuf, int off, int len) throws IOException {
104 out.write(cbuf, off, len);
108 public void flush() throws IOException {
113 public void close() throws IOException {
118 return create(new BufferedWriter(new Delegate()));
122 public static interface Impl {
123 public void close() throws IOException;
124 public void flush() throws IOException;
125 public void write(String str, int off, int len) throws IOException;
126 public void write(char[] arr, int off, int len) throws IOException;
128 public static interface ImplSeq {
129 public void close() throws IOException;
130 public void flush() throws IOException;
131 public void write(CharSequence seq) throws IOException;