1c3d793fa781ad98e4df1c6ddd3a2bdeedafcda12Hugo Hudson/* 2c3d793fa781ad98e4df1c6ddd3a2bdeedafcda12Hugo Hudson * Copyright (C) 2011 The Android Open Source Project 3c3d793fa781ad98e4df1c6ddd3a2bdeedafcda12Hugo Hudson * 4c3d793fa781ad98e4df1c6ddd3a2bdeedafcda12Hugo Hudson * Licensed under the Apache License, Version 2.0 (the "License"); 5c3d793fa781ad98e4df1c6ddd3a2bdeedafcda12Hugo Hudson * you may not use this file except in compliance with the License. 6c3d793fa781ad98e4df1c6ddd3a2bdeedafcda12Hugo Hudson * You may obtain a copy of the License at 7c3d793fa781ad98e4df1c6ddd3a2bdeedafcda12Hugo Hudson * 8c3d793fa781ad98e4df1c6ddd3a2bdeedafcda12Hugo Hudson * http://www.apache.org/licenses/LICENSE-2.0 9c3d793fa781ad98e4df1c6ddd3a2bdeedafcda12Hugo Hudson * 10c3d793fa781ad98e4df1c6ddd3a2bdeedafcda12Hugo Hudson * Unless required by applicable law or agreed to in writing, software 11c3d793fa781ad98e4df1c6ddd3a2bdeedafcda12Hugo Hudson * distributed under the License is distributed on an "AS IS" BASIS, 12c3d793fa781ad98e4df1c6ddd3a2bdeedafcda12Hugo Hudson * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13c3d793fa781ad98e4df1c6ddd3a2bdeedafcda12Hugo Hudson * See the License for the specific language governing permissions and 14c3d793fa781ad98e4df1c6ddd3a2bdeedafcda12Hugo Hudson * limitations under the License. 15c3d793fa781ad98e4df1c6ddd3a2bdeedafcda12Hugo Hudson */ 16c3d793fa781ad98e4df1c6ddd3a2bdeedafcda12Hugo Hudson 17c3d793fa781ad98e4df1c6ddd3a2bdeedafcda12Hugo Hudsonpackage com.android.common.io; 18c3d793fa781ad98e4df1c6ddd3a2bdeedafcda12Hugo Hudson 19c3d793fa781ad98e4df1c6ddd3a2bdeedafcda12Hugo Hudsonimport android.content.res.AssetFileDescriptor; 20c3d793fa781ad98e4df1c6ddd3a2bdeedafcda12Hugo Hudsonimport android.database.Cursor; 21c3d793fa781ad98e4df1c6ddd3a2bdeedafcda12Hugo Hudson 22c3d793fa781ad98e4df1c6ddd3a2bdeedafcda12Hugo Hudsonimport java.io.IOException; 23c3d793fa781ad98e4df1c6ddd3a2bdeedafcda12Hugo Hudson 24c3d793fa781ad98e4df1c6ddd3a2bdeedafcda12Hugo Hudsonpublic class MoreCloseables { 25c3d793fa781ad98e4df1c6ddd3a2bdeedafcda12Hugo Hudson /** 26c3d793fa781ad98e4df1c6ddd3a2bdeedafcda12Hugo Hudson * Closes the supplied cursor iff it is not null. 27c3d793fa781ad98e4df1c6ddd3a2bdeedafcda12Hugo Hudson */ 28c3d793fa781ad98e4df1c6ddd3a2bdeedafcda12Hugo Hudson public static void closeQuietly(Cursor cursor) { 29c3d793fa781ad98e4df1c6ddd3a2bdeedafcda12Hugo Hudson if (cursor != null) { 30c3d793fa781ad98e4df1c6ddd3a2bdeedafcda12Hugo Hudson cursor.close(); 31c3d793fa781ad98e4df1c6ddd3a2bdeedafcda12Hugo Hudson } 32c3d793fa781ad98e4df1c6ddd3a2bdeedafcda12Hugo Hudson } 33c3d793fa781ad98e4df1c6ddd3a2bdeedafcda12Hugo Hudson 34c3d793fa781ad98e4df1c6ddd3a2bdeedafcda12Hugo Hudson /** 35c3d793fa781ad98e4df1c6ddd3a2bdeedafcda12Hugo Hudson * Closes the given file descriptor iff it is not null, ignoring exceptions. 36c3d793fa781ad98e4df1c6ddd3a2bdeedafcda12Hugo Hudson */ 37c3d793fa781ad98e4df1c6ddd3a2bdeedafcda12Hugo Hudson public static void closeQuietly(AssetFileDescriptor assetFileDescriptor) { 38c3d793fa781ad98e4df1c6ddd3a2bdeedafcda12Hugo Hudson if (assetFileDescriptor != null) { 39c3d793fa781ad98e4df1c6ddd3a2bdeedafcda12Hugo Hudson try { 40c3d793fa781ad98e4df1c6ddd3a2bdeedafcda12Hugo Hudson assetFileDescriptor.close(); 41c3d793fa781ad98e4df1c6ddd3a2bdeedafcda12Hugo Hudson } catch (IOException e) { 42c3d793fa781ad98e4df1c6ddd3a2bdeedafcda12Hugo Hudson // Ignore exceptions when closing. 43c3d793fa781ad98e4df1c6ddd3a2bdeedafcda12Hugo Hudson } 44c3d793fa781ad98e4df1c6ddd3a2bdeedafcda12Hugo Hudson } 45c3d793fa781ad98e4df1c6ddd3a2bdeedafcda12Hugo Hudson } 46c3d793fa781ad98e4df1c6ddd3a2bdeedafcda12Hugo Hudson} 47