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 inputStream)
024: throws IOException {
025: ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
026: copyStream(inputStream, byteOut);
027: return byteOut.toByteArray();
028: }
029:
030: public static void copyStream(InputStream in, OutputStream out)
031: throws IOException {
032: byte[] buffer = new byte[4096];
033: while (true) {
034: int bytesRead = in.read(buffer);
035: if (bytesRead == -1) {
036: break;
037: }
038: out.write(buffer, 0, bytesRead);
039: }
040: }
041:
042: public static void close(OutputStream outputStream) {
043: if (outputStream != null) {
044: try {
045: outputStream.close();
046: } catch (IOException e) {
047: //nothing to do
048: }
049: }
050: }
051:
052: public static void close(InputStream inputStream) {
053: if (inputStream != null) {
054: try {
055: inputStream.close();
056: } catch (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: } catch (IOException e) {
067: //nothing to do
068: }
069: }
070: }
071:
072: public static void close(Reader reader) {
073: if (reader != null) {
074: try {
075: reader.close();
076: } catch (IOException e) {
077: //nothing to do
078: }
079: }
080: }
081:
082: public static void copy(File sourceFile, File destinationFile)
083: throws IOException {
084: if (!ensureFoldersExist(destinationFile.getParentFile())) {
085: throw new IOException(
086: "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(
093: sourceFile));
094: bos = new BufferedOutputStream(new FileOutputStream(
095: destinationFile));
096: copyStream(bis, bos);
097: } finally {
098: close(bis);
099: close(bos);
100: }
101: }
102:
103: public static boolean ensureFoldersExist(File folder) {
104: if (folder.exists()) {
105: return true;
106: }
107: return folder.mkdirs();
108: }
109:
110: public static File exchangeFileExtension(File file,
111: String newFileExtension) {
112: String fileName = file.getAbsolutePath();
113: int index = fileName.lastIndexOf('.');
114: if (index == -1) {
115: throw new IllegalStateException(
116: "Unable to determine file extension from file name '" //$NON-NLS-1$
117: + fileName + "'"); //$NON-NLS-1$
118: }
119: return new File(fileName.substring(0, index + 1)
120: + newFileExtension);
121: }
122: }
|