cd4efb8a5e20fcf32383806e3fcb6f7da630e6a0
[jpim.git] / src / de / j32 / util / Util.java
1 package de.j32.util;
2
3 import java.io.Closeable;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.io.OutputStream;
7 import java.util.Iterator;
8
9 public class Util
10 {
11
12     public static <E> E nonnull(E ob)
13     {
14         if (ob == null) throw new NullPointerException();
15         return ob;
16     }
17
18     public static void transfer(InputStream is, OutputStream os)
19             throws IOException
20     {
21         byte[] buffer = new byte[16384];
22         int len = -1;
23         while ((len = is.read(buffer)) != -1)
24             os.write(buffer, 0, len);
25     }
26
27     public static void nothrowClose(Closeable c)
28     {
29         if (c != null) {
30             try {
31                 c.close();
32             }
33             catch (IOException e) {}
34         }
35     }
36
37     public static <E> E first(Iterable<E> i)
38     {
39         Iterator<E> it = i.iterator();
40         if (it.hasNext()) return it.next();
41         return null;
42     }
43
44     public static <E> E first(Iterator<E> it)
45     {
46         if (it.hasNext()) return it.next();
47         return null;
48     }
49
50 }