Skip to content

Commit

Permalink
Added Synchronization to MultiOutputStream
Browse files Browse the repository at this point in the history
- This prevents ConcurrentModificationExceptions of the contained
streams when adding/printing.
  • Loading branch information
defaultsamson committed Jul 2, 2016
1 parent 49e863d commit 5f0e02e
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions src/samson/stream/MultiOutputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ public MultiOutputStream(OutputStream... streams) {

for (OutputStream out : streams) {
if (out != null)
this.streams.add(out);
addStream(out);
}
}

public void addStream(OutputStream stream) {
streams.add(stream);
synchronized (streams) {
streams.add(stream);
}
}

public List<OutputStream> getStreams() {
Expand All @@ -27,36 +29,46 @@ public List<OutputStream> getStreams() {

@Override
public void write(int b) throws IOException {
for (OutputStream stream : streams) {
stream.write(b);
synchronized (streams) {
for (OutputStream stream : streams) {
stream.write(b);
}
}
}

@Override
public void write(byte[] b) throws IOException {
for (OutputStream stream : streams) {
stream.write(b);
synchronized (streams) {
for (OutputStream stream : streams) {
stream.write(b);
}
}
}

@Override
public void write(byte[] b, int off, int len) throws IOException {
for (OutputStream stream : streams) {
stream.write(b, off, len);
synchronized (streams) {
for (OutputStream stream : streams) {
stream.write(b, off, len);
}
}
}

@Override
public void flush() throws IOException {
for (OutputStream stream : streams) {
stream.flush();
synchronized (streams) {
for (OutputStream stream : streams) {
stream.flush();
}
}
}

@Override
public void close() throws IOException {
for (OutputStream stream : streams) {
stream.close();
synchronized (streams) {
for (OutputStream stream : streams) {
stream.close();
}
}
}
}

0 comments on commit 5f0e02e

Please sign in to comment.