All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sourceforge.tink.model.FileObject Maven / Gradle / Ivy

/**
 * Copyright 2008,2009 Ivan SZKIBA
 *
 * 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.
 * under the License.
 */
package net.sourceforge.tink.model;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;

import java.net.URI;

import java.nio.charset.Charset;

public class FileObject extends File
{
    public static final Charset CHARSET = Charset.forName("UTF-8");
    public static final char PATH_SEPARATOR = '/';
    private final String relativePath;
    private final File topFile;

    public FileObject(String pathname) throws TinkException
    {
        super(pathname);
        try
        {
            topFile = this.getCanonicalFile().getParentFile();
        }
        catch (IOException x)
        {
            throw new TinkException(x);
        }

        relativePath = makeRelativePath();
    }

    public FileObject(File top, String child) throws TinkException
    {
        super(top, child);
        topFile = top;
        relativePath = makeRelativePath();
    }

    public FileObject(File top, FileObject base, URI reference) throws TinkException
    {
        super(((reference.getPath().charAt(0) == PATH_SEPARATOR) ? top.getAbsolutePath() : (base.getParentFile().getAbsolutePath() + PATH_SEPARATOR)) + reference.getPath());
        topFile = top;
        relativePath = makeRelativePath();
    }

    public String getRelativePath()
    {
        return relativePath;
    }

    public File getTopFile()
    {
        return topFile;
    }

    public Reader openReader() throws IOException
    {
        FileInputStream stream = new FileInputStream(this);

        return new InputStreamReader(stream, CHARSET);
    }

    public Writer openWriter() throws IOException
    {
        FileOutputStream stream = new FileOutputStream(this);

        return new OutputStreamWriter(stream, CHARSET);
    }

    protected String makeRelativePath() throws TinkException
    {
        try
        {
            String path = getCanonicalPath();
            String dir = topFile.getCanonicalPath();

            if (!path.startsWith(dir))
            {
                throw new TinkException("File " + path + " not under top directory " + dir);
            }

            return path.substring(dir.length());
        }
        catch (IOException x)
        {
            throw new TinkException(x);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy