KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > util > FileUtil


1 /****************************************************************************
2 * CruiseControl, a Continuous Integration Toolkit
3 * Copyright (c) 2001, ThoughtWorks, Inc.
4 * 651 W Washington Ave. Suite 600
5 * Chicago, IL 60661 USA
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * + Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * + Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer in the documentation and/or other materials provided
18 * with the distribution.
19 *
20 * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
21 * names of its contributors may be used to endorse or promote
22 * products derived from this software without specific prior
23 * written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
29 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 ****************************************************************************/

37
38 package net.sourceforge.cruisecontrol.util;
39
40 import java.io.File JavaDoc;
41 import java.io.FileInputStream JavaDoc;
42 import java.io.FileOutputStream JavaDoc;
43 import java.io.IOException JavaDoc;
44 import java.io.InputStream JavaDoc;
45 import java.io.ObjectOutputStream JavaDoc;
46
47 import org.apache.log4j.Logger;
48
49 public final class FileUtil {
50
51     private static final Logger LOG = Logger.getLogger(FileUtil.class);
52
53     private FileUtil() {
54
55     }
56
57     public static byte[] getFileAsBytes(File JavaDoc file) throws IOException JavaDoc {
58         InputStream JavaDoc is = new FileInputStream JavaDoc(file);
59
60         // Get the size of the file
61
long length = file.length();
62
63         // You cannot create an array using a long type.
64
// It needs to be an int type.
65
// Before converting to an int type, check
66
// to ensure that file is not larger than Integer.MAX_VALUE.
67
if (length > Integer.MAX_VALUE) {
68             // File is too large
69
throw new IOException JavaDoc("File too large. File size is " + length + ". Maximum allowed is "
70                     + Integer.MAX_VALUE);
71         }
72
73         // Create the byte array to hold the data
74
byte[] bytes = new byte[(int) length];
75
76         // Read in the bytes
77
int offset = 0;
78         int numRead = 0;
79         while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
80             offset += numRead;
81         }
82
83         // Ensure all the bytes have been read in
84
if (offset < bytes.length) {
85             throw new IOException JavaDoc("Could not completely read file " + file.getName());
86         }
87
88         // Close the input stream and return bytes
89
is.close();
90         return bytes;
91     }
92     
93     public static String JavaDoc bytesToFile(byte[] data, String JavaDoc filePath) {
94         File JavaDoc outFile = new File JavaDoc(filePath);
95         try {
96             FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(outFile);
97             ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(fos);
98             oos.writeObject(data);
99             oos.close();
100             fos.close();
101         } catch (IOException JavaDoc e) {
102             String JavaDoc message = "Error creating output file";
103             LOG.error(message, e);
104             System.err.println(message + " - " + e.getMessage());
105         }
106         return filePath;
107     }
108     
109     public static String JavaDoc bytesToFile(byte[] data, String JavaDoc parentDirName, String JavaDoc fileName) {
110         String JavaDoc filePath = null;
111         try {
112             filePath = new File JavaDoc(parentDirName, fileName).getCanonicalPath();
113         } catch (IOException JavaDoc e) {
114             String JavaDoc message = "Could not determine canonical name for: " + parentDirName + File.separator + fileName;
115             System.err.println(message);
116             LOG.error(message, e);
117         }
118         return bytesToFile(data, filePath);
119     }
120 }
121
Popular Tags