View Javadoc
1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
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.util.zip.ZipInputStream;
23  
24  public class IOSystem {
25  	private static final Logger LOG = LogManager.getLogger(IOSystem.class);
26  
27  	public String slurp(final File file) throws IOException {
28  		final ByteArrayOutputStream out = new ByteArrayOutputStream();
29  		copy(file, out);
30  		return new String(out.toByteArray());
31  	}
32  
33  	public String slurp(final URL url) throws IOException {
34  		final ByteArrayOutputStream out = new ByteArrayOutputStream();
35  		copy(url.openStream(), out);
36  		return new String(out.toByteArray());
37  	}
38  
39  	public void writeString(final File file, final String string) throws IOException {
40  		final FileWriter out = new FileWriter(file);
41  		try {
42  			final BufferedWriter bufferedWriter = new BufferedWriter(out);
43  			try {
44  				bufferedWriter.write(string);
45  				bufferedWriter.newLine();
46  			} finally {
47  				close(bufferedWriter);
48  			}
49  		} finally {
50  			close(out);
51  		}
52  	}
53  
54  	private void copy(final File from, final OutputStream to) throws IOException {
55  		final InputStream read = read(from);
56  		try {
57  			copy(read, to);
58  		} finally {
59  			close(read);
60  		}
61  	}
62  
63  	public void copy(final InputStream from, final File to) throws IOException {
64  		final OutputStream write = write(to);
65  		try {
66  			copy(from, write);
67  		} finally {
68  			close(write);
69  		}
70  	}
71  
72  	private void copy(final InputStream from, final OutputStream to) throws IOException {
73  		final byte[] buffer = new byte[1024];
74  		int length = 0;
75  		while ((length = from.read(buffer)) != -1) {
76  			to.write(buffer, 0, length);
77  		}
78  		to.flush();
79  	}
80  
81  	public void copy(final byte[] from, final File to) throws IOException {
82  		copy(new ByteArrayInputStream(from), to);
83  	}
84  
85  	public ZipInputStream unzip(final File file) throws IOException {
86  		final InputStream read = read(file);
87  		return new ZipInputStream(read);
88  	}
89  
90  	public void close(final Closeable closeable) throws IOException {
91  		if (closeable == null) {
92  			return;
93  		}
94  		try {
95  			if (closeable instanceof Flushable) {
96  				((Flushable) closeable).flush();
97  			}
98  		} catch (final IOException e) {
99  			LOG.trace("Error when trying to flush before closing " + closeable, e);
100 		}
101 		try {
102 			closeable.close();
103 		} catch (final IOException e) {
104 			LOG.trace("Error when trying to close " + closeable, e);
105 		}
106 	}
107 
108 	public OutputStream write(final File destination) throws FileNotFoundException {
109 		final OutputStream out = new FileOutputStream(destination);
110 		return new BufferedOutputStream(out, 32768);
111 	}
112 
113 	public InputStream read(final File source) throws FileNotFoundException {
114 		final InputStream in = new FileInputStream(source);
115 		return new BufferedInputStream(in, 32768);
116 	}
117 
118 	public byte[] read(final InputStream in) throws IOException {
119 		final ByteArrayOutputStream out = new ByteArrayOutputStream();
120 		copy(in, out);
121 		out.close();
122 		return out.toByteArray();
123 	}
124 }