Author: echatellier Date: 2010-01-15 13:07:48 +0100 (Fri, 15 Jan 2010) New Revision: 1741 Modified: trunk/src/main/java/org/nuiton/util/FileUtil.java Log: Close channel before writing output file and make sure that channel and stream are closed. Modified: trunk/src/main/java/org/nuiton/util/FileUtil.java =================================================================== --- trunk/src/main/java/org/nuiton/util/FileUtil.java 2010-01-14 17:43:52 UTC (rev 1740) +++ trunk/src/main/java/org/nuiton/util/FileUtil.java 2010-01-15 12:07:48 UTC (rev 1741) @@ -1000,26 +1000,39 @@ * @since 1.1.2 */ public static List<CharSequence> grep(String searchRegex, File f, String encoding) throws IOException { - - // Open the file and then get a channel from the stream - FileInputStream fis = new FileInputStream(f); - FileChannel fc = fis.getChannel(); - // Get the file's size and then map it into memory - int sz = (int)fc.size(); - MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, sz); + List<CharSequence> lines = null; - // Decode the file into a char buffer - Charset charset = Charset.forName(encoding); - CharsetDecoder decoder = charset.newDecoder(); - CharBuffer cb = decoder.decode(bb); + FileInputStream fis = null; + FileChannel fc = null; - // Perform the search - List<CharSequence> lines = grep(searchRegex, cb); + try { + // Open the file and then get a channel from the stream + fis = new FileInputStream(f); + fc = fis.getChannel(); - // Close the channel and the stream - fc.close(); - + // Get the file's size and then map it into memory + int sz = (int)fc.size(); + MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, sz); + + // Decode the file into a char buffer + Charset charset = Charset.forName(encoding); + CharsetDecoder decoder = charset.newDecoder(); + CharBuffer cb = decoder.decode(bb); + + // Perform the search + lines = grep(searchRegex, cb); + } + finally { + // Close the channel and the stream + if (fc != null) { + fc.close(); + } + if (fis != null) { + fis.close(); + } + } + return lines; } @@ -1077,28 +1090,53 @@ Pattern pattern = Pattern.compile(searchRegex); - // Open the file and then get a channel from the stream - FileInputStream fis = new FileInputStream(file); - FileChannel fc = fis.getChannel(); + FileInputStream fis = null; + FileChannel fc = null; + String outString = null; + try { + // Open the file and then get a channel from the stream + fis = new FileInputStream(file); + fc = fis.getChannel(); - // Get the file's size and then map it into memory - int sz = (int)fc.size(); - MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, sz); + // Get the file's size and then map it into memory + int sz = (int)fc.size(); + MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, sz); - // Decode the file into a char buffer - // Charset and decoder for encoding - Charset charset = Charset.forName(encoding); - CharsetDecoder decoder = charset.newDecoder(); - CharBuffer cb = decoder.decode(bb); + // Decode the file into a char buffer + // Charset and decoder for encoding + Charset charset = Charset.forName(encoding); + CharsetDecoder decoder = charset.newDecoder(); + CharBuffer cb = decoder.decode(bb); - Matcher matcher = pattern.matcher(cb); - String outString = matcher.replaceAll(replace); + Matcher matcher = pattern.matcher(cb); + outString = matcher.replaceAll(replace); + } + finally { + if (fc != null) { + fc.close(); + } + if (fis != null) { + fis.close(); + } + } - FileOutputStream fos = new FileOutputStream(file.getAbsolutePath()); - PrintStream ps = new PrintStream(fos); - ps.print(outString); - ps.close(); - fos.close(); + if (outString != null) { + FileOutputStream fos = null; + PrintStream ps = null; + try { + fos = new FileOutputStream(file.getAbsolutePath()); + ps = new PrintStream(fos); + ps.print(outString); + } + finally { + if (ps != null) { + ps.close(); + } + if (fos != null) { + fos.close(); + } + } + } } /** @@ -1127,6 +1165,7 @@ * @param fileRegex regex for file to find in current dir * @param encoding encoding to use * @throws IOException + * @since 1.1.2 */ public static void sed(String searchRegex, String replace, String fileRegex, String encoding) throws IOException { sed(searchRegex, replace, new File("."), fileRegex, encoding);
participants (1)
-
echatellier@users.nuiton.org