View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      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  
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  }