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.File; 027 028import org.slf4j.Logger; 029import org.slf4j.LoggerFactory; 030 031import dk.netarkivet.common.exceptions.ArgumentNotValid; 032 033/** 034 * Default Free Space Provider of the number of bytes free on the file system. 035 */ 036public class DefaultFreeSpaceProvider implements FreeSpaceProvider { 037 038 /** The error logger we notify about error messages on. */ 039 private static final Logger log = LoggerFactory.getLogger(DefaultFreeSpaceProvider.class); 040 041 /** 042 * Returns the number of bytes free on the file system that the given file resides on. Will return 0 on non-existing 043 * files. 044 * 045 * @param f a given file 046 * @return the number of bytes free on the file system where file f resides. 0 if the file cannot be found. 047 */ 048 public long getBytesFree(File f) { 049 ArgumentNotValid.checkNotNull(f, "File f"); 050 if (!f.exists()) { 051 log.warn("The file '{}' does not exist. The value 0 returned.", f.getAbsolutePath()); 052 return 0; 053 } 054 return f.getUsableSpace(); 055 } 056 057}