01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
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: * @author tcurdt
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: public static void setResourceStore(final ResourceStore pStore) {
42: storeThreadLocal.set(pStore);
43: }
44:
45: public FileOutputStreamProxy(File pFile, boolean append)
46: throws FileNotFoundException {
47: this ("" + pFile);
48: }
49:
50: public FileOutputStreamProxy(File pFile)
51: 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)
60: throws FileNotFoundException {
61: this (pName);
62: }
63:
64: public FileOutputStreamProxy(String pName)
65: throws FileNotFoundException {
66: name = ConversionUtils.getResourceNameFromFileName(pName);
67: }
68:
69: public void write(int value) throws IOException {
70: out.write(value);
71: }
72:
73: public void close() throws IOException {
74: out.close();
75:
76: final ResourceStore store = (ResourceStore) storeThreadLocal
77: .get();
78:
79: if (store == null) {
80: throw new RuntimeException(
81: "forgot to set the ResourceStore for this thread?");
82: }
83:
84: store.write(name, out.toByteArray());
85: }
86:
87: public void flush() throws IOException {
88: out.flush();
89: }
90:
91: public void write(byte[] b, int off, int len) throws IOException {
92: out.write(b, off, len);
93: }
94:
95: public void write(byte[] b) throws IOException {
96: out.write(b);
97: }
98: }
|