Error scenario: A ByteBuffer has its mark currently set at position 0. ByteBuffer.duplicate is used to create a duplicate ByteBuffer from the original. If the reset method is called on the duplicate ByteBuffer, it throws InvalidMarkException, implying that the mark was not set for the duplicate ByteBuffer. Note that this problem only seems to occur if the mark was set for position 0 when the original ByteBuffer was duplicated. This was tested with every version of Java 1.4 that I could find with the same results. This includes using build 1.4.2_07-b05. The problem could also be reproduced for 5.0 and 6.0, on both Windows and Solaris; no attempts were made to test on other OS's but the problem is probably OS-independent. // ******************** START JAVA TEST CODE ******************** /** * Class to test using ByteBuffers. */ import java.nio.*; class ByteBufferTest { public static void main(String args[]) { int setPosition = 0; ByteBuffer buf = ByteBuffer.allocateDirect(10); if (args.length > 0) try {setPosition = Integer.parseInt(args[0]);} catch (Exception e) {} System.out.println("Initializing position and mark to " + setPosition); buf.position(setPosition); buf.mark(); ByteBuffer dup = buf.duplicate(); try { buf.reset(); System.out.println("Reset worked correctly for original buffer."); } catch (InvalidMarkException e) { System.out.println("Reset failed for original buffer."); } try { dup.reset(); System.out.println("Reset worked correctly for duplicate buffer."); } catch (InvalidMarkException e) { System.out.println("Reset failed for duplicate buffer."); } System.out.println(); } } // ******************** END JAVA TEST CODE ******************** REM ******************** START TEST BAT FILE ******************** @echo off set JAVA_HOME=C:\j2sdk1.4.2_07 echo Compiling ... %JAVA_HOME%\bin\javac ByteBufferTest.java echo. echo Running ... %JAVA_HOME%\jre\bin\java -cp . ByteBufferTest 0 %JAVA_HOME%\jre\bin\java -cp . ByteBufferTest 1 %JAVA_HOME%\jre\bin\java -cp . ByteBufferTest 2 pause REM ******************** END TEST BAT FILE ******************** When running the test bat file, the following output is produced: Compiling ... Running ... Initializing position and mark to 0 Reset worked correctly for original buffer. Reset failed for duplicate buffer. Initializing position and mark to 1 Reset worked correctly for original buffer. Reset worked correctly for duplicate buffer. Initializing position and mark to 2 Reset worked correctly for original buffer. Reset worked correctly for duplicate buffer. Press any key to continue . . . ###@###.### 2005-2-22 05:31:11 GMT
|