In this post, I am going to explain how to replicate an output stream into two separate streams.
We can do this by extending OutputStream class.
public class BiOutputStream extends OutputStream {
private OutputStream first;
private OutputStream second;
public BiOutputStream(OutputStream first, OutputStream second) {
this.first = first;
this.second = second;
}
.........
.........
}
Apart from extending, we need to override below methods.
a. public void close() throws IOException
b. public void flush() throws IOException
c. write method
1. public void write(byte[] b) throws IOException
2. public void write(byte[] b, int off, int len) throws IOException
3. public void write(int b) throws IOException
Close both output streams
@Override
public void close() throws IOException {
closeStream(first);
closeStream(second);
}
Flush the streams
@Override
public void flush() throws IOException {
first.flush();
second.flush();
}
Write the data to streams
@Override
public void write(byte[] b) throws IOException {
first.write(b);
second.write(b);
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
first.write(b, off, len);
second.write(b, off, len);
}
@Override
public void write(int b) throws IOException {
first.write(b);
second.write(b);
}
Find the below working application.
BiOutputStream.java
package com.sample.app.streams;
import java.io.IOException;
import java.io.OutputStream;
public class BiOutputStream extends OutputStream {
private OutputStream first;
private OutputStream second;
public BiOutputStream(OutputStream first, OutputStream second) {
if (first == null) {
throw new IllegalArgumentException("first stream is null");
}
if (second == null) {
throw new IllegalArgumentException("first stream is null");
}
this.first = first;
this.second = second;
}
@Override
public void close() throws IOException {
closeStream(first);
closeStream(second);
}
@Override
public void flush() throws IOException {
first.flush();
second.flush();
}
@Override
public void write(byte[] b) throws IOException {
first.write(b);
second.write(b);
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
first.write(b, off, len);
second.write(b, off, len);
}
@Override
public void write(int b) throws IOException {
first.write(b);
second.write(b);
}
private static void closeStream(OutputStream outputStream) {
if (outputStream == null) {
return;
}
try {
outputStream.close();
} catch (Throwable t) {
t.printStackTrace(System.err);
}
}
}
BiOutputStreamDemo.java
package com.sample.app.streams;
import java.io.IOException;
public class BiOutputStreamDemo {
public static void main(String[] args) throws IOException {
BiOutputStream bos = new BiOutputStream(System.out, System.err);
bos.write(new String("This message is published to both\n\t1. System.out and\n\t2. System.err\n").getBytes());
bos.close();
}
}
Output in Eclipse IDE
You may like
file and stream programs in Java
Check given path is a file and not a symbolic link in Java
Check given path is a directory and not a symbolic link in Java
Write InputStream to a file in Java
File separator, separatorChar, pathSeparator, pathSeparatorChar in Java
No comments:
Post a Comment