001/* 002 * #%L 003 * Netarchivesuite - common 004 * %% 005 * Copyright (C) 2005 - 2014 The Royal Danish Library, the Danish State and University Library, 006 * the National Library of France and the Austrian National Library. 007 * %% 008 * This program is free software: you can redistribute it and/or modify 009 * it under the terms of the GNU Lesser General Public License as 010 * published by the Free Software Foundation, either version 2.1 of the 011 * License, or (at your option) any later version. 012 * 013 * This program is distributed in the hope that it will be useful, 014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 016 * GNU General Lesser Public License for more details. 017 * 018 * You should have received a copy of the GNU General Lesser Public 019 * License along with this program. If not, see 020 * <http://www.gnu.org/licenses/lgpl-2.1.html>. 021 * #L% 022 */ 023 024package dk.netarkivet.common.utils; 025 026import java.io.BufferedReader; 027import java.io.File; 028import java.io.FileReader; 029import java.io.IOException; 030 031import org.slf4j.Logger; 032import org.slf4j.LoggerFactory; 033 034import dk.netarkivet.common.exceptions.ArgumentNotValid; 035 036/** 037 * File Free Space Provider returns the number of bytes free out of a file. 038 */ 039 040public class FilebasedFreeSpaceProvider implements FreeSpaceProvider { 041 042 /** The error logger we notify about error messages on. */ 043 private static final Logger log = LoggerFactory.getLogger(FilebasedFreeSpaceProvider.class); 044 045 /** The default place in classpath where the settings file can be found. */ 046 private static String DEFAULT_SETTINGS_CLASSPATH = "dk/netarkivet/common/utils/FilebasedFreeSpaceProvider.xml"; 047 048 /* 049 * The static initialiser is called when the class is loaded. It will add default values for all settings defined in 050 * this class, by loading them from a settings.xml file in classpath. 051 */ 052 static { 053 Settings.addDefaultClasspathSettings(DEFAULT_SETTINGS_CLASSPATH); 054 } 055 056 /** 057 * <b>settings.common.freespaceprovider.file</b>: <br> 058 * The setting for filename of the free space information. 059 */ 060 public static final String FREESPACEPROVIDER_DIR_SETTING = "settings.common.freespaceprovider.dir"; 061 062 /** The filename for reading out the free space infomation. */ 063 private static final String FREESPACEPROVIDER_DIR = Settings.get(FREESPACEPROVIDER_DIR_SETTING); 064 065 /** 066 * Returns the number of bytes free which is read out of a file containing the bytes free information. This file is 067 * located in the FREESPACEPROVIDER_DIR and has the name as parameter f. Will return 0 on any IO- or 068 * Format-Exceptions. 069 * 070 * @param f a given file 071 * @return the number of bytes free. 072 */ 073 public long getBytesFree(File f) { 074 ArgumentNotValid.checkNotNull(f, "File f"); 075 076 BufferedReader reader = null; 077 String content; 078 long bytes = 0; 079 File bytesFreeFile = new File(FREESPACEPROVIDER_DIR, f.getName()); 080 081 try { 082 reader = new BufferedReader(new FileReader(bytesFreeFile)); 083 content = reader.readLine(); // only read first line 084 bytes = Long.parseLong(content); 085 } catch (Exception e) { 086 log.warn("Exception while reading {}. The value 0 returned.", bytesFreeFile.getAbsolutePath()); 087 return 0; 088 } finally { 089 if (reader != null) { 090 try { 091 reader.close(); 092 } catch (IOException e) { 093 log.warn("Unable to close FileReader"); 094 } 095 } 096 } 097 098 return bytes; 099 } 100 101}