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

org.microemu.app.util.IOUtils Maven / Gradle / Ivy

The newest version!
/*
 *  MicroEmulator
 *  Copyright (C) 2002 Bartek Teodorczyk 
 *
 *  It is licensed under the following two licenses as alternatives:
 *    1. GNU Lesser General Public License (the "LGPL") version 2.1 or any newer version
 *    2. Apache License (the "AL") Version 2.0
 *
 *  You may not use this file except in compliance with at least one of
 *  the above two licenses.
 *
 *  You may obtain a copy of the LGPL at
 *      http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
 *
 *  You may obtain a copy of the AL 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 LGPL or the AL for the specific language governing permissions and
 *  limitations.
 */

package org.microemu.app.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Writer;

/**
 * General IO stream manipulation utilities.
 * Some functions are based on org.apache.commons.io
 * 
 * 

* This class provides static utility methods for input/output operations. *

    *
  • closeQuietly - these methods close a stream ignoring nulls and exceptions *
*

*/ public class IOUtils { /** * Solution for JVM bug * http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6351751 */ public static String getCanonicalFileURL(File file) { String path = file.getAbsoluteFile().getPath(); if (File.separatorChar != '/') { path = path.replace(File.separatorChar, '/'); } // Not network path if (!path.startsWith("//")) { if (path.startsWith("/")) { path = "//" + path; } else { path = "///" + path; } } return "file:" + path; } public static String getCanonicalFileClassLoaderURL(File file) { String url = getCanonicalFileURL(file); if ((file.isDirectory()) && (!url.endsWith("/"))) { url += "/"; } return url; } public static void copyFile(File src, File dst) throws IOException { FileInputStream fis = null; try { fis = new FileInputStream(src); copyToFile(fis, dst); } finally { closeQuietly(fis); } } public static void copyToFile(InputStream is, File dst) throws IOException { FileOutputStream fos = null; try { fos = new FileOutputStream(dst); byte[] buf = new byte[1024]; int i = 0; while ((i = is.read(buf)) != -1) { fos.write(buf, 0, i); } } finally { closeQuietly(fos); } } /** * Unconditionally close an InputStream. *

* Equivalent to {@link InputStream#close()}, except any exceptions will be ignored. * This is typically used in finally blocks. * * @param input the InputStream to close, may be null or already closed */ public static void closeQuietly(InputStream input) { try { if (input != null) { input.close(); } } catch (IOException ignore) { // ignore } } /** * Unconditionally close an OutputStream. *

* Equivalent to {@link OutputStream#close()}, except any exceptions will be ignored. * This is typically used in finally blocks. * * @param output the OutputStream to close, may be null or already closed */ public static void closeQuietly(OutputStream output) { try { if (output != null) { output.close(); } } catch (IOException ignore) { // ignore } } /** * Unconditionally close a Writer. *

* Equivalent to {@link Writer#close()}, except any exceptions will be ignored. * This is typically used in finally blocks. * * @param output the Writer to close, may be null or already closed */ public static void closeQuietly(Writer output) { try { if (output != null) { output.close(); } } catch (IOException ioe) { // ignore } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy