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.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
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 }