/* * Copyright 2014 Skynav, Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY SKYNAV, INC. AND ITS CONTRIBUTORS “AS IS” AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL SKYNAV, INC. OR ITS CONTRIBUTORS BE LIABLE FOR * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package com.skynav.ttv.util; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.Reader; public class IOUtil { public static void closeSafely(InputStream is) { try { if (is != null) is.close(); } catch (Throwable e) { } } public static void closeSafely(OutputStream os) { try { if (os != null) os.close(); } catch (Throwable e) { } } public static void closeSafely(Reader r) { try { if (r != null) r.close(); } catch (Throwable e) { } } public static boolean deleteSafely(File file) { boolean deleted = false; try { if (file != null) deleted = file.delete(); } catch (Throwable e) { } return deleted; } public static void copy(InputStream is, OutputStream os) throws IOException { byte[] buf = new byte[4096]; int nb; while ((nb = is.read(buf)) >= 0) { if (nb > 0) os.write(buf, 0, nb); else Thread.yield(); } } public static int readCompletely(InputStream is, byte[] buf) throws IOException { int len = buf.length; int rem = len; int nb; while (rem > 0) { nb = is.read(buf, len - rem, rem); if (nb < 0) break; else if (nb > 0) rem -= nb; else Thread.yield(); } return len - rem; } public static String getDirectoryPath(File f) { try { File d = getDirectory(f); return (d != null) ? d.getCanonicalPath() : null; } catch (IOException e) { return null; } } public static File getDirectory(File f) { if (f.isDirectory()) return f; else { try { String p = f.getCanonicalPath(); return new File(p.substring(0, p.lastIndexOf(File.separatorChar))); } catch (IOException e) { } } return null; } }