org.openflamingo.uploader.util.FileUtils.java Source code

Java tutorial

Introduction

Here is the source code for org.openflamingo.uploader.util.FileUtils.java

Source

/**
 * Flamingo HDFS File Uploader - a tool to upload from datasource to datasource and schedule jobs
 *
 * Copyright (C) 2011-2012 Cloudine.
 *
 * This file is part of Flamingo HDFS File Uploader.
 *
 * Flamingo HDFS File Uploader is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Flamingo HDFS File Uploader is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.openflamingo.uploader.util;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.SystemUtils;
import org.openflamingo.uploader.exception.SystemException;

import java.io.File;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * File Utility.
 *
 * @author Edward KIM
 * @since 0.1
 */
public class FileUtils {

    public static long KB = 1024L;
    public static long MB = KB * KB;
    public static long GB = MB * KB;
    public static long TB = GB * KB;
    public static long PB = TB * KB;
    public static long ZB = PB * KB;

    /**
     *  ??  ? ? .
     *
     * @param filename ?
     * @return ? . ? ??    ? ? 0L? .
     */
    public static long lastModified(final String filename) {
        return new File(filename).lastModified();
    }

    /**
     *  ? ? . ) "mypath/myfile.txt" -> "myfile.txt".
     *
     * @param path ? (<tt>null</tt>? ? ? ?)
     * @return ? ? ? ??   <tt>null</tt>
     */
    public static String getFilename(String path) {
        return org.springframework.util.StringUtils.getFilename(path);
    }

    /**
     *  ? ?? ? . ) "mypath/myfile.txt" -> "txt".
     *
     * @param path ? (<tt>null</tt>? ? ? ?)
     * @return the extracted filename extension, or <tt>null</tt> if none
     */
    public static String getFilenameExtension(String path) {
        return org.springframework.util.StringUtils.getFilenameExtension(path);
    }

    /**
     *  ? ?? ?   . ) "mypath/myfile.txt" -> "mypath/myfile".
     *
     * @param path ? (<tt>null</tt>? ? ? ?)
     * @return ? ? ??  ? ??  <tt>null</tt>
     */
    public static String stripFilenameExtension(String path) {
        return org.springframework.util.StringUtils.stripFilenameExtension(path);
    }

    /**
     * Fully Qualified Path?  Path Separator    .
     *
     * @param fullyQualifiedPath Fully Qualified Path
     * @return  Path Separator   
     */
    public static String getPath(String fullyQualifiedPath) {
        int sep = fullyQualifiedPath.lastIndexOf(SystemUtils.FILE_SEPARATOR);
        if (sep != 0) {
            return fullyQualifiedPath.substring(0, sep);
        }
        return SystemUtils.FILE_SEPARATOR;
    }

    /**
     * Fully Qualified Path?  Path Separator   ? ?  ? ?? ?.
     *
     * @param fullyQualifiedPath Fully Qualified Path
     * @param replacement        ? ? ? 
     * @return ? ? ?  ?? fully qualified path
     */
    public static String replaceLast(String fullyQualifiedPath, String replacement) {
        String path = getPath(fullyQualifiedPath);
        if (path.endsWith(SystemUtils.FILE_SEPARATOR)) {
            return path + replacement;
        }
        return path + SystemUtils.FILE_SEPARATOR + replacement;
    }

    /**
     *  URL?  Input Stream? .
     *
     * @param url URL
     * @return Input Stream
     */
    public static InputStream openStream(String url) {
        String message = ExceptionUtils
                .getMessage("URL '{}' ?  Input Stream? ?  .", url);
        try {
            return new URL(url).openStream();
        } catch (MalformedURLException ex) {
            if (ex.getMessage().contains("no protocol") && url.startsWith("/")) {
                try {
                    return new URL("file:" + url).openStream();
                } catch (Exception e) {
                    throw new SystemException(message, e);
                }
            }
            throw new SystemException(message, ex);
        } catch (Exception ex) {
            throw new SystemException(message, ex);
        }
    }

    /**
     *  URL?    ?.
     *
     * @param url URL
     * @return   <tt>true</tt>
     */
    public static boolean isExist(String url) {
        String message = ExceptionUtils
                .getMessage("URL '{}' ?  Input Stream? ?  .", url);
        InputStream inputStream = null;
        try {
            inputStream = new URL(url).openStream();
            return true;
        } catch (MalformedURLException ex) {
            if (ex.getMessage().contains("no protocol") && url.startsWith("/")) {
                try {
                    inputStream = new URL("file:" + url).openStream();
                    return true;
                } catch (Exception e) {
                    return false;
                }
            }
            return false;
        } catch (Exception ex) {
            return false;
        } finally {
            IOUtils.closeQuietly(inputStream);
        }
    }

}