View Javadoc

1   package org.eparapher.core.tools;
2   
3   
4   import java.io.BufferedOutputStream;
5   import java.io.DataInputStream;
6   import java.io.File;
7   import java.io.FileInputStream;
8   import java.io.FileOutputStream;
9   import java.io.IOException;
10  import java.io.InputStream;
11  import java.io.OutputStream;
12  
13  /**
14   * Work with files in file system (delete, get size, etc...).
15   *
16   * @author   Alexander Feldman (updated by $Author: feldman $)
17   * @version  $Revision: 1.11 $ $Date: 2002/05/02 14:41:53 $
18   *
19   */
20  public class FileUtil {
21  
22  	  /**
23  	   * Calls writeToFile with createDir flag false.
24  	   *
25  	   * @see writeToFile(String fileName, InputStream iStream, boolean createDir)
26  	   *
27  	   */
28  	  public static void writeToFile(String fileName, InputStream iStream)
29  	    throws IOException
30  	  {
31  	    writeToFile(fileName, iStream, false);
32  	  }
33  
34  
35  	  /**
36  	   * Writes InputStream to a given <code>fileName<code>.
37  	   * And, if directory for this file does not exists,
38  	   * if createDir is true, creates it, otherwice throws OMDIOexception.
39  	   *
40  	   * @param fileName - filename save to.
41  	   * @param iStream  - InputStream with data to read from.
42  	   * @param createDir (false by default)
43  	   * @throws IOException in case of any error.
44  	   *
45  	   */
46  	  public static void writeToFile(String fileName, InputStream iStream,
47  	    boolean createDir)
48  	    throws IOException
49  	  {
50  	    String me = "FileUtil.WriteToFile";
51  	    if (fileName == null)
52  	    {
53  	      throw new IOException(me + ": filename is null");
54  	    }
55  	    if (iStream == null)
56  	    {
57  	      throw new IOException(me + ": InputStream is null");
58  	    }
59  
60  	    File theFile = new File(fileName);
61  
62  	    // Check if a file exists.
63  	    if (theFile.exists())
64  	    {
65  	       String msg =
66  	         theFile.isDirectory() ? "directory" :
67  	         (! theFile.canWrite() ? "not writable" : null);
68  	       if (msg != null)
69  	       {
70  	         throw new IOException(me + ": file '" + fileName + "' is " + msg);
71  	       }
72  	    }
73  
74  	    // Create directory for the file, if requested.
75  	    if (createDir && theFile.getParentFile() != null)
76  	    {
77  	      theFile.getParentFile().mkdirs();
78  	    }
79  
80  	    // Save InputStream to the file.
81  	    BufferedOutputStream fOut = null;
82  	    try
83  	    {
84  	      fOut = new BufferedOutputStream(new FileOutputStream(theFile));
85  	      byte[] buffer = new byte[32 * 1024];
86  	      int bytesRead = 0;
87  	      while ((bytesRead = iStream.read(buffer)) != -1)
88  	      {
89  	        fOut.write(buffer, 0, bytesRead);
90  	      }
91  	    }
92  	    catch (Exception e)
93  	    {
94  	      throw new IOException(me + " failed, got: " + e.toString());
95  	    }
96  	    finally
97  	    {
98  	      close(iStream, fOut);
99  	    }
100 	  }
101 
102 	  public static void writeToFile(String fileName, byte[] data)
103 	  	throws IOException  {
104 			FileOutputStream envfos = new FileOutputStream(fileName);
105 			envfos.write(data);
106 			envfos.close();
107 	  }
108 	  /**
109 	   * Closes InputStream and/or OutputStream.
110 	   * It makes sure that both streams tried to be closed,
111 	   * even first throws an exception.
112 	   *
113 	   * @throw IOException if stream (is not null and) cannot be closed.
114 	   *
115 	   */
116 	  protected static void close(InputStream iStream, OutputStream oStream)
117 	    throws IOException
118 	  {
119 	    try
120 	    {
121 	      if (iStream != null)
122 	      {
123 	        iStream.close();
124 	      }
125 	    }
126 	    finally
127 	    {
128 	      if (oStream != null)
129 	      {
130 	        oStream.close();
131 	      }
132 	    }
133 	  }
134 
135 
136 	public static boolean fileExists(String fileName) {
137 		File file = new File(fileName);
138 		return file.exists();
139 	}
140 
141 	public static boolean fileEmpty(String fileName) {
142 		File file = new File(fileName);
143 		long filesize = file.length();
144 		return (file.length() == 0L);
145 	}
146 	
147 	public static byte[] readFile(String fileName) throws IOException {
148 		
149 		File file_to_sign = new File(fileName);
150 		byte[] buffer = new byte[(int)file_to_sign.length()];
151 		DataInputStream in = new DataInputStream(new FileInputStream(file_to_sign));
152 		in.readFully(buffer);
153 		in.close();
154 		
155 		return buffer;
156 		/*
157 		File file = new File(fileName);
158 		InputStream is = new FileInputStream(file);
159 		
160 		// Get the size of the file
161 		long length = file.length();
162 		if (length > Integer.MAX_VALUE)
163 			throw new IOException("File is too large");
164 		
165 		// Create the byte array to hold the data
166 		byte[] bytes = new byte[(int)length];
167 		
168 		// Read in the bytes
169 		int offset = 0;
170 		int numRead = 0;
171 		while (offset < bytes.length && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
172 			offset += numRead;
173 		}
174 		
175 		// Ensure all the bytes have been read in
176 		if (offset < bytes.length)
177 			throw new IOException("Could not completely read file "+file.getName());
178 		
179 		// Close the input stream and return bytes
180 		is.close();
181 		
182 		return bytes;*/
183 	}
184 
185 
186 
187 }