View Javadoc

1   package net.krecan.m2_proxy.utils;
2   
3   import java.io.ByteArrayOutputStream;
4   import java.io.File;
5   import java.io.IOException;
6   import java.io.InputStream;
7   import java.io.InputStreamReader;
8   import java.io.OutputStream;
9   
10  import net.krecan.m2_proxy.Resource;
11  
12  import org.apache.commons.logging.Log;
13  import org.apache.commons.logging.LogFactory;
14  import org.springframework.util.FileCopyUtils;
15  
16  /***
17   * Utilities for coping with resources
18   * @author krel
19   *
20   */
21  public class ResourceUtils {
22  	
23  	private static final Log LOG = LogFactory.getLog(ResourceUtils.class);
24  	
25  	private ResourceUtils() {
26  		super();
27  	}
28  	
29  	/***
30  	 * Copy file
31  	 * @param in
32  	 * @param out
33  	 * @throws IOException
34  	 */
35  	public static final void copy(File in, File out) throws IOException
36  	{
37  		LOG.debug("Copying file "+in+" to "+out);
38  		FileCopyUtils.copy(in, out);
39  	}
40  	/***
41  	 * Copy data from input stream to the output stream
42  	 * @param in
43  	 * @param out
44  	 * @throws IOException
45  	 */
46  	public static final void copy(InputStream in, OutputStream out) throws IOException
47  	{
48  		FileCopyUtils.copy(in, out);
49  	}
50  	
51  	/***
52  	 * Copy the data from the stream to the string
53  	 * @param in
54  	 * @return
55  	 * @throws IOException
56  	 */
57  	public static final String copyToString(InputStream in) throws IOException
58  	{
59  		return FileCopyUtils.copyToString(new InputStreamReader(in));
60  	}
61  	
62  	/***
63  	 * Returns content of the resource as a string
64  	 * @param resource
65  	 * @return
66  	 * @throws IOException
67  	 */
68  	public static final String extractStringFromResource(Resource resource) throws IOException
69  	{
70  		ByteArrayOutputStream out = new ByteArrayOutputStream();
71  		resource.copyToStream(out);
72  		return new String(out.toByteArray());
73  	}
74  
75  }