1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/samples/delegatingwriterfinal/src-api1.0/api/Writer.java Sat Jun 14 09:53:06 2008 +0200
1.3 @@ -0,0 +1,96 @@
1.4 +package api;
1.5 +
1.6 +import java.io.BufferedWriter;
1.7 +import java.io.IOException;
1.8 +
1.9 +/** Fixing the problem caused by mixing subclassing and delegation in
1.10 + * the <code>java.io.BufferedWriter</code>
1.11 + *
1.12 + * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
1.13 + */
1.14 +public final class Writer {
1.15 + private final Impl impl;
1.16 +
1.17 + private Writer(Impl impl) {
1.18 + this.impl = impl;
1.19 + }
1.20 + public final void write(int c) throws IOException {
1.21 + char[] arr = { (char)c };
1.22 + impl.write(arr, 0, 1);
1.23 + }
1.24 +
1.25 + public final void write(char cbuf[]) throws IOException {
1.26 + impl.write(cbuf, 0, cbuf.length);
1.27 + }
1.28 + public final void write(char cbuf[], int off, int len) throws IOException {
1.29 + impl.write(cbuf, off, len);
1.30 + }
1.31 + public final void write(String str) throws IOException {
1.32 + impl.write(str, 0, str.length());
1.33 + }
1.34 + public final void write(String str, int off, int len) throws IOException {
1.35 + impl.write(str, off, len);
1.36 + }
1.37 + public final void flush() throws IOException {
1.38 + impl.flush();
1.39 + }
1.40 + public final void close() throws IOException {
1.41 + impl.close();
1.42 + }
1.43 +
1.44 + public static Writer create(Impl impl) {
1.45 + return new Writer(impl);
1.46 + }
1.47 +
1.48 + public static Writer create(final java.io.Writer w) {
1.49 + return new Writer(new Impl() {
1.50 + public void write(String str, int off, int len) throws IOException {
1.51 + w.write(str, off, len);
1.52 + }
1.53 +
1.54 + public void write(char[] arr, int off, int len) throws IOException {
1.55 + w.write(arr, off, len);
1.56 + }
1.57 +
1.58 + public void close() throws IOException {
1.59 + w.close();
1.60 + }
1.61 +
1.62 + public void flush() throws IOException {
1.63 + w.flush();
1.64 + }
1.65 + });
1.66 + }
1.67 +
1.68 + public static Writer createBuffered(final Writer out) {
1.69 + class Delegate extends java.io.Writer {
1.70 + @Override
1.71 + public void write(char[] cbuf, int off, int len) throws IOException {
1.72 + out.write(cbuf, off, len);
1.73 + }
1.74 +
1.75 + @Override
1.76 + public void flush() throws IOException {
1.77 + out.flush();
1.78 + }
1.79 +
1.80 + @Override
1.81 + public void close() throws IOException {
1.82 + out.close();
1.83 + }
1.84 +
1.85 + }
1.86 + return create(new BufferedWriter(new Delegate()));
1.87 + }
1.88 +
1.89 +
1.90 + public static interface Impl {
1.91 + public void close() throws IOException;
1.92 + public void flush() throws IOException;
1.93 + public void write(String str, int off, int len) throws IOException;
1.94 + public void write(char[] arr, int off, int len) throws IOException;
1.95 + }
1.96 +
1.97 +
1.98 +
1.99 +}