Created
July 25, 2020 14:59
-
-
Save MasterEx/ae991e164a5f45e40b2b29c65f12a250 to your computer and use it in GitHub Desktop.
Code segment from https://masterex.github.io/archive/2020/07/25/decorator-design-pattern-synergies.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class OutputStreamAdapter extends OutputStream implements Appendable, Closeable { | |
private OutputStream os; | |
private CheckedOutputStream cos; | |
private DeflaterOutputStream dos; | |
private Appendable appendable; | |
private PrintStream pos; | |
public OutputStreamAdapter(OutputStream os) { | |
setOutputStream(os); | |
} | |
public void addOutputStream(OutputStream o) { | |
setOutputStream(o); | |
} | |
private void setOutputStream(OutputStream o) { | |
os = o; | |
if (o instanceof CheckedOutputStream) { | |
cos = (CheckedOutputStream) o; | |
} | |
if (o instanceof DeflaterOutputStream) { | |
dos = (DeflaterOutputStream) o; | |
} | |
if (o instanceof Appendable) { | |
appendable = (Appendable) o; | |
} | |
if (o instanceof PrintStream) { | |
pos = (PrintStream) o; | |
} | |
} | |
@Override | |
public void write(int i) throws IOException { | |
os.write(i); | |
} | |
public Checksum getChecksum() { | |
if (cos != null) { | |
return cos.getChecksum(); | |
} else if (os instanceof OutputStreamAdapter) { | |
return ((OutputStreamAdapter) os).getChecksum(); | |
} | |
throw new UnsupportedOperationException("Decorator method not implemented."); | |
} | |
public void finish() throws IOException { | |
if (dos != null) { | |
dos.finish(); | |
} else if (os instanceof OutputStreamAdapter) { | |
((OutputStreamAdapter) os).finish(); | |
} | |
throw new UnsupportedOperationException("Decorator method not implemented."); | |
} | |
@Override | |
public void flush() throws IOException { | |
os.flush(); | |
} | |
@Override | |
public void write(byte[] b, int off, int len) throws IOException { | |
os.write(b, off, len); | |
} | |
@Override | |
public void write(byte[] b) throws IOException { | |
System.out.println("WRITE!"); | |
os.write(b); | |
} | |
@Override | |
public void close() throws IOException { | |
os.close(); | |
} | |
@Override | |
public Appendable append(CharSequence cs) throws IOException { | |
if (appendable != null) { | |
appendable.append(cs); | |
} | |
throw new UnsupportedOperationException("Decorator method not implemented."); | |
} | |
@Override | |
public Appendable append(CharSequence cs, int i, int i1) throws IOException { | |
if (appendable != null) { | |
appendable.append(cs, i, i1); | |
} | |
throw new UnsupportedOperationException("Decorator method not implemented."); | |
} | |
@Override | |
public Appendable append(char c) throws IOException { | |
if (appendable != null) { | |
appendable.append(c); | |
} | |
throw new UnsupportedOperationException("Decorator method not implemented."); | |
} | |
public void println(String x) { | |
if (pos != null) { | |
pos.println(x); | |
} else { | |
throw new UnsupportedOperationException("Decorator method not implemented."); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment