View Javadoc
1   /*
2    * Copyright (c) 2001-2017, Zoltan Farkas All Rights Reserved.
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this program; if not, write to the Free Software
16   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17   *
18   * Additionally licensed with:
19   *
20   * Licensed under the Apache License, Version 2.0 (the "License");
21   * you may not use this file except in compliance with the License.
22   * You may obtain a copy of the License at
23   *
24   *      http://www.apache.org/licenses/LICENSE-2.0
25   *
26   * Unless required by applicable law or agreed to in writing, software
27   * distributed under the License is distributed on an "AS IS" BASIS,
28   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29   * See the License for the specific language governing permissions and
30   * limitations under the License.
31   */
32  package org.spf4j.base;
33  
34  import java.io.Closeable;
35  import java.io.IOException;
36  import java.nio.channels.SelectableChannel;
37  import java.nio.channels.SelectionKey;
38  import java.nio.channels.Selector;
39  import javax.annotation.CheckReturnValue;
40  import javax.annotation.Nullable;
41  
42  /**
43   *
44   * @author zoly
45   */
46  public final class Closeables {
47  
48    private Closeables() {
49    }
50  
51    @Nullable
52    @CheckReturnValue
53    public static Exception closeAll(final AutoCloseable... closeables) {
54      return closeAll(null, closeables);
55    }
56  
57    @Nullable
58    @CheckReturnValue
59    public static Exception closeAll(@Nullable final Exception propagate, final AutoCloseable... closeables) {
60      Exception ex = propagate;
61      for (AutoCloseable closeable : closeables) {
62        try {
63          closeable.close();
64        } catch (Exception ex1) {
65          if (ex == null) {
66            ex = ex1;
67          } else {
68            Throwables.suppressLimited(ex1, ex);
69            ex = ex1;
70          }
71        }
72      }
73      return ex;
74    }
75  
76    @Nullable
77    @CheckReturnValue
78    public static IOException closeAll(final Closeable... closeables) {
79      return closeAll(null, closeables);
80    }
81  
82    @Nullable
83    @CheckReturnValue
84    public static IOException closeAll(@Nullable final Exception propagate, final Closeable... closeables) {
85      IOException ex;
86      if (propagate == null) {
87        ex = null;
88      } else if (propagate instanceof IOException) {
89        ex = (IOException) propagate;
90      } else {
91        ex = new IOException(propagate);
92      }
93      for (Closeable closeable : closeables) {
94        try {
95          closeable.close();
96        } catch (IOException ex1) {
97          if (ex != null) {
98            Throwables.suppressLimited(ex1, ex);
99          }
100         ex = ex1;
101       }
102     }
103     return ex;
104   }
105 
106   @Nullable
107   @CheckReturnValue
108   public static IOException closeAll(final Closeable[] closeables, final int from, final int to) {
109     IOException ex = null;
110     for (int i = from; i < to; i++) {
111       Closeable closeable = closeables[i];
112       try {
113         closeable.close();
114       } catch (IOException ex1) {
115         if (ex != null) {
116           Throwables.suppressLimited(ex1, ex);
117         }
118         ex = ex1;
119       }
120     }
121     return ex;
122   }
123 
124   @Nullable
125   @CheckReturnValue
126   public static Exception closeAll(final Iterable<? extends AutoCloseable> closeables) {
127     return closeAll(null, closeables);
128   }
129 
130   @Nullable
131   @CheckReturnValue
132   public static Exception closeAll(@Nullable final Exception propagate,
133           final Iterable<? extends AutoCloseable> closeables) {
134     Exception ex = propagate;
135     for (AutoCloseable closeable : closeables) {
136       try {
137         closeable.close();
138       } catch (Exception ex1) {
139         if (ex != null) {
140           Throwables.suppressLimited(ex1, ex);
141         }
142         ex = ex1;
143       }
144     }
145     return ex;
146   }
147 
148   @Nullable
149   @CheckReturnValue
150   public static IOException closeSelectorChannels(final Selector selector) {
151     return closeSelectorChannels(null, selector);
152   }
153 
154   @Nullable
155   @CheckReturnValue
156   public static IOException closeSelectorChannels(@Nullable final IOException propagate, final Selector selector) {
157     IOException ex = propagate;
158     for (SelectionKey key : selector.keys()) {
159       SelectableChannel channel = key.channel();
160       try {
161         channel.close();
162       } catch (IOException ex2) {
163         if (ex != null) {
164           Throwables.suppressLimited(ex2, ex);
165         }
166         ex = ex2;
167       }
168     }
169     return ex;
170   }
171 
172 }