001 /* 002 * Licensed under the Apache License, Version 2.0 (the "License"); 003 * you may not use this file except in compliance with the License. 004 * You may obtain a copy of the License at 005 * 006 * http://www.apache.org/licenses/LICENSE-2.0 007 * 008 * Unless required by applicable law or agreed to in writing, software 009 * distributed under the License is distributed on an "AS IS" BASIS, 010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 011 * See the License for the specific language governing permissions and 012 * limitations under the License. 013 */ 014 package org.apache.commons.classscan.builtin; 015 016 import java.io.FilterInputStream; 017 import java.io.IOException; 018 import java.io.InputStream; 019 import java.util.jar.JarEntry; 020 import java.util.jar.JarFile; 021 022 import org.apache.commons.classscan.ResourceFile; 023 024 public class JarResourceFile implements ResourceFile { 025 026 final JarFile jarFile; 027 final JarEntry jarEntry; 028 029 public JarResourceFile(JarFile jarFile, JarEntry jarEntry) { 030 this.jarFile = jarFile; 031 this.jarEntry = jarEntry; 032 } 033 034 @Override 035 public InputStream getBytes() throws IOException { 036 final InputStream is = jarFile.getInputStream(jarEntry); 037 // close the jarFile when stream is closed 038 return new FilterInputStream(is) { 039 @Override 040 public void close() throws IOException { 041 super.close(); 042 jarFile.close(); 043 } 044 }; 045 } 046 047 @Override 048 public String getFileName() { 049 return jarEntry.getName(); 050 } 051 }