~mortenoh/+junk/dhis2-detailed-import-export

« back to all changes in this revision

Viewing changes to dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/stream/BlockingPipedOutputStream.java

  • Committer: larshelge at gmail
  • Date: 2009-03-03 16:46:36 UTC
  • Revision ID: larshelge@gmail.com-20090303164636-2sjlrquo7ib1gf7r
Initial check-in

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.hisp.dhis.system.stream;
 
2
 
 
3
/*
 
4
 * Copyright (c) 2004-2007, University of Oslo
 
5
 * All rights reserved.
 
6
 *
 
7
 * Redistribution and use in source and binary forms, with or without
 
8
 * modification, are permitted provided that the following conditions are met:
 
9
 * * Redistributions of source code must retain the above copyright notice, this
 
10
 *   list of conditions and the following disclaimer.
 
11
 * * Redistributions in binary form must reproduce the above copyright notice,
 
12
 *   this list of conditions and the following disclaimer in the documentation
 
13
 *   and/or other materials provided with the distribution.
 
14
 * * Neither the name of the HISP project nor the names of its contributors may
 
15
 *   be used to endorse or promote products derived from this software without
 
16
 *   specific prior written permission.
 
17
 *
 
18
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 
19
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 
20
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 
21
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 
22
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 
23
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 
24
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 
25
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
26
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 
27
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
28
 */
 
29
 
 
30
import java.io.IOException;
 
31
import java.io.PipedInputStream;
 
32
import java.io.PipedOutputStream;
 
33
 
 
34
/**
 
35
 * @author Torgeir Lorange Ostby
 
36
 * @version $Id: BlockingPipedOutputStream.java 4597 2008-02-16 14:11:24Z torgeilo $
 
37
 */
 
38
public class BlockingPipedOutputStream
 
39
    extends PipedOutputStream
 
40
{
 
41
    private boolean connected = false;
 
42
 
 
43
    private boolean waiting = false;
 
44
 
 
45
    private boolean closed = false;
 
46
 
 
47
    // -------------------------------------------------------------------------
 
48
    // Overrides
 
49
    // -------------------------------------------------------------------------
 
50
 
 
51
    @Override
 
52
    public synchronized void connect( PipedInputStream snk )
 
53
        throws IOException
 
54
    {
 
55
        super.connect( snk );
 
56
        connected = true;
 
57
        notifyAll();
 
58
    }
 
59
 
 
60
    @Override
 
61
    public void write( byte[] b )
 
62
        throws IOException
 
63
    {
 
64
        if ( !connected )
 
65
        {
 
66
            block();
 
67
 
 
68
            if ( !connected )
 
69
            {
 
70
                return;
 
71
            }
 
72
        }
 
73
 
 
74
        super.write( b );
 
75
    }
 
76
 
 
77
    @Override
 
78
    public void write( int b )
 
79
        throws IOException
 
80
    {
 
81
        if ( !connected )
 
82
        {
 
83
            block();
 
84
 
 
85
            if ( !connected )
 
86
            {
 
87
                return;
 
88
            }
 
89
        }
 
90
 
 
91
        super.write( b );
 
92
    }
 
93
 
 
94
    @Override
 
95
    public void write( byte[] b, int off, int len )
 
96
        throws IOException
 
97
    {
 
98
        if ( !connected )
 
99
        {
 
100
            block();
 
101
 
 
102
            if ( !connected )
 
103
            {
 
104
                return;
 
105
            }
 
106
        }
 
107
 
 
108
        super.write( b, off, len );
 
109
    }
 
110
 
 
111
    @Override
 
112
    public void flush()
 
113
        throws IOException
 
114
    {
 
115
        if ( !connected )
 
116
        {
 
117
            block();
 
118
 
 
119
            if ( !connected )
 
120
            {
 
121
                return;
 
122
            }
 
123
        }
 
124
 
 
125
        super.flush();
 
126
    }
 
127
 
 
128
    @Override
 
129
    public synchronized void close()
 
130
        throws IOException
 
131
    {
 
132
        connected = false;
 
133
        closed = true;
 
134
 
 
135
        try
 
136
        {
 
137
            super.close();
 
138
        }
 
139
        finally
 
140
        {
 
141
            notifyAll();
 
142
        }
 
143
    }
 
144
 
 
145
    // -------------------------------------------------------------------------
 
146
    // Getters
 
147
    // -------------------------------------------------------------------------
 
148
 
 
149
    public boolean isWaiting()
 
150
    {
 
151
        return waiting;
 
152
    }
 
153
 
 
154
    public boolean isClosed()
 
155
    {
 
156
        return closed;
 
157
    }
 
158
 
 
159
    // -------------------------------------------------------------------------
 
160
    // Lock
 
161
    // -------------------------------------------------------------------------
 
162
 
 
163
    private synchronized void block()
 
164
        throws IOException
 
165
    {
 
166
        if ( closed )
 
167
        {
 
168
            throw new IOException( "Output stream is closed" );
 
169
        }
 
170
 
 
171
        if ( waiting )
 
172
        {
 
173
            throw new IOException( "Someone else is waiting on this pipe" );
 
174
        }
 
175
 
 
176
        waiting = true;
 
177
 
 
178
        try
 
179
        {
 
180
            wait();
 
181
        }
 
182
        catch ( InterruptedException e )
 
183
        {
 
184
            throw new IOException( "Interrupted!" );
 
185
        }
 
186
        finally
 
187
        {
 
188
            waiting = false;
 
189
        }
 
190
    }
 
191
}