001: package net.sourceforge.cruisecontrol.util;
002:
003: import java.io.BufferedOutputStream;
004: import java.io.BufferedReader;
005: import java.io.File;
006: import java.io.FileOutputStream;
007: import java.io.FileReader;
008: import java.io.FileWriter;
009: import java.io.IOException;
010: import java.io.InputStream;
011: import java.io.OutputStream;
012: import java.io.PrintStream;
013: import java.io.Reader;
014: import java.io.Writer;
015: import java.util.ArrayList;
016: import java.util.List;
017:
018: import net.sourceforge.cruisecontrol.CruiseControlException;
019:
020: import org.apache.log4j.Logger;
021: import org.jdom.Document;
022: import org.jdom.Element;
023: import org.jdom.output.Format;
024: import org.jdom.output.XMLOutputter;
025:
026: /**
027: * This class provides helper methods for interacting with Input/Output classes.
028: */
029: public final class IO {
030:
031: private IO() {
032: //Helper methods only.
033: }
034:
035: public static void close(OutputStream o) {
036: if (o != null) {
037: try {
038: o.close();
039: } catch (IOException ignored) {
040: // nevermind, then
041: }
042: }
043: }
044:
045: public static void close(InputStream i) {
046: if (i != null) {
047: try {
048: i.close();
049: } catch (IOException ignored) {
050: }
051: }
052: }
053:
054: public static void close(Reader r) {
055: if (r != null) {
056: try {
057: r.close();
058: } catch (IOException ignored) {
059: //Never mind
060: }
061: }
062: }
063:
064: public static void close(Writer w) {
065: if (w != null) {
066: try {
067: w.close();
068: } catch (IOException ignored) {
069: //Never mind
070: }
071: }
072: }
073:
074: public static void close(Process p) {
075: try {
076: close(p.getInputStream());
077: close(p.getOutputStream());
078: close(p.getErrorStream());
079: } finally {
080: if (p != null) {
081: p.destroy();
082: }
083: }
084: }
085:
086: public static void output(File to, Element xml, String encoding)
087: throws CruiseControlException {
088: OutputStream logStream = null;
089: try {
090: Format format = Format.getPrettyFormat();
091: if (encoding != null) {
092: format.setEncoding(encoding);
093: }
094: XMLOutputter outputter = new XMLOutputter(format);
095: logStream = new BufferedOutputStream(new FileOutputStream(
096: to));
097: outputter.output(new Document(xml), logStream);
098: } catch (IOException e) {
099: throw new CruiseControlException(e);
100: } finally {
101: close(logStream);
102: }
103: }
104:
105: /**
106: * Deletes a File instance. If the file represents a directory, all
107: * the subdirectories and files within.
108: */
109: public static void delete(File f) {
110: if (f == null || !f.exists()) {
111: return;
112: }
113: if (f.isDirectory()) {
114: deleteDir(f);
115: return;
116: }
117: f.delete();
118: }
119:
120: private static void deleteDir(File dir) {
121: File[] children = dir.listFiles();
122: for (int i = 0; i < children.length; i++) {
123: File child = children[i];
124: delete(child);
125: }
126: dir.delete();
127: }
128:
129: public static void delete(File f, boolean debuggerOn, Logger log) {
130: try {
131: delete(f);
132: if (debuggerOn) {
133: log.info("Removed temp file " + f.getAbsolutePath());
134: }
135: } catch (Exception ignored) {
136: //never mind
137: }
138: }
139:
140: /**
141: * Writes the contents of a file to a PrintStream.
142: */
143: public static void dumpTo(File f, PrintStream out) {
144: BufferedReader in = null;
145: try {
146: in = new BufferedReader(new FileReader(f));
147: while (in.ready()) {
148: out.println(in.readLine());
149: }
150: } catch (Exception ignored) {
151: } finally {
152: close(in);
153: }
154: }
155:
156: /**
157: * Write the content to the file.
158: */
159: public static void write(String fileName, String content)
160: throws CruiseControlException {
161: write(new File(fileName), content);
162: }
163:
164: /**
165: * Write the content to the file.
166: */
167: public static void write(File f, String contents)
168: throws CruiseControlException {
169: FileWriter fw = null;
170: try {
171: fw = new FileWriter(f);
172: fw.write(contents);
173: } catch (IOException ioe) {
174: throw new CruiseControlException("Error writing file: "
175: + f.getAbsolutePath(), ioe);
176: } finally {
177: close(fw);
178: }
179: }
180:
181: /**
182: * @return List of lines of text (String objects)
183: */
184: public static List readLines(File source)
185: throws CruiseControlException {
186:
187: List result = new ArrayList();
188: BufferedReader reader = null;
189: try {
190: reader = new BufferedReader(new FileReader(source));
191: String line;
192: while ((line = reader.readLine()) != null) {
193: result.add(line);
194: }
195: } catch (IOException ioe) {
196: throw new CruiseControlException("Error reading file: "
197: + source.getAbsolutePath(), ioe);
198: } finally {
199: close(reader);
200: }
201:
202: return result;
203: }
204:
205: }
|