Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4619746

The Matcher class doesn't work with the CharBuffer class.

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.4.0
    • core-libs



      Name: nt126004 Date: 01/04/2002


      FULL PRODUCT VERSION :
      java version "1.4.0-beta3"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta3-b84)
      Java HotSpot(TM) Client VM (build 1.4.0-beta3-b84, mixed mode)

      A DESCRIPTION OF THE PROBLEM :
      The following code throw an IndexOutOfBoundsException.

      import java.util.regex.*;
      import java.io.*;
      import java.nio.*;
      import java.nio.charset.*;
      import java.nio.channels.*;

      public class CharBufferExample {
      public static void main(String[] args) throws Exception {
      // Create a pattern to match comments
      Pattern p = Pattern.compile("//.*$", Pattern.MULTILINE);

      // Get a Channel for the source file
      File f = new File("Replacement.java");
      FileInputStream fis = new FileInputStream(f);
      FileChannel fc = fis.getChannel();

      // Get a CharBuffer from the source file
      ByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, (int)fc.size());
      Charset cs = Charset.forName("8859_1");
      CharsetDecoder cd = cs.newDecoder();
      CharBuffer cb = cd.decode(bb);

      // Run some matches
      Matcher m = p.matcher(cb);
      while (m.find()) {
      System.out.println("Found comment: " + m.group());
      }
      }
      }

      But if the line 24 was changed by:

      Matcher m = p.matcher(cb.toString());

      then it works fine.

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      Exception in thread "main"
      java.lang.IndexOutOfBoundsException
              at java.nio.Buffer.checkIndex(Buffer.java:417)
              at java.nio.CharBuffer.charAt(CharBuffer.java:1108)
              at java.util.regex.Pattern$Slice.match(Pattern.java:3097)
              at java.util.regex.Pattern$Start.match(Pattern.java:2660)
              at java.util.regex.Matcher.find(Matcher.java:690)
              at java.util.regex.Matcher.find(Matcher.java:407)
              at CharBufferExample.main(CharBufferExample.java:25)

      This bug can be reproduced always.
      (Review ID: 137519)
      ======================================================================

            mmcclosksunw Michael Mccloskey (Inactive)
            nthompsosunw Nathanael Thompson (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: