001/* 002 GRANITE DATA SERVICES 003 Copyright (C) 2011 GRANITE DATA SERVICES S.A.S. 004 005 This file is part of Granite Data Services. 006 007 Granite Data Services is free software; you can redistribute it and/or modify 008 it under the terms of the GNU Library General Public License as published by 009 the Free Software Foundation; either version 2 of the License, or (at your 010 option) any later version. 011 012 Granite Data Services is distributed in the hope that it will be useful, but 013 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 014 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License 015 for more details. 016 017 You should have received a copy of the GNU Library General Public License 018 along with this library; if not, see <http://www.gnu.org/licenses/>. 019*/ 020 021package org.granite.scan; 022 023import java.io.File; 024import java.io.FileInputStream; 025import java.io.IOException; 026import java.io.InputStream; 027 028/** 029 * @author Franck WOLFF 030 */ 031public class FileScannedItem extends AbstractScannedItem { 032 033 private final File root; 034 private final File file; 035 036 private String relativePath = null; 037 038 public FileScannedItem(Scanner scanner, FileScannedItem marker, File root, File file) { 039 super(scanner, marker); 040 041 this.root = root; 042 this.file = file; 043 } 044 045 public File getRoot() { 046 return root; 047 } 048 049 public File getFile() { 050 return file; 051 } 052 053 public long getSize() { 054 return file.length(); 055 } 056 057 public InputStream getInputStream() throws IOException { 058 return new FileInputStream(file); 059 } 060 061 public String getName() { 062 return file.getName(); 063 } 064 065 public String getRelativePath() { 066 if (relativePath == null) { 067 StringBuffer sb = new StringBuffer(); 068 for (File f = file; f != null && !root.equals(f); f = f.getParentFile()) { 069 if (sb.length() > 0) 070 sb.insert(0, '/'); 071 sb.insert(0, f.getName()); 072 } 073 relativePath = sb.toString(); 074 } 075 return relativePath; 076 } 077 078 public String getAbsolutePath() { 079 return file.getAbsolutePath(); 080 } 081 082 @Override 083 public boolean equals(Object obj) { 084 if (obj == this) 085 return true; 086 if (!(obj instanceof FileScannedItem)) 087 return false; 088 return file.equals(((FileScannedItem)obj).file) && root.equals(((FileScannedItem)obj).root); 089 } 090 091 @Override 092 public int hashCode() { 093 return root.hashCode() + (31 * file.hashCode()); 094 } 095}