ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bugzi...@apache.org
Subject [Bug 1208] New - replace tag fails on IndexOutOfBoundsException with multibytes file
Date Wed, 04 Apr 2001 05:10:17 GMT
http://nagoya.apache.org/bugzilla/show_bug.cgi?id=1208

*** shadow/1208	Tue Apr  3 22:10:17 2001
--- shadow/1208.tmp.363	Tue Apr  3 22:10:17 2001
***************
*** 0 ****
--- 1,91 ----
+ +============================================================================+
+ | replace tag fails on IndexOutOfBoundsException with multibytes file        |
+ +----------------------------------------------------------------------------+
+ |        Bug #: 1208                        Product: Ant                     |
+ |       Status: NEW                         Version: 1.3                     |
+ |   Resolution:                            Platform: All                     |
+ |     Severity: Major                    OS/Version: All                     |
+ |     Priority:                           Component: Core tasks              |
+ +----------------------------------------------------------------------------+
+ |  Assigned To: ant-dev@jakarta.apache.org                                   |
+ |  Reported By: akky@bigfoot.com                                             |
+ |      CC list: Cc:                                                          |
+ +----------------------------------------------------------------------------+
+ |          URL:                                                              |
+ +============================================================================+
+ |                              DESCRIPTION                                   |
+ Hi, firstly let me thank all of you for making and maintaining
+ this really useful tool. It's always enjoyable to explorer new
+ tasks in Ant.
+ 
+ This problem I am going to report only occurs when you are
+ involved with multibytes code so I am unsure about severity and
+ priority. Some people never encounter this.
+ 
+ With replace tag, when replacing file which has multibytes characters,
+ ant failed with stacktrace like,
+ ----------------------------------------------------------------
+ BUILD FAILED
+ 
+ java.lang.IndexOutOfBoundsException
+         at java.io.BufferedReader.read(BufferedReader.java:256)
+         at org.apache.tools.ant.taskdefs.Replace.processFile(Replace.java:293)
+         at org.apache.tools.ant.taskdefs.Replace.execute(Replace.java:201)
+         at org.apache.tools.ant.Target.execute(Target.java:153)
+         at org.apache.tools.ant.Project.runTarget(Project.java:898)
+         at org.apache.tools.ant.Project.executeTarget(Project.java:536)
+         at org.apache.tools.ant.Project.executeTargets(Project.java:510)
+         at org.apache.tools.ant.Main.runBuild(Main.java:421)
+         at org.apache.tools.ant.Main.main(Main.java:149)
+ ----------------------------------------------------------------
+ 
+ As looking at Replace#processFile(File), it seems to get a size of
+ buffer by File.length(), which is *bytes*.
+ However, BufferedReader.read() is trying to read original strings
+ as *chars* array.
+ As a result of it, if the original file has a multibyte character
+ which consists of more than one byte, File.length() can be longer
+ than the number of characters, so read() exceeds and meets this
+ IndexOutOfBoundsException.
+ 
+ My repairment suggestion is below. Use read() instead of 
+ read(char[], int, int) so you can know when input ends. And I
+ guess reading one by one doesn't harm performance because
+ BufferedReader helps.
+ ==================================================================
+ --- Replace.java_orig	Fri Mar 02 14:46:36 2001
+ +++ Replace.java	Wed Apr 04 03:58:29 2001
+ @@ -285,13 +285,17 @@
+              BufferedWriter bw = new BufferedWriter(new FileWriter(temp));
+  
+              // read the entire file into a char[]
+ -            int fileLength = (int)(src.length());
+ -            char[] tmpBuf = new char[fileLength];
+ -            int numread = 0;
+ -            int totread = 0;
+ -            while (numread != -1 && totread < fileLength) {
+ -                numread = br.read(tmpBuf,totread,fileLength);
+ -                totread += numread;
+ +            //   size of work buffer may be bigger than needed
+ +            //   when multibyte characters exist in the source file
+ +            int fileLengthInBytes = (int)(src.length());
+ +            char[] tmpBuf = new char[fileLengthInBytes];
+ +            int readChar = 0;
+ +            int gottenLength = 0;
+ +            while (true) {
+ +                readChar = br.read();
+ +                if (readChar < 0) { break; }
+ +                tmpBuf[gottenLength] = (char)readChar;
+ +                gottenLength++;
+              }
+  
+              // create a String so we can use indexOf
+ ==================================================================
+ 
+ This seems working fine with Japanese characters, not tested with
+ other multibyte characters though.
+ 
+ Regards,
+ Akky (AKIMOTO, Hiroki)
+ PFU LIMITED, Tokyo
+ mailto:akky@bigfoot.com

Mime
View raw message