1 6 21 22 package de.schlichtherle.io.util; 23 24 import java.io.*; 25 26 34 public class SynchronizedOutputStream extends OutputStream { 35 38 protected final Object lock; 39 40 43 protected OutputStream out; 44 45 51 public SynchronizedOutputStream(final OutputStream out) { 52 this(out, null); 53 } 54 55 62 public SynchronizedOutputStream(final OutputStream out, final Object lock) { 63 this.out = out; 64 this.lock = lock != null ? lock : this; 65 } 66 67 public void write(int b) throws IOException { 68 synchronized (lock) { 69 out.write(b); 70 } 71 } 72 73 public void write(byte[] b) throws IOException { 74 synchronized (lock) { 75 write(b, 0, b.length); 76 } 77 } 78 79 public void write(byte[] b, int off, int len) throws IOException { 80 synchronized (lock) { 81 out.write(b, off, len); 82 } 83 } 84 85 public void flush() throws IOException { 86 synchronized (lock) { 87 out.flush(); 88 } 89 } 90 91 public void close() throws IOException { 92 synchronized (lock) { 93 try { 94 flush(); 95 } finally { 96 out.close(); 97 } 98 } 99 } 100 } 101 | Popular Tags |