Author: echatellier Date: 2010-01-25 15:52:16 +0100 (Mon, 25 Jan 2010) New Revision: 1753 Modified: trunk/src/main/java/org/nuiton/util/FileUtil.java trunk/src/test/java/org/nuiton/util/FileUtilTest.java Log: Test another way to write file at sed method end. Modified: trunk/src/main/java/org/nuiton/util/FileUtil.java =================================================================== --- trunk/src/main/java/org/nuiton/util/FileUtil.java 2010-01-24 23:36:43 UTC (rev 1752) +++ trunk/src/main/java/org/nuiton/util/FileUtil.java 2010-01-25 14:52:16 UTC (rev 1753) @@ -43,7 +43,8 @@ import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; -import java.io.PrintStream; +import java.io.RandomAccessFile; +import java.nio.ByteBuffer; import java.nio.CharBuffer; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; @@ -1090,17 +1091,17 @@ Pattern pattern = Pattern.compile(searchRegex); - FileInputStream fis = null; + RandomAccessFile raf = 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(); + raf = new RandomAccessFile(file, "rw"); + fc = raf.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); + MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_WRITE, 0, sz); // Decode the file into a char buffer // Charset and decoder for encoding @@ -1110,33 +1111,21 @@ Matcher matcher = pattern.matcher(cb); outString = matcher.replaceAll(replace); + + // write content + // TODO is it a good way to write file ? + fc.position(0); + fc.write(ByteBuffer.wrap(outString.getBytes())); + fc.truncate(outString.length()); } finally { if (fc != null) { fc.close(); } - if (fis != null) { - fis.close(); + if (raf != null) { + raf.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(); - } - } - } } /** Modified: trunk/src/test/java/org/nuiton/util/FileUtilTest.java =================================================================== --- trunk/src/test/java/org/nuiton/util/FileUtilTest.java 2010-01-24 23:36:43 UTC (rev 1752) +++ trunk/src/test/java/org/nuiton/util/FileUtilTest.java 2010-01-25 14:52:16 UTC (rev 1753) @@ -202,5 +202,28 @@ // clean FileUtil.deleteRecursively(testDirectory); } + + /** + * Test that sed result which produce shorter file work as well. + * + * @throws IOException + */ + @Test + public void testSedComment() throws IOException { + // try to not make sed in real src dir ;) + File testDirectory = FileUtil.createTempDirectory("sed", "test"); + FileUtil.copyRecursively(new File("src").getAbsoluteFile(), testDirectory); + File testUtilFile = FileUtil.find(testDirectory, ".*FileUtil\\.java", true).get(0); + + // real method to test here : sed + FileUtil.sed(".*\\*.*", "**removed**", testUtilFile , "UTF-8"); + + List<CharSequence> lines = FileUtil.grep("/var/tmp/bidulle", testUtilFile , "UTF-8"); + Assert.assertNull(lines); + + // clean + FileUtil.deleteRecursively(testDirectory); + } + } // FileUtilTest