Coverage Report - ca.uhn.hl7v2.hoh.util.IOUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
IOUtils
18%
9/49
10%
2/20
2.5
 
 1  
 package ca.uhn.hl7v2.hoh.util;
 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.OutputStream;
 8  
 import java.util.List;
 9  
 
 10  
 /**
 11  
  * Utilities for dealing with IO
 12  
  */
 13  
 public class IOUtils {
 14  
 
 15  
         public static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
 16  5
         public static final String FILE_PATH_SEP = System.getProperty("file.separator");
 17  
 
 18  5
         private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(IOUtils.class);
 19  
 
 20  
         /**
 21  
          * Non instantiable
 22  
          */
 23  
         private IOUtils() {
 24  0
                 super();
 25  0
         }
 26  
 
 27  
         /**
 28  
          * <p>
 29  
          * Copy bytes from a large (over 2GB) <code>InputStream</code> to an
 30  
          * <code>OutputStream</code>.
 31  
          * </p>
 32  
          * <p>
 33  
          * This method is based on an implementation from Apache Commons-IO
 34  
          * </p>
 35  
          * 
 36  
          * @param input
 37  
          *            the <code>InputStream</code> to read from
 38  
          * @param output
 39  
          *            the <code>OutputStream</code> to write to
 40  
          * @return the number of bytes copied
 41  
          * @throws NullPointerException
 42  
          *             if the input or output is null
 43  
          * @throws IOException
 44  
          *             if an I/O error occurs
 45  
          */
 46  
         public static long copy(InputStream input, OutputStream output) throws IOException {
 47  10
                 byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
 48  10
                 long count = 0;
 49  10
                 int n = 0;
 50  20
                 while (-1 != (n = input.read(buffer))) {
 51  10
                         output.write(buffer, 0, n);
 52  10
                         count += n;
 53  
                 }
 54  10
                 return count;
 55  
         }
 56  
 
 57  
         public static long copyWhileDataAvailable(InputStream input, OutputStream output) throws IOException {
 58  0
                 byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
 59  0
                 long count = 0;
 60  0
                 int n = 0;
 61  0
                 while (input.available() > 0 && -1 != (n = input.read(buffer))) {
 62  0
                         output.write(buffer, 0, n);
 63  0
                         count += n;
 64  
                 }
 65  0
                 return count;
 66  
         }
 67  
 
 68  
         public static void deleteAllFromDirectory(File theWorkfilesDir) {
 69  0
                 File[] listFiles = theWorkfilesDir.listFiles();
 70  0
                 if (listFiles.length == 0) {
 71  0
                         return;
 72  
                 }
 73  0
                 ourLog.info("Deleting {} files from {}", listFiles.length, theWorkfilesDir.getAbsoluteFile());
 74  0
                 for (File next : listFiles) {
 75  0
                         ourLog.info("Deleting existing file: " + next);
 76  0
                         next.delete();
 77  
                 }
 78  0
         }
 79  
 
 80  
         public static void deleteAllFromDirectoryExcept(File theDirectory, List<File> theExcept) throws IOException {
 81  0
                 File[] listFiles = theDirectory.listFiles();
 82  0
                 if (listFiles.length == 0) {
 83  0
                         return;
 84  
                 }
 85  0
                 ourLog.info("Deleting unneeded files from {}", theDirectory.getAbsoluteFile());
 86  0
                 for (File nextFile : listFiles) {
 87  0
                         boolean keep = false;
 88  0
                         for (File nextExcept : theExcept) {
 89  0
                                 if (nextFile.getCanonicalPath().equals(nextExcept.getCanonicalPath())) {
 90  0
                                         keep = true;
 91  0
                                         break;
 92  
                                 }
 93  0
                         }
 94  0
                         if (!keep) {
 95  0
                                 ourLog.info("Deleting existing file: " + nextFile);
 96  0
                                 nextFile.delete();
 97  
                         }
 98  
                 }
 99  0
         }
 100  
 
 101  
         /**
 102  
          * Read a classpath resource into a byte array
 103  
          */
 104  
         public static byte[] readClasspathIntoByteArray(String theString) throws IOException {
 105  0
                 InputStream res = IOUtils.class.getResourceAsStream(theString);
 106  0
                 return readInputStreamIntoByteArray(res);
 107  
         }
 108  
 
 109  
         public static byte[] readInputStreamIntoByteArraWhileDataAvailable(InputStream res) throws IOException {
 110  0
                 java.io.ByteArrayOutputStream bos = new ByteArrayOutputStream();
 111  0
                 copyWhileDataAvailable(res, bos);
 112  0
                 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  0
                 java.io.ByteArrayOutputStream bos = new ByteArrayOutputStream();
 120  0
                 copy(res, bos);
 121  0
                 return bos.toByteArray();
 122  
         }
 123  
 
 124  
 }