KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * SynchronizedInputStream.java
3  *
4  * Created on 23. November 2006, 20:21
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 InputStream}
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 SynchronizedInputStream extends InputStream {
35     /**
36      * The object to synchronize on - never <code>null</code>.
37      */

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

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

51     public SynchronizedInputStream(final InputStream in) {
52     this(in, null);
53     }
54
55     /**
56      * Constructs a new synchronized input stream.
57      *
58      * @param in The input 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 SynchronizedInputStream(final InputStream in, final Object JavaDoc lock) {
63     this.in = in;
64     this.lock = lock != null ? lock : this;
65     }
66
67     public int read() throws IOException {
68     synchronized (lock) {
69         return in.read();
70     }
71     }
72
73     public int read(byte[] b) throws IOException {
74     synchronized (lock) {
75         return read(b, 0, b.length);
76     }
77     }
78
79     public int read(byte[] b, int off, int len) throws IOException {
80     synchronized (lock) {
81         return in.read(b, off, len);
82     }
83     }
84
85     public long skip(long n) throws IOException {
86     synchronized (lock) {
87         return in.skip(n);
88     }
89     }
90
91     public int available() throws IOException {
92     synchronized (lock) {
93         return in.available();
94     }
95     }
96
97     public void close() throws IOException {
98     synchronized (lock) {
99         in.close();
100     }
101     }
102
103     public void mark(int readlimit) {
104     synchronized (lock) {
105         in.mark(readlimit);
106     }
107     }
108
109     public void reset() throws IOException {
110     synchronized (lock) {
111         in.reset();
112     }
113     }
114
115     public boolean markSupported() {
116     synchronized (lock) {
117         return in.markSupported();
118     }
119     }
120 }
121
Popular Tags