KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > schlichtherle > io > util > SynchronizedOutputStream


1 /*
2  * SynchronizedOutputStream.java
3  *
4  * Created on 23. November 2006, 20:24
5  */

6 /*
7  * Copyright 2006 Schlichtherle IT Services
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */

21
22 package de.schlichtherle.io.util;
23
24 import java.io.*;
25
26 /**
27  * A decorator which synchronizes all access to an {@link OutputStream}
28  * via an object provided to its constructor.
29  *
30  * @author Christian Schlichtherle
31  * @version @version@
32  * @since TrueZIP 6.4
33  */

34 public class SynchronizedOutputStream extends OutputStream {
35     /**
36      * The object to synchronize on - never <code>null</code>.
37      */

38     protected final Object JavaDoc lock;
39
40     /**
41      * The decorated output stream.
42      */

43     protected OutputStream out;
44
45     /**
46      * Constructs a new synchronized output stream.
47      * This object will synchronize on itself.
48      *
49      * @param out The output stream to wrap in this decorator.
50      */

51     public SynchronizedOutputStream(final OutputStream out) {
52     this(out, null);
53     }
54
55     /**
56      * Constructs a new synchronized output stream.
57      *
58      * @param out The output stream to wrap in this decorator.
59      * @param lock The object to synchronize on.
60      * If <code>null</code>, then this object is used, not the stream.
61      */

62     public SynchronizedOutputStream(final OutputStream out, final Object JavaDoc 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