Stuart McCulloch | 26e7a5a | 2011-10-17 10:31:43 +0000 | [diff] [blame^] | 1 | package aQute.lib.io; |
| 2 | |
| 3 | import java.io.*; |
| 4 | import java.net.*; |
| 5 | import java.nio.*; |
| 6 | import java.util.*; |
| 7 | |
| 8 | public class IO { |
| 9 | public static void copy(InputStream in, OutputStream out) throws IOException { |
| 10 | DataOutputStream dos = new DataOutputStream(out); |
| 11 | copy(in, (DataOutput) dos); |
| 12 | } |
| 13 | |
| 14 | public static void copy(InputStream in, DataOutput out) throws IOException { |
| 15 | byte[] buffer = new byte[10000]; |
| 16 | try { |
| 17 | int size = in.read(buffer); |
| 18 | while (size > 0) { |
| 19 | out.write(buffer, 0, size); |
| 20 | size = in.read(buffer); |
| 21 | } |
| 22 | } finally { |
| 23 | in.close(); |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | public static void copy(InputStream in, ByteBuffer bb) throws IOException { |
| 28 | byte[] buffer = new byte[10000]; |
| 29 | try { |
| 30 | int size = in.read(buffer); |
| 31 | while (size > 0) { |
| 32 | bb.put(buffer, 0, size); |
| 33 | size = in.read(buffer); |
| 34 | } |
| 35 | } finally { |
| 36 | in.close(); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | public static void copy(File a, File b) throws IOException { |
| 41 | FileOutputStream out = new FileOutputStream(b); |
| 42 | try { |
| 43 | copy(new FileInputStream(a), out); |
| 44 | } finally { |
| 45 | out.close(); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | public static void copy(InputStream a, File b) throws IOException { |
| 50 | FileOutputStream out = new FileOutputStream(b); |
| 51 | try { |
| 52 | copy(a, out); |
| 53 | } finally { |
| 54 | out.close(); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | public static void copy(File a, OutputStream b) throws IOException { |
| 59 | copy(new FileInputStream(a), b); |
| 60 | } |
| 61 | |
| 62 | public static String collect(File a, String encoding) throws IOException { |
| 63 | ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 64 | copy(a, out); |
| 65 | return new String(out.toByteArray(), encoding); |
| 66 | } |
| 67 | |
| 68 | public static String collect(URL a, String encoding) throws IOException { |
| 69 | ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 70 | copy(a.openStream(), out); |
| 71 | return new String(out.toByteArray(), encoding); |
| 72 | } |
| 73 | |
| 74 | public static String collect(File a) throws IOException { |
| 75 | return collect(a, "UTF-8"); |
| 76 | } |
| 77 | |
| 78 | public static String collect(InputStream a, String encoding) throws IOException { |
| 79 | ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 80 | copy(a, out); |
| 81 | return new String(out.toByteArray(), encoding); |
| 82 | } |
| 83 | |
| 84 | public static String collect(InputStream a) throws IOException { |
| 85 | return collect(a, "UTF-8"); |
| 86 | } |
| 87 | |
| 88 | public static String collect(Reader a) throws IOException { |
| 89 | StringWriter sw = new StringWriter(); |
| 90 | char[] buffer = new char[10000]; |
| 91 | int size = a.read(buffer); |
| 92 | while (size > 0) { |
| 93 | sw.write(buffer, 0, size); |
| 94 | size = a.read(buffer); |
| 95 | } |
| 96 | return sw.toString(); |
| 97 | } |
| 98 | |
| 99 | public static File getFile(File base, String file) { |
| 100 | File f = new File(file); |
| 101 | if (f.isAbsolute()) |
| 102 | return f; |
| 103 | int n; |
| 104 | |
| 105 | f = base.getAbsoluteFile(); |
| 106 | while ((n = file.indexOf('/')) > 0) { |
| 107 | String first = file.substring(0, n); |
| 108 | file = file.substring(n + 1); |
| 109 | if (first.equals("..")) |
| 110 | f = f.getParentFile(); |
| 111 | else |
| 112 | f = new File(f, first); |
| 113 | } |
| 114 | if (file.equals("..")) |
| 115 | return f.getParentFile(); |
| 116 | else |
| 117 | return new File(f, file).getAbsoluteFile(); |
| 118 | } |
| 119 | |
| 120 | public static void delete(File f) { |
| 121 | f = f.getAbsoluteFile(); |
| 122 | if (f.getParentFile() == null) |
| 123 | throw new IllegalArgumentException("Cannot recursively delete root for safety reasons"); |
| 124 | |
| 125 | if (f.isDirectory()) { |
| 126 | File[] subs = f.listFiles(); |
| 127 | for (File sub : subs) |
| 128 | delete(sub); |
| 129 | } |
| 130 | |
| 131 | f.delete(); |
| 132 | } |
| 133 | |
| 134 | public static void drain(InputStream in) throws IOException { |
| 135 | byte[] buffer = new byte[10000]; |
| 136 | try { |
| 137 | int size = in.read(buffer); |
| 138 | while (size > 0) { |
| 139 | size = in.read(buffer); |
| 140 | } |
| 141 | } finally { |
| 142 | in.close(); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | public void copy(Collection<?> c, OutputStream out) { |
| 147 | PrintStream ps = new PrintStream(out); |
| 148 | for (Object o : c) { |
| 149 | ps.println(o); |
| 150 | } |
| 151 | ps.flush(); |
| 152 | } |
| 153 | |
| 154 | public static Throwable close(Closeable in) { |
| 155 | try { |
| 156 | in.close(); |
| 157 | return null; |
| 158 | } catch (Throwable e) { |
| 159 | return e; |
| 160 | } |
| 161 | } |
| 162 | } |