001 package de.java2html.util;
002 
003 import java.io.BufferedInputStream;
004 import java.io.BufferedOutputStream;
005 import java.io.ByteArrayOutputStream;
006 import java.io.File;
007 import java.io.FileInputStream;
008 import java.io.FileOutputStream;
009 import java.io.IOException;
010 import java.io.InputStream;
011 import java.io.OutputStream;
012 import java.io.Reader;
013 import java.io.Writer;
014 
015 /**
016  @author Markus Gebhard
017  */
018 public class IoUtilities {
019   private IoUtilities() {
020     //no instance available
021   }
022 
023   public static byte[] readBytes(InputStream inputStreamthrows IOException {
024     final ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
025     copyStream(inputStream, byteOut);
026     return byteOut.toByteArray();
027   }
028 
029   public static void copyStream(InputStream in, OutputStream outthrows IOException {
030     final byte[] buffer = new byte[4096];
031     while (true) {
032       final int bytesRead = in.read(buffer);
033       if (bytesRead == -1) {
034         break;
035       }
036       out.write(buffer, 0, bytesRead);
037     }
038   }
039 
040   public static void close(OutputStream outputStream) {
041     if (outputStream != null) {
042       try {
043         outputStream.close();
044       }
045       catch (final IOException e) {
046         //nothing to do
047       }
048     }
049   }
050 
051   public static void close(InputStream inputStream) {
052     if (inputStream != null) {
053       try {
054         inputStream.close();
055       }
056       catch (final IOException e) {
057         //nothing to do
058       }
059     }
060   }
061 
062   public static void close(Writer writer) {
063     if (writer != null) {
064       try {
065         writer.close();
066       }
067       catch (final IOException e) {
068         //nothing to do
069       }
070     }
071   }
072 
073   public static void close(Reader reader) {
074     if (reader != null) {
075       try {
076         reader.close();
077       }
078       catch (final IOException e) {
079         //nothing to do
080       }
081     }
082   }
083 
084   public static void copy(File sourceFile, File destinationFilethrows IOException {
085     if (!ensureFoldersExist(destinationFile.getParentFile())) {
086       throw new IOException("Unable to create necessary output directory " //$NON-NLS-1$
087           + destinationFile.getParentFile());
088     }
089     BufferedInputStream bis = null;
090     BufferedOutputStream bos = null;
091     try {
092       bis = new BufferedInputStream(new FileInputStream(sourceFile));
093       bos = new BufferedOutputStream(new FileOutputStream(destinationFile));
094       copyStream(bis, bos);
095     }
096     finally {
097       close(bis);
098       close(bos);
099     }
100   }
101 
102   public static boolean ensureFoldersExist(File folder) {
103     if (folder.exists()) {
104       return true;
105     }
106     return folder.mkdirs();
107   }
108 
109   public static File exchangeFileExtension(File file, String newFileExtension) {
110     final String fileName = file.getAbsolutePath();
111     final int index = fileName.lastIndexOf('.');
112     if (index == -1) {
113       throw new IllegalStateException("Unable to determine file extension from file name '" //$NON-NLS-1$
114           + fileName
115           "'")//$NON-NLS-1$
116     }
117     return new File(fileName.substring(0, index + 1+ newFileExtension);
118   }
119 }