package net.sourceforge.javautil.common.io.impl;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import javax.swing.Icon;
import net.sourceforge.javautil.common.FileUtil;
import net.sourceforge.javautil.common.IOUtil;
import net.sourceforge.javautil.common.exception.ThrowableManagerRegistry;
import net.sourceforge.javautil.common.io.VirtualArtifactAbstract;
import net.sourceforge.javautil.common.io.VirtualArtifactException;
import net.sourceforge.javautil.common.io.IVirtualDirectory;
import net.sourceforge.javautil.common.io.IVirtualFile;
import net.sourceforge.javautil.common.io.VirtualFileAbstract;
import net.sourceforge.javautil.common.xml.annotation.XmlTag;
import net.sourceforge.javautil.common.xml.annotation.XmlTag.ElementType;
/**
* An implementation for {@link File}'s that are regular files (non-directories).
*
* @author elponderador
* @author $Author: ponderator $
* @version $Id: SystemFile.java 2706 2010-12-31 00:09:22Z ponderator $
*/
@XmlTag(elementType=ElementType.Simple)
public class SystemFile extends VirtualFileAbstract implements IVirtualFile, ISystemArtifact<IVirtualFile> {
protected File file;
public SystemFile(String path) { this(new File(path)); }
public SystemFile(File file) {
super(file.getName(), null);
this.file = file;
}
public SystemFile(SystemDirectory parent, File file) {
this(file);
this.owner = parent;
}
public void rename(String newName) {
File newFile = new File(file.getParent(), newName);
if (!this.file.renameTo(newFile))
throw new IllegalStateException("Could not rename " + this.name + " to " + newName);
this.file = newFile;
this.name = newName;
this.clearCache();
}
public String getDisplayName() {
return file.exists() ? SystemDirectory.getFileSystemView().getSystemDisplayName(file) : file.getName();
}
public Icon getSystemIcon() {
return SystemDirectory.getFileSystemView().getSystemIcon(file);
}
public String getTypeDescription() {
return SystemDirectory.getFileSystemView().getSystemTypeDescription(file);
}
public boolean isDrive() { return false; }
public long getSize() { return file.length(); }
public URL getURL() {
try {
return file.toURI().toURL();
} catch (MalformedURLException e) {
throw ThrowableManagerRegistry.caught(new VirtualArtifactException(this, e));
}
}
public long getLastModified() { return file.lastModified(); }
public File getRealArtifact() { return this.file; }
public boolean isDirectory() { return false; }
public boolean isFile() { return true; }
public InputStream getRawInputStream() throws IOException {
return new FileInputStream(file);
}
public OutputStream getRawOutputStream() throws IOException {
return new FileOutputStream(file);
}
public boolean isReadOnly() { return !file.canWrite(); }
public SystemDirectory getOwner() {
if (owner == null) {
if (file.getParent() != null)
this.owner = new SystemDirectory(file.getParentFile());
else
this.owner = new SystemDirectory(".");
}
return (SystemDirectory) owner;
}
public boolean remove() { return this.file.delete(); }
public boolean isExists() { return file.exists(); }
}