Skip to content
Snippets Groups Projects
FileBasedContentNode.java 3.7 KiB
Newer Older
  • Learn to ignore specific revisions
  • /*
     * Copyright 2010 ETH Zuerich, CISD
     *
     * Licensed under the Apache License, Version 2.0 (the "License");
     * you may not use this file except in compliance with the License.
     * You may obtain a copy of the License at
     *
     *      http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     */
    
    package ch.systemsx.cisd.common.io;
    
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    
    gpawel's avatar
    gpawel committed
    import java.util.List;
    
    import org.apache.commons.io.FileUtils;
    
    
    import ch.systemsx.cisd.base.exceptions.CheckedExceptionTunnel;
    
    gpawel's avatar
    gpawel committed
    import ch.systemsx.cisd.base.exceptions.IOExceptionUnchecked;
    
    brinn's avatar
    brinn committed
    import ch.systemsx.cisd.base.io.IRandomAccessFile;
    import ch.systemsx.cisd.base.io.RandomAccessFileImpl;
    
    gpawel's avatar
    gpawel committed
    import ch.systemsx.cisd.common.io.hierarchical_content.api.IHierarchicalContentNode;
    
    
    /**
     * File content. Wraps an instance of {@link File}.
    
    tpylak's avatar
    tpylak committed
     * 
    
    gpawel's avatar
    gpawel committed
    public class FileBasedContentNode implements IHierarchicalContentNode
    
    {
        private final File file;
    
        /**
         * Creates an instance based on the specified file.
         */
    
    gpawel's avatar
    gpawel committed
        public FileBasedContentNode(File file)
    
        {
            this.file = file;
        }
    
        /**
         * Returns the name of the wrapped file.
         */
    
        public String tryGetName()
    
        {
            return file.getName();
        }
    
        /**
         * Returns <code>true</code> if the wrapped file exists.
         */
    
        public boolean exists()
        {
            return file.exists();
        }
    
        /**
         * Returns a new instance of {@link FileInputStream} for the wrapped file.
         */
    
        public InputStream getInputStream()
        {
            try
            {
    
                return new BufferedInputStream(new FileInputStream(file));
    
            } catch (FileNotFoundException ex)
            {
                throw CheckedExceptionTunnel.wrapIfNecessary(ex);
            }
        }
    
    gpawel's avatar
    gpawel committed
        public String getName()
        {
            return file.getName();
        }
    
    
    gpawel's avatar
    gpawel committed
        public String getRelativePath()
        {
            return file.getPath();
        }
    
    
    gpawel's avatar
    gpawel committed
        public String getParentRelativePath()
        {
            return file.getParent();
        }
    
    
    gpawel's avatar
    gpawel committed
        public boolean isDirectory()
        {
            return file.isDirectory();
        }
    
    
    gpawel's avatar
    gpawel committed
        public long getLastModified()
        {
            return file.lastModified();
        }
    
    
    gpawel's avatar
    gpawel committed
        public List<IHierarchicalContentNode> getChildNodes() throws UnsupportedOperationException
        {
            throw new UnsupportedOperationException();
        }
    
    
    gpawel's avatar
    gpawel committed
        public File getFile() throws UnsupportedOperationException
        {
            return file;
        }
    
    
    gpawel's avatar
    gpawel committed
        public long getFileLength() throws UnsupportedOperationException
        {
            return file.length();
        }
    
    
        public int getChecksumCRC32() throws UnsupportedOperationException
    
                return (int) FileUtils.checksumCRC32(file);
    
            } catch (IOException ex)
            {
                throw CheckedExceptionTunnel.wrapIfNecessary(ex);
            }
        }
    
    
        @Override
        public boolean isChecksumCRC32Precalculated()
        {
            return false;
        }
        
    
    gpawel's avatar
    gpawel committed
        public IRandomAccessFile getFileContent() throws UnsupportedOperationException,
                IOExceptionUnchecked
    
    buczekp's avatar
    buczekp committed
            return new RandomAccessFileImpl(file, "r");