1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.creadur.tentacles;
18
19 import org.apache.logging.log4j.*;
20 import java.io.*;
21 import java.net.URL;
22 import java.nio.file.Files;
23 import java.util.zip.ZipInputStream;
24
25 public class IOSystem {
26 private static final Logger LOG = LogManager.getLogger(IOSystem.class);
27
28 public String slurp(final File file) throws IOException {
29 final ByteArrayOutputStream out = new ByteArrayOutputStream();
30 copy(file, out);
31 return out.toString();
32 }
33
34 public String slurp(final URL url) throws IOException {
35 final ByteArrayOutputStream out = new ByteArrayOutputStream();
36 copy(url.openStream(), out);
37 return out.toString();
38 }
39
40 public void writeString(final File file, final String string) throws IOException {
41 final FileWriter out = new FileWriter(file);
42 try {
43 final BufferedWriter bufferedWriter = new BufferedWriter(out);
44 try {
45 bufferedWriter.write(string);
46 bufferedWriter.newLine();
47 } finally {
48 close(bufferedWriter);
49 }
50 } finally {
51 close(out);
52 }
53 }
54
55 private void copy(final File from, final OutputStream to) throws IOException {
56 final InputStream read = read(from);
57 try {
58 copy(read, to);
59 } finally {
60 close(read);
61 }
62 }
63
64 public void copy(final InputStream from, final File to) throws IOException {
65 final OutputStream write = write(to);
66 try {
67 copy(from, write);
68 } finally {
69 close(write);
70 }
71 }
72
73 private void copy(final InputStream from, final OutputStream to) throws IOException {
74 final byte[] buffer = new byte[1024];
75 int length = 0;
76 while ((length = from.read(buffer)) != -1) {
77 to.write(buffer, 0, length);
78 }
79 to.flush();
80 }
81
82 public void copy(final byte[] from, final File to) throws IOException {
83 copy(new ByteArrayInputStream(from), to);
84 }
85
86 public ZipInputStream unzip(final File file) throws IOException {
87 final InputStream read = read(file);
88 return new ZipInputStream(read);
89 }
90
91 public void close(final Closeable closeable) {
92 if (closeable == null) {
93 return;
94 }
95 try {
96 if (closeable instanceof Flushable) {
97 ((Flushable) closeable).flush();
98 }
99 } catch (final IOException e) {
100 LOG.trace("Error when trying to flush before closing {}", closeable, e);
101 }
102 try {
103 closeable.close();
104 } catch (final IOException e) {
105 LOG.trace("Error when trying to close {}", closeable, e);
106 }
107 }
108
109 public OutputStream write(final File destination) throws IOException {
110 final OutputStream out = Files.newOutputStream(destination.toPath());
111 return new BufferedOutputStream(out, 32768);
112 }
113
114 public InputStream read(final File source) throws IOException {
115 final InputStream in = Files.newInputStream(source.toPath());
116 return new BufferedInputStream(in, 32768);
117 }
118
119 public byte[] read(final InputStream in) throws IOException {
120 final ByteArrayOutputStream out = new ByteArrayOutputStream();
121 copy(in, out);
122 out.close();
123 return out.toByteArray();
124 }
125 }