/* * Copyright 2004 Gunn Software Ltd. http://www.gunnsoft.com.au/ * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.sf.tapestrypublisher; import java.io.*; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; /** * @author Paul Stanton */ public class IOUtils { /** * copies a file creating parent directories for new file if necissary * @param src * @param dest * @throws IOException */ public static void copy(File src, File dest) throws IOException { dest.getParentFile().mkdirs(); FileInputStream in = null; FileOutputStream out = null; try { in = new FileInputStream(src); out = new FileOutputStream(dest); readInto(in, out); } finally { close(in); close(out); } } /** * convenience method closes io resources */ public static void close(Object obj) { if (obj == null) return; if (obj instanceof OutputStream) { try { ((OutputStream) obj).flush(); } catch (Exception e) {} try { ((OutputStream) obj).close(); } catch (Exception e) {} } else if (obj instanceof InputStream) { try { ((InputStream) obj).close(); } catch (Exception e) {} } else if (obj instanceof Writer) { try { ((Writer) obj).flush(); } catch (Exception e) {} try { ((Writer) obj).close(); } catch (Exception e) {} } else if (obj instanceof Reader) { try { ((Reader) obj).close(); } catch (Exception e) {} } else throw new IllegalArgumentException("unhandled class : " + obj.getClass().getName()); } /** * @param in * @return the content contained by in * @throws IOException */ public static String istreamToString(InputStream in) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); String message; try { IOUtils.readInto(in, out); message = out.toString(); } finally { IOUtils.close(out); } return message; } /** * reads from the input stream into the output stream */ private static void readInto(InputStream in, OutputStream out) throws IOException { byte[] buf = new byte[1024]; int i = 0; while ((i = in.read(buf)) != -1) out.write(buf, 0, i); } /** * @param url * @return the document content represented by this url * @throws MalformedURLException * @throws IOException */ public static String getWebDocument(String url) throws MalformedURLException, IOException { URLConnection conn = new URL(url).openConnection(); conn.connect(); InputStream in = null; try { in = conn.getInputStream(); return IOUtils.istreamToString(in); } finally { IOUtils.close(in); } } /** * writes data to a file * @param file * @param bytes * @throws IOException */ public static void writeToFile(File file, byte[] bytes) throws IOException { file.getParentFile().mkdirs(); FileOutputStream out = new FileOutputStream(file); try { out.write(bytes); } finally { close(out); } } }