001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.commons.jci.compilers;
019:
020: import java.io.ByteArrayInputStream;
021: import java.io.File;
022: import java.io.FileDescriptor;
023: import java.io.FileNotFoundException;
024: import java.io.IOException;
025: import java.io.InputStream;
026:
027: import org.apache.commons.jci.readers.ResourceReader;
028: import org.apache.commons.jci.utils.ConversionUtils;
029:
030: /**
031: *
032: * @author tcurdt
033: */
034: public final class FileInputStreamProxy extends InputStream {
035:
036: private final static ThreadLocal readerThreadLocal = new ThreadLocal();
037:
038: private final InputStream in;
039: private final String name;
040:
041: public static void setResourceReader(final ResourceReader pReader) {
042: readerThreadLocal.set(pReader);
043: }
044:
045: public FileInputStreamProxy(File pFile)
046: throws FileNotFoundException {
047: this ("" + pFile);
048: }
049:
050: public FileInputStreamProxy(FileDescriptor fdObj) {
051: throw new RuntimeException();
052: }
053:
054: public FileInputStreamProxy(String pName)
055: throws FileNotFoundException {
056: name = ConversionUtils.getResourceNameFromFileName(pName);
057:
058: final ResourceReader reader = (ResourceReader) readerThreadLocal
059: .get();
060:
061: if (reader == null) {
062: throw new RuntimeException(
063: "forgot to set the ResourceReader for this thread?");
064: }
065:
066: final byte[] bytes = reader.getBytes(name);
067:
068: if (bytes == null) {
069: throw new FileNotFoundException(name);
070: }
071:
072: in = new ByteArrayInputStream(bytes);
073: }
074:
075: public int read() throws IOException {
076: return in.read();
077: }
078:
079: public int available() throws IOException {
080: return in.available();
081: }
082:
083: public void close() throws IOException {
084: in.close();
085: }
086:
087: public synchronized void mark(int readlimit) {
088: in.mark(readlimit);
089: }
090:
091: public boolean markSupported() {
092: return in.markSupported();
093: }
094:
095: public int read(byte[] b, int off, int len) throws IOException {
096: return in.read(b, off, len);
097: }
098:
099: public int read(byte[] b) throws IOException {
100: return in.read(b);
101: }
102:
103: public synchronized void reset() throws IOException {
104: in.reset();
105: }
106:
107: public long skip(long n) throws IOException {
108: return in.skip(n);
109: }
110: }
|