com.github.mgramin.sqlboot.tools.files.file_system.impl.LocalFileSystem.java Source code

Java tutorial

Introduction

Here is the source code for com.github.mgramin.sqlboot.tools.files.file_system.impl.LocalFileSystem.java

Source

/**
 * The MIT License (MIT)
 *
 * Copyright (c) 2016-2017 Maksim Gramin
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.github.mgramin.sqlboot.tools.files.file_system.impl;

import com.github.mgramin.sqlboot.exceptions.BootException;
import com.github.mgramin.sqlboot.tools.files.file.File;
import com.github.mgramin.sqlboot.tools.files.file.impl.SimpleFile;
import com.github.mgramin.sqlboot.tools.files.file_system.FileSystem;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.io.FileUtils;
import org.apache.tools.ant.DirectoryScanner;

/**
 * @author Maksim Gramin (mgramin@gmail.com)
 * @version $Id: 7a9d1817116768c069ceead3f240104b3bc1d764 $
 * @since 0.1
 */
public final class LocalFileSystem implements FileSystem {

    private final String basedir;

    public LocalFileSystem(final String basedir) {
        this.basedir = basedir;
    }

    @Override
    public List<File> listFiles(final String mask) {
        try {
            DirectoryScanner scanner = new DirectoryScanner();
            scanner.setIncludes(new String[] { mask });
            scanner.setBasedir(this.basedir.replace("\\", "/"));
            scanner.setCaseSensitive(false);
            scanner.scan();
            String[] files = scanner.getIncludedFiles();
            List<File> result = new ArrayList<>();
            for (String file : files) {
                result.add(new SimpleFile(file,
                        FileUtils.readFileToByteArray(new java.io.File(basedir + "/" + file))));
            }
            return result;
        } catch (IOException exception) {
            throw new BootException(exception);
        }
    }

}