/*
* transformica 2
* Code generator
* Copyright (C) 2004 Hammurapi Group
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* URL: http://www.pavelvlasov.com/pv/content/menu.show@id=products.transformica.html
* e-Mail: support@hammurapi.biz
*/
package biz.hammurapi.transformica;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.StringTokenizer;
import java.util.zip.Adler32;
/**
* @author Pavel Vlasov
* @version $Revision: 1.1 $
*/
public class FileInfo {
public static final char FIELD_SEPARATOR='\t';
private String name;
private long size;
private long checkSum;
private long lastModified;
/**
* Parses line using default field separator
* @param line String to parse
*/
public FileInfo(String line) throws ParseException {
this(line, FIELD_SEPARATOR);
}
/**
* Parses line
* @param line String to parse
* @param separator Field separator
*/
public FileInfo(String line, char separator) throws ParseException {
StringTokenizer st=new StringTokenizer(line, String.valueOf(separator));
lastModified=sdf.parse(st.nextToken().trim()).getTime();
size=Long.parseLong(st.nextToken().trim());
checkSum=Long.parseLong(st.nextToken().trim());
name=st.nextToken();
}
/**
*
*/
public FileInfo(String name, long size, long checkSum, long lastModified) {
this.name=name;
this.size=size;
this.checkSum=checkSum;
this.lastModified=lastModified;
}
/**
*
*/
public FileInfo(File genRoot, File file) throws TransformicaException {
if (genRoot==null) {
name=file.getAbsolutePath().replace(File.separatorChar, '/');
} else if (file.getAbsolutePath().startsWith(genRoot.getAbsolutePath())) {
name=file.getAbsolutePath().substring(genRoot.getAbsolutePath().length()+1).replace(File.separatorChar, '/');
} else {
throw new TransformicaException("File "+file.getAbsolutePath()+" must be under 'genRoot' "+genRoot.getAbsolutePath());
}
if (file.exists() && file.isFile()) {
lastModified=file.lastModified();
size=file.length();
try {
InputStream is=new FileInputStream(file);
byte[] buf=new byte[4096];
long l=0;
Adler32 adler32=new Adler32();
int len;
while ((len=is.read(buf))!=-1) {
l+=len;
adler32.update(buf, 0, len);
}
is.close();
if (l!=size) {
throw new TransformicaException("Number of bytes read differs from file size");
}
checkSum=adler32.getValue();
} catch (IOException e) {
throw new TransformicaException("Can't calculate checksum", e);
}
} else {
}
}
/**
* @return
*/
public long getCheckSum() {
return checkSum;
}
/**
* @return
*/
public String getName() {
return name;
}
/**
* @return
*/
public long getSize() {
return size;
}
boolean isModified(FileInfo anotherFileInfo, boolean ignoreTimeStamp) {
if (anotherFileInfo.size!=size) {
return true;
}
if (anotherFileInfo.checkSum!=checkSum) {
return true;
}
if (anotherFileInfo.lastModified!=lastModified && !ignoreTimeStamp) {
return true;
}
return false;
}
public boolean exists(File genRoot) {
return genRoot==null ? new File(name).exists() : new File(genRoot, name).exists();
}
/**
* @return
*/
public long getLastModified() {
return lastModified;
}
private SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.S");
/**
* Converts FileInfo to String with default field separator
*/
public String toString() {
return toString(FIELD_SEPARATOR);
}
/**
* Converts FileInfo to String
* @param separator Field Separator
* @return String representation of FileInfo
*/
public String toString(char separator) {
return sdf.format(new Date(lastModified))+separator+size+separator+checkSum+separator+name;
}
public static final String HEADER="# Date \tSize \tChecksum \tName";
}
|