001/*
002 * To change this template, choose Tools | Templates
003 * and open the template in the editor.
004 */
005
006package com.nativelibs4java.util;
007
008import java.io.*;
009import java.net.*;
010
011/**
012 *
013 * @author Olivier
014 */
015public class IOUtils {
016    public static String readText(File f) throws IOException {
017        Reader in = new FileReader(f);
018        try {
019            return readText(in);
020        } finally {
021            in.close();
022        }
023    }
024    public static String readText(InputStream in) throws IOException {
025        return readText(new InputStreamReader(in));
026    }
027    public static String readText(URL url) throws IOException {
028        return readTextClose(url.openStream());
029    }
030    public static String readTextClose(InputStream in) throws IOException {
031        return readTextClose(new InputStreamReader(in));
032    }
033    public static String readTextClose(Reader in) throws IOException {
034        try {
035            return readText(in);
036        } finally {
037            in.close();
038        }
039    }
040    public static String readText(Reader in) throws IOException {
041        StringBuffer b = new StringBuffer();
042        BufferedReader bin = new BufferedReader(in);
043        String line;
044        while ((line = bin.readLine()) != null) {
045            b.append(line);
046            b.append('\n');
047        }
048        return b.toString();
049    }
050    public static void readWrite(InputStream in, OutputStream out) throws IOException {
051                byte[] buf = new byte[1024];
052                int len;
053                while ((len = in.read(buf)) > 0)
054                        out.write(buf, 0, len);
055    }
056}