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 libcore.io; 18 19 import android.system.ErrnoException; 20 import android.system.Os; 21 22 import com.android.ravenwood.common.JvmWorkaround; 23 24 import java.io.File; 25 import java.io.FileDescriptor; 26 import java.io.IOException; 27 import java.net.Socket; 28 29 /** @hide */ 30 public final class IoUtils { IoUtils()31 private IoUtils() { 32 } 33 closeQuietly(AutoCloseable closeable)34 public static void closeQuietly(AutoCloseable closeable) { 35 if (closeable != null) { 36 try { 37 closeable.close(); 38 } catch (RuntimeException rethrown) { 39 throw rethrown; 40 } catch (Exception ignored) { 41 } 42 } 43 } 44 closeQuietly(Socket socket)45 public static void closeQuietly(Socket socket) { 46 if (socket != null) { 47 try { 48 socket.close(); 49 } catch (RuntimeException rethrown) { 50 throw rethrown; 51 } catch (Exception ignored) { 52 } 53 } 54 } 55 closeQuietly(FileDescriptor fd)56 public static void closeQuietly(FileDescriptor fd) { 57 try { 58 Os.close(fd); 59 } catch (ErrnoException ignored) { 60 } 61 } 62 deleteContents(File dir)63 public static void deleteContents(File dir) throws IOException { 64 File[] files = dir.listFiles(); 65 if (files != null) { 66 for (File file : files) { 67 if (file.isDirectory()) { 68 deleteContents(file); 69 } 70 file.delete(); 71 } 72 } 73 } 74 75 /** 76 * FD owners currently unsupported under Ravenwood; ignored 77 */ setFdOwner(FileDescriptor fd, Object owner)78 public static void setFdOwner(FileDescriptor fd, Object owner) { 79 } 80 81 /** 82 * FD owners currently unsupported under Ravenwood; return FD directly 83 */ acquireRawFd(FileDescriptor fd)84 public static int acquireRawFd(FileDescriptor fd) { 85 return JvmWorkaround.getInstance().getFdInt(fd); 86 } 87 } 88