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.ByteArrayInputStream;
21  import java.io.File;
22  import java.io.FileDescriptor;
23  import java.io.FileNotFoundException;
24  import java.io.IOException;
25  import java.io.InputStream;
26  
27  import org.apache.commons.jci.readers.ResourceReader;
28  import org.apache.commons.jci.utils.ConversionUtils;
29  
30  /**
31   * 
32   * @author tcurdt
33   */
34  public final class FileInputStreamProxy extends InputStream {
35  
36      private final static ThreadLocal readerThreadLocal = new ThreadLocal();
37  
38      private final InputStream in;
39      private final String name;
40  
41      public static void setResourceReader( final ResourceReader pReader ) {
42          readerThreadLocal.set(pReader);
43      }
44  
45      public FileInputStreamProxy(File pFile) throws FileNotFoundException {
46          this("" + pFile);
47      }
48  
49      public FileInputStreamProxy(FileDescriptor fdObj) {
50          throw new RuntimeException();
51      }
52  
53      public FileInputStreamProxy(String pName) throws FileNotFoundException {
54          name = ConversionUtils.getResourceNameFromFileName(pName);
55  
56          final ResourceReader reader = (ResourceReader) readerThreadLocal.get();
57  
58          if (reader == null) {
59              throw new RuntimeException("forgot to set the ResourceReader for this thread?");
60          }
61  
62          final byte[] bytes = reader.getBytes(name);
63  
64          if (bytes == null) {
65              throw new FileNotFoundException(name);
66          }
67  
68          in = new ByteArrayInputStream(bytes);
69      }
70  
71      public int read() throws IOException {
72          return in.read();
73      }
74  
75      public int available() throws IOException {
76          return in.available();
77      }
78  
79      public void close() throws IOException {
80          in.close();
81      }
82  
83      public synchronized void mark(int readlimit) {
84          in.mark(readlimit);
85      }
86  
87      public boolean markSupported() {
88          return in.markSupported();
89      }
90  
91      public int read(byte[] b, int off, int len) throws IOException {
92          return in.read(b, off, len);
93      }
94  
95      public int read(byte[] b) throws IOException {
96          return in.read(b);
97      }
98  
99      public synchronized void reset() throws IOException {
100         in.reset();
101     }
102 
103     public long skip(long n) throws IOException {
104         return in.skip(n);
105     }
106 }