1 package fr.in2p3.jsaga.impl.job.instance.stream;
2
3 import java.io.*;
4
5
6
7
8
9
10
11
12
13
14
15
16
17 public class OutputStreamContainer extends OutputStream {
18 private boolean m_isClosed;
19
20 public OutputStreamContainer() {
21 m_isClosed = false;
22 }
23
24 public boolean isClosed() {
25 return m_isClosed;
26 }
27
28 public InputStream getInputStream() throws InterruptedException {
29 while(!m_isClosed) {
30 Thread.currentThread().sleep(100);
31 }
32 if (m_buffer != null) {
33 byte[] buffer = m_buffer.toByteArray();
34 m_buffer.reset();
35 return new ByteArrayInputStream(buffer);
36 } else {
37 return new ByteArrayInputStream(new byte[]{});
38 }
39 }
40
41
42
43 public void write(int b) throws IOException {this.stream().write(b);}
44 public void write(byte[] b) throws IOException {this.stream().write(b);}
45 public void write(byte[] b, int off, int len) throws IOException {this.stream().write(b, off, len);}
46 public void flush() throws IOException {this.stream().flush();}
47 public void close() throws IOException {this.stream().close(); m_isClosed=true;}
48
49
50
51 private ByteArrayOutputStream m_buffer;
52 private OutputStream stream() throws IOException {
53 if (m_buffer == null) {
54 m_buffer = new ByteArrayOutputStream();
55 }
56 return m_buffer;
57 }
58 }