001/** 002 * Copyright 2010-2016 Boxfuse GmbH 003 * <p/> 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * <p/> 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * <p/> 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.avaje.classpath.scanner.internal.scanner.filesystem; 017 018import org.avaje.classpath.scanner.core.ClassPathScanException; 019import org.avaje.classpath.scanner.Resource; 020import org.avaje.classpath.scanner.internal.FileCopyUtils; 021 022import java.io.File; 023import java.io.FileInputStream; 024import java.io.IOException; 025import java.io.InputStream; 026import java.io.InputStreamReader; 027import java.io.Reader; 028import java.nio.charset.Charset; 029 030/** 031 * A resource on the filesystem. 032 */ 033public class FileSystemResource implements Resource, Comparable<FileSystemResource> { 034 /** 035 * The location of the resource on the filesystem. 036 */ 037 private File location; 038 039 /** 040 * Creates a new ClassPathResource. 041 * 042 * @param location The location of the resource on the filesystem. 043 */ 044 public FileSystemResource(String location) { 045 this.location = new File(location); 046 } 047 048 public String toString() { 049 return location.toString(); 050 } 051 052 /** 053 * @return The location of the resource on the classpath. 054 */ 055 public String getLocation() { 056 return location.getPath().replace('\\','/'); 057 } 058 059 /** 060 * Retrieves the location of this resource on disk. 061 * 062 * @return The location of this resource on disk. 063 */ 064 public String getLocationOnDisk() { 065 return location.getAbsolutePath(); 066 } 067 068 /** 069 * Loads this resource as a string. 070 * 071 * @param encoding The encoding to use. 072 * @return The string contents of the resource. 073 */ 074 public String loadAsString(String encoding) { 075 try { 076 InputStream inputStream = new FileInputStream(location); 077 Reader reader = new InputStreamReader(inputStream, Charset.forName(encoding)); 078 079 return FileCopyUtils.copyToString(reader); 080 } catch (IOException e) { 081 throw new ClassPathScanException("Unable to load filesystem resource: " + location.getPath() + " (encoding: " + encoding + ")", e); 082 } 083 } 084 085 /** 086 * Loads this resource as a byte array. 087 * 088 * @return The contents of the resource. 089 */ 090 public byte[] loadAsBytes() { 091 try { 092 InputStream inputStream = new FileInputStream(location); 093 return FileCopyUtils.copyToByteArray(inputStream); 094 } catch (IOException e) { 095 throw new ClassPathScanException("Unable to load filesystem resource: " + location.getPath(), e); 096 } 097 } 098 099 /** 100 * @return The filename of this resource, without the path. 101 */ 102 public String getFilename() { 103 return location.getName(); 104 } 105 106 @SuppressWarnings("NullableProblems") 107 public int compareTo(FileSystemResource o) { 108 return location.compareTo(o.location); 109 } 110}