001package org.avaje.freemarker.util;
002
003import java.io.ByteArrayOutputStream;
004import java.io.IOException;
005import java.io.InputStream;
006import java.io.OutputStream;
007import java.io.Reader;
008
009/**
010 * IO utilities.
011 */
012public class IOUtil {
013
014        private IOUtil() {
015                /* no instances */
016        }
017
018        /**
019         * Reads the entire contents of the specified input stream and returns them
020         * as a byte array.
021         */
022        public static byte[] read(InputStream in) throws IOException {
023                ByteArrayOutputStream buffer = new ByteArrayOutputStream();
024                pump(in, buffer);
025                return buffer.toByteArray();
026        }
027
028        /**
029         * Reads the entire contents of the specified input stream and returns them
030         * as an ASCII string.
031         */
032        public static String readAscii(InputStream in) throws IOException {
033                return EncodeUtil.bytesToAscii(read(in));
034        }
035        
036        public static String readUTF8(InputStream in) throws IOException {
037    return EncodeUtil.bytesToUtf8(read(in));
038  }
039
040        /**
041         * Reads the entire contents of the specified input stream and returns them
042         * as UTF-8 string.
043         */
044        public static String readUtf8(InputStream in) throws IOException {
045                return EncodeUtil.bytesToUtf8(read(in));
046        }
047
048        /**
049         * Reads the entire contents of the specified input stream and returns them
050         * as a string using the encoding supplied.
051         */
052        public static String readEncoded(InputStream in, String encoding) throws IOException {
053                return EncodeUtil.decodeBytes(read(in), encoding);
054        }
055
056        public static String read(Reader reader) throws IOException {
057                
058                StringBuilder sb = new StringBuilder();
059                try {
060                        char[] buffer = new char[4096];
061                        for (;;) {
062                                int len = reader.read(buffer);
063                                if (len < 0) {
064                                        break;
065                                }
066                                sb.append(buffer, 0, len);
067                        }
068                } finally {
069                        reader.close();
070                }       
071                
072                return sb.toString();
073        }
074        
075        /**
076         * Reads data from the specified input stream and copies it to the specified
077         * output stream, until the input stream is at EOF. Both streams are then
078         * closed.
079         * 
080         * @throws IOException
081         *             if the input or output stream is <code>null</code>
082         */
083        public static void pump(InputStream in, OutputStream out) throws IOException {
084                if (in == null) throw new IOException("Input stream is null");
085                if (out == null) throw new IOException("Output stream is null");
086
087                try {
088                        try {
089                                byte[] buffer = new byte[4096];
090                                for (;;) {
091                                        int bytes = in.read(buffer);
092                                        if (bytes < 0) {
093                                                break;
094                                        }
095                                        out.write(buffer, 0, bytes);
096                                }
097                        } finally {
098                                in.close();
099                        }
100                } finally {
101                        out.close();
102                }
103        }
104
105}