001package ca.uhn.hl7v2.hoh.util;
002
003import java.io.ByteArrayOutputStream;
004import java.io.File;
005import java.io.IOException;
006import java.io.InputStream;
007import java.io.OutputStream;
008import java.util.List;
009
010/**
011 * Utilities for dealing with IO
012 */
013public class IOUtils {
014
015        public static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
016        public static final String FILE_PATH_SEP = System.getProperty("file.separator");
017
018        private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(IOUtils.class);
019
020        /**
021         * Non instantiable
022         */
023        private IOUtils() {
024                super();
025        }
026
027        /**
028         * <p>
029         * Copy bytes from a large (over 2GB) <code>InputStream</code> to an
030         * <code>OutputStream</code>.
031         * </p>
032         * <p>
033         * This method is based on an implementation from Apache Commons-IO
034         * </p>
035         * 
036         * @param input
037         *            the <code>InputStream</code> to read from
038         * @param output
039         *            the <code>OutputStream</code> to write to
040         * @return the number of bytes copied
041         * @throws NullPointerException
042         *             if the input or output is null
043         * @throws IOException
044         *             if an I/O error occurs
045         */
046        public static long copy(InputStream input, OutputStream output) throws IOException {
047                byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
048                long count = 0;
049                int n = 0;
050                while (-1 != (n = input.read(buffer))) {
051                        output.write(buffer, 0, n);
052                        count += n;
053                }
054                return count;
055        }
056
057        public static long copyWhileDataAvailable(InputStream input, OutputStream output) throws IOException {
058                byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
059                long count = 0;
060                int n = 0;
061                while (input.available() > 0 && -1 != (n = input.read(buffer))) {
062                        output.write(buffer, 0, n);
063                        count += n;
064                }
065                return count;
066        }
067
068        public static void deleteAllFromDirectory(File theWorkfilesDir) {
069                File[] listFiles = theWorkfilesDir.listFiles();
070                if (listFiles.length == 0) {
071                        return;
072                }
073                ourLog.info("Deleting {} files from {}", listFiles.length, theWorkfilesDir.getAbsoluteFile());
074                for (File next : listFiles) {
075                        ourLog.info("Deleting existing file: " + next);
076                        next.delete();
077                }
078        }
079
080        public static void deleteAllFromDirectoryExcept(File theDirectory, List<File> theExcept) throws IOException {
081                File[] listFiles = theDirectory.listFiles();
082                if (listFiles.length == 0) {
083                        return;
084                }
085                ourLog.info("Deleting unneeded files from {}", theDirectory.getAbsoluteFile());
086                for (File nextFile : listFiles) {
087                        boolean keep = false;
088                        for (File nextExcept : theExcept) {
089                                if (nextFile.getCanonicalPath().equals(nextExcept.getCanonicalPath())) {
090                                        keep = true;
091                                        break;
092                                }
093                        }
094                        if (!keep) {
095                                ourLog.info("Deleting existing file: " + nextFile);
096                                nextFile.delete();
097                        }
098                }
099        }
100
101        /**
102         * Read a classpath resource into a byte array
103         */
104        public static byte[] readClasspathIntoByteArray(String theString) throws IOException {
105                InputStream res = IOUtils.class.getResourceAsStream(theString);
106                return readInputStreamIntoByteArray(res);
107        }
108
109        public static byte[] readInputStreamIntoByteArraWhileDataAvailable(InputStream res) throws IOException {
110                java.io.ByteArrayOutputStream bos = new ByteArrayOutputStream();
111                copyWhileDataAvailable(res, bos);
112                return bos.toByteArray();
113        }
114
115        /**
116         * Read an entire input stream into a byte array
117         */
118        public static byte[] readInputStreamIntoByteArray(InputStream res) throws IOException {
119                java.io.ByteArrayOutputStream bos = new ByteArrayOutputStream();
120                copy(res, bos);
121                return bos.toByteArray();
122        }
123
124}