JDK-4782970 : DirectByteBuffer.put(ByteBuffer src) fails when source is non-direct, read-only
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.nio
  • Affected Version: 1.4.1
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: windows_2000
  • CPU: x86
  • Submitted: 2002-11-21
  • Updated: 2002-12-17
  • Resolved: 2002-12-17
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
1.4.2 b11Fixed
Related Reports
Relates :  
Description

Name: rmT116609			Date: 11/21/2002


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

FULL OPERATING SYSTEM VERSION :
Microsoft Windows 2000 [Version 5.00.2195]

A DESCRIPTION OF THE PROBLEM :
DirectByteBuffer.put(ByteBuffer src) throws java.nio.ReadOnlyBufferException when the SRC parameter is a Read only ByteBuffer and NOT DirectByteBuffer
This is happend because the put method makes a call to the src.array() method without checking if its a ReadOnlyBuffer

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
See code provided

EXPECTED VERSUS ACTUAL BEHAVIOR :
Expected: Not to throw exception
Result: Throw java.nio.ReadOnlyBufferException

ERROR MESSAGES/STACK TRACES THAT OCCUR :
Exception in thread "main" java.nio.ReadOnlyBufferException
        at java.nio.ByteBuffer.array(ByteBuffer.java:900)
        at java.nio.DirectByteBuffer.put(DirectByteBuffer.java:277)
        at DirectByteBufferPutTest.main(DirectByteBufferPutTest.java:10)

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.nio.*;
public class DirectByteBufferPutTest {

	public static void main (String[] args) {
		
		ByteBuffer bufferA = ByteBuffer.allocate(10);
		ByteBuffer bufferB = ByteBuffer.allocateDirect(10);
		bufferA.putInt(10);
		ByteBuffer readOnly = bufferA.asReadOnlyBuffer();
		bufferB.put(readOnly);
	}
}
---------- END SOURCE ----------

Release Regression From : 1.4.0_03
The above release value was the last known release where this 
bug was known to work. Since then there has been a regression.

(Review ID: 167271) 
======================================================================

Comments
CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: mantis-beta FIXED IN: mantis-beta INTEGRATED IN: mantis-b11 mantis-beta
14-06-2004

EVALUATION The submitter is correct. This problem was introduced by the hopper fix for bug 4676548 - Buffer-put operations too slow when direct and non-direct buffers are mixed. -- iag@sfbay 2002-11-21 Regression from 1.4.0. Should fix in 1.4.2. -- ###@###.### 2002/12/2 The problem was in the bulk put method defined in Direct-X-Buffer.java, which was using src.array() and src.arrayOffset() to access the backing array of a source heap buffer. The fix is to use the fields behind these accessors, thereby bypassing the read-only checks. This is completely safe since we're only copying from the source buffer, not writing to it. I've also verified that the performance of the new code is in line with the other cases, using a variant of the BufferPutSpeed test from 4676548. -- ###@###.### 2002/12/9
12-10-0175

WORK AROUND Copy the source buffer to a writable heap buffer and then put that buffer into the direct buffer. static void put(ByteBuffer src, ByteBuffer dst) { ByteBuffer tmp = ByteBuffer.allocate(src.remaining()); tmp.put(src); tmp.rewind(); dst.put(tmp); } -- ###@###.### 2002/12/8
12-10-0174