United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-4782970 : DirectByteBuffer.put(ByteBuffer src) fails when source is non-direct, read-only

Details
Type:
Bug
Submit Date:
2002-11-21
Status:
Resolved
Updated Date:
2002-12-17
Project Name:
JDK
Resolved Date:
2002-12-17
Component:
core-libs
OS:
windows_2000
Sub-Component:
java.nio
CPU:
x86
Priority:
P3
Resolution:
Fixed
Affected Versions:
1.4.1
Fixed Versions:
1.4.2 (b11)

Related Reports
Relates:

Sub Tasks

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


                                     
2004-06-14
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
                                     
175-10-12 0
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
                                     
174-10-12 0



Hardware and Software, Engineered to Work Together