/* Copyright (C) 2003-2008 The University of Iowa * * This file is part of the Das2 utilities library. * * Das2 utilities are free software: you can redistribute and/or modify them * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License, or (at your * option) any later version. * * Das2 utilities are distributed in the hope that they 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 * as well as the GNU General Public License along with Das2 utilities. If * not, see . * * LocalFileObject.java * * Created on May 25, 2004, 6:01 PM */ package org.das2.util.filesystem; import org.das2.util.monitor.ProgressMonitor; import java.io.*; import java.util.zip.GZIPInputStream; import org.das2.util.monitor.NullProgressMonitor; /** * Provide access to local files. * @author Jeremy */ public class LocalFileObject extends FileObject { File localFile; File localGzFile; File localRoot; LocalFileSystem lfs; protected LocalFileObject( LocalFileSystem lfs, File localRoot, String filename ) { this.lfs= lfs; this.localFile= new File( localRoot, filename ); this.localGzFile= new File( localRoot, filename+".gz" ); this.localRoot= localRoot; } @Override public boolean canRead() { return localFile.canRead(); } @Override public FileObject[] getChildren() { File[] files= localFile.listFiles(); if ( files==null ) throw new NullPointerException("listFiles returns null: "+localFile); LocalFileObject[] result= new LocalFileObject[files.length]; for ( int i=0; ilocalGzFile.lastModified() ) { return tempFile; } else { synchronized ( LocalFileObject.class ) { if ( !tempFile.getParentFile().exists() && !tempFile.getParentFile().mkdirs() ) { throw new FileNotFoundException("unable to create parent directories: "+tempFile ); } } FileSystemUtil.gunzip( localGzFile, tempFile ); tempFile.deleteOnExit(); //TODO: verify this on all platforms. return tempFile; } } catch (FileNotFoundException ex ) { throw ex; //cheesy } catch (IOException ex) { throw new FileNotFoundException("unable to gunzip: "+localGzFile+", "+ex.toString() ); } } else { throw new FileNotFoundException("file not found: "+localFile); } } return localFile; } @Override public boolean isLocal() { return true; } @Override public T getCapability(Class clazz) { if ( clazz==WriteCapability.class ) { return (T) new WriteCapability() { @Override public OutputStream getOutputStream() throws FileNotFoundException { return new FileOutputStream(localFile); } @Override public boolean canWrite() { return localFile.canWrite() || localFile.getParentFile().canWrite(); } @Override public boolean delete() { return localFile.delete(); } @Override public boolean commit(String message) throws IOException { //do nothing //TODO: check to see if this is a local git repo. return true; } }; } else { return super.getCapability(clazz); } } }