1 /* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package vogar.util; 18 19 import java.io.Closeable; 20 import java.io.File; 21 import java.io.IOException; 22 import java.net.Socket; 23 24 public final class IoUtils { 25 safeMkdirs(File path)26 public static void safeMkdirs(File path) { 27 boolean success; 28 if (!path.exists()) { 29 success = path.mkdirs(); 30 } else if (!path.isDirectory()) { 31 success = path.delete() && path.mkdirs(); 32 } else { 33 success = true; 34 } 35 36 if (!success) { 37 throw new RuntimeException("Failed to make directory " + path); 38 } 39 } 40 closeQuietly(Closeable c)41 public static void closeQuietly(Closeable c) { 42 if (c != null) { 43 try { 44 c.close(); 45 } catch (IOException ignored) { 46 } 47 } 48 } 49 closeQuietly(Socket c)50 public static void closeQuietly(Socket c) { 51 if (c != null) { 52 try { 53 c.close(); 54 } catch (IOException ignored) { 55 } 56 } 57 } 58 } 59