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
17package libcore.io;
18
19import java.io.InterruptedIOException;
20import java.net.Socket;
21
22public final class IoUtils {
23    private IoUtils() {}
24
25    /**
26     * Closes 'closeable', ignoring any checked exceptions. Does nothing if 'closeable' is null.
27     */
28    public static void closeQuietly(AutoCloseable closeable) {
29        if (closeable != null) {
30            try {
31                closeable.close();
32            } catch (RuntimeException rethrown) {
33                throw rethrown;
34            } catch (Exception ignored) {
35            }
36        }
37    }
38
39    /**
40     * Closes 'socket', ignoring any exceptions. Does nothing if 'socket' is null.
41     */
42    public static void closeQuietly(Socket socket) {
43        if (socket != null) {
44            try {
45                socket.close();
46            } catch (Exception ignored) {
47            }
48        }
49    }
50
51    public static void throwInterruptedIoException() throws InterruptedIOException {
52        // This is typically thrown in response to an
53        // InterruptedException which does not leave the thread in an
54        // interrupted state, so explicitly interrupt here.
55        Thread.currentThread().interrupt();
56        // TODO: set InterruptedIOException.bytesTransferred
57        throw new InterruptedIOException();
58    }
59}
60