cziegeler 02/03/18 23:55:34 Modified: src/java/org/apache/cocoon/util IOUtils.java Log: Always closing streams Revision Changes Path 1.6 +81 -73 xml-cocoon2/src/java/org/apache/cocoon/util/IOUtils.java Index: IOUtils.java =================================================================== RCS file: /home/cvs/xml-cocoon2/src/java/org/apache/cocoon/util/IOUtils.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- IOUtils.java 8 Mar 2002 04:15:02 -0000 1.5 +++ IOUtils.java 19 Mar 2002 07:55:34 -0000 1.6 @@ -63,86 +63,94 @@ * * @author <a href="mailto:[EMAIL PROTECTED]">Ricardo Rocha</a> * @author <a href="mailto:[EMAIL PROTECTED]">Stefano Mazzocchi</a> - * @version CVS $Id: IOUtils.java,v 1.5 2002/03/08 04:15:02 vgritsenko Exp $ + * @version CVS $Id: IOUtils.java,v 1.6 2002/03/19 07:55:34 cziegeler Exp $ */ public class IOUtils { - // ********************** - // Serialize Methods - // ********************** - - /** - * Dump a <code>String</code> to a text file. - * - * @param file The output file - * @param string The string to be dumped - * @exception IOException IO Error - */ - public static void serializeString(File file, String string) - throws IOException - { - FileWriter fw = new FileWriter(file); - fw.write(string); - fw.flush(); - fw.close(); - } - - /** - * Load a text file contents as a <code>String<code>. - * This method does not perform enconding conversions - * - * @param file The input file - * @return The file contents as a <code>String</code> - * @exception IOException IO Error - */ - public static String deserializeString(File file) - throws IOException - { - int len; - char[] chr = new char[4096]; - FileReader reader = new FileReader(file); - StringBuffer buffer = new StringBuffer(); - - while ((len = reader.read(chr)) > 0) { - buffer.append(chr, 0, len); + // ********************** + // Serialize Methods + // ********************** + + /** + * Dump a <code>String</code> to a text file. + * + * @param file The output file + * @param string The string to be dumped + * @exception IOException IO Error + */ + public static void serializeString(File file, String string) + throws IOException { + final FileWriter fw = new FileWriter(file); + try { + fw.write(string); + fw.flush(); + } finally { + if (fw != null) fw.close(); + } } - return buffer.toString(); - } + /** + * Load a text file contents as a <code>String<code>. + * This method does not perform enconding conversions + * + * @param file The input file + * @return The file contents as a <code>String</code> + * @exception IOException IO Error + */ + public static String deserializeString(File file) + throws IOException { + int len; + char[] chr = new char[4096]; + final StringBuffer buffer = new StringBuffer(); + final FileReader reader = new FileReader(file); + try { + while ((len = reader.read(chr)) > 0) { + buffer.append(chr, 0, len); + } + } finally { + if (reader != null) reader.close(); + } + return buffer.toString(); + } - /** - * This method serializes an object to an output stream. - * - * @param file The output file - * @param object The object to be serialized - * @exception IOException IOError - */ - public static void serializeObject(File file, Object object) - throws IOException - { - FileOutputStream fos = new FileOutputStream(file); - ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(fos)); - oos.writeObject(object); - oos.flush(); - fos.close(); - } + /** + * This method serializes an object to an output stream. + * + * @param file The output file + * @param object The object to be serialized + * @exception IOException IOError + */ + public static void serializeObject(File file, Object object) + throws IOException { + FileOutputStream fos = new FileOutputStream(file); + try { + ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(fos)); + oos.writeObject(object); + oos.flush(); + } finally { + if (fos != null) fos.close(); + } + } - /** - * This method deserializes an object from an input stream. - * - * @param file The input file - * @return The deserialized object - * @exception IOException IOError - */ - public static Object deserializeObject(File file) - throws IOException, ClassNotFoundException - { - FileInputStream fis = new FileInputStream(file); - ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(fis)); - Object object = ois.readObject(); - fis.close(); - return object; - } + /** + * This method deserializes an object from an input stream. + * + * @param file The input file + * @return The deserialized object + * @exception IOException IOError + */ + public static Object deserializeObject(File file) + throws IOException, ClassNotFoundException { + FileInputStream fis = new FileInputStream(file); + Object object = null; + try { + ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(fis)); + object = ois.readObject(); + } finally { + if (fis != null) fis.close(); + } + return object; + } /** * These are java keywords as specified at the following URL (sorted alphabetically).
---------------------------------------------------------------------- In case of troubles, e-mail: [EMAIL PROTECTED] To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]