1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.jci.compilers;
19
20 import java.io.ByteArrayOutputStream;
21 import java.io.File;
22 import java.io.FileDescriptor;
23 import java.io.FileNotFoundException;
24 import java.io.IOException;
25 import java.io.OutputStream;
26
27 import org.apache.commons.jci.stores.ResourceStore;
28 import org.apache.commons.jci.utils.ConversionUtils;
29
30
31
32
33
34 public final class FileOutputStreamProxy extends OutputStream {
35
36 private final static ThreadLocal storeThreadLocal = new ThreadLocal();
37
38 private final ByteArrayOutputStream out = new ByteArrayOutputStream();
39 private final String name;
40
41
42 public static void setResourceStore( final ResourceStore pStore ) {
43 storeThreadLocal.set(pStore);
44 }
45
46
47 public FileOutputStreamProxy(File pFile, boolean append) throws FileNotFoundException {
48 this("" + pFile);
49 }
50
51 public FileOutputStreamProxy(File pFile) throws FileNotFoundException {
52 this("" + pFile);
53 }
54
55 public FileOutputStreamProxy(FileDescriptor fdObj) {
56 throw new RuntimeException();
57 }
58
59 public FileOutputStreamProxy(String pName, boolean append) throws FileNotFoundException {
60 this(pName);
61 }
62
63 public FileOutputStreamProxy(String pName) throws FileNotFoundException {
64 name = ConversionUtils.getResourceNameFromFileName(pName);
65 }
66
67 public void write(int value) throws IOException {
68 out.write(value);
69 }
70
71 public void close() throws IOException {
72 out.close();
73
74 final ResourceStore store = (ResourceStore) storeThreadLocal.get();
75
76 if (store == null) {
77 throw new RuntimeException("forgot to set the ResourceStore for this thread?");
78 }
79
80 store.write(name, out.toByteArray());
81 }
82
83 public void flush() throws IOException {
84 out.flush();
85 }
86
87 public void write(byte[] b, int off, int len) throws IOException {
88 out.write(b, off, len);
89 }
90
91 public void write(byte[] b) throws IOException {
92 out.write(b);
93 }
94 }