United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-4489356 (bf) Add asByteBuffer() method to {Double,Char,Int,...}Buffer
JDK-4489356 : (bf) Add asByteBuffer() method to {Double,Char,Int,...}Buffer

Details
Type:
Enhancement
Submit Date:
2001-08-07
Status:
Closed
Updated Date:
2002-03-22
Project Name:
JDK
Resolved Date:
2002-03-22
Component:
core-libs
OS:
generic
Sub-Component:
java.nio
CPU:
generic
Priority:
P4
Resolution:
Won't Fix
Affected Versions:
1.4.0
Fixed Versions:

Related Reports

Sub Tasks

Description

Name: rmT116609			Date: 08/07/2001


java version "1.4.0-beta"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta-b65)
Java HotSpot(TM) Client VM (build 1.4.0-beta-b65, mixed mode)


The ByteBuffer class defines methods that can create views of a given ByteBuffer such as:

CharBuffer ByteBuffer.asCharBuffer()
DoubleBuffer ByteBuffer.asDoubleBuffer()
LongBuffer ByteBuffer.asLongBuffer()
... etc.

Why is there no corresponding method in the view buffer classes to create a ByteBuffer view?

ByteBuffer CharBuffer.asByteBuffer()
ByteBuffer DoubleBuffer.asByteBuffer()
ByteBuffer LongBuffer.asByteBuffer()
... etc.

The nio class Channel only has methods for IO using ByteBuffer so the only way I can see to 
write a DoubleBuffer is to 'copy' it into a ByteBuffer. I can see no utility in the method

DoubleBuffer.wrap(double[] array)

if I cannot write the newly created DoubleBuffer.

Ideally I would like to create a ByteBuffer view of a DoubleBuffer and/or be
able to write a DoubleBuffer directly via a Channel method. Another possiblity
would be additonal wrap methods in ByteBuffer such as:

ByteBuffer ByteBuffer.wrap(char[] array)
ByteBuffer ByteBuffer.wrap(double[] array)
ByteBuffer ByteBuffer.wrap(long[] array)
... etc.

I am developing an application that must process very large amounts of both
numerical and text data. Because this data will not all fit in memory I must
stage this data in TEMPORARY files while I process it. This requires that I
write and reread this data during processing (potentially many times). I
feel it is unnecessary to add the additional overhead of byteswapping for
numerical data or Unicode encoding/decoding for text data for a temporary
file read/write. For instance: I see no way using nio to write a Unicode
string to disk without incurring substantial overhead. I can create a
CharBuffer by wrapping a String but I cannot write a CharBuffer. I could use
CharsetDecoder to decode the String into a ByteBuffer, write it, and then
read it back using CharsetEncoder but the overhead is substantial for use in
a temporary storage situation. The same holds true for other primitive
types. I can create a DoubleBuffer by wrapping a double[] but I cannot write
a DoubleBuffer, ... etc. I propose a simple solution, add the following
static methods to ByteBuffer:

ByteBuffer ByteBuffer.wrap(String)
ByteBuffer ByteBuffer.wrap(char[])
ByteBuffer ByteBuffer.wrap(double[])
ByteBuffer ByteBuffer.wrap(float[])
ByteBuffer ByteBuffer.wrap(long[])
ByteBuffer ByteBuffer.wrap(int[])
ByteBuffer ByteBuffer.wrap(short[])


(Review ID: 129561) 
======================================================================

                                    

Comments
EVALUATION

ByteBuffer views of CharBuffer, DoubleBuffer, LongBuffer, etc., objects would
not be particularly useful from an efficiency standpoint.  All I/O operations
are ultimately performed only upon direct byte buffers.  If you pass a
non-direct byte buffer to a channel read (or write) operation then the bytes
are copied from (to) an internal byte buffer after (before) the actual read
(write) operation is performed.  Adding asByteBuffer() methods to the non-byte
buffer classes would not obviate the need for this intermediate copy.

You can easily implement your temporary data store by using views in the other
direction, i.e., by using char, double, etc., views of ByteBuffers.  Given a
ByteBuffer you can, e.g., deposit a string into it this way:

    ByteBuffer bb = ByteBuffer.allocateDirect(SIZE);
    CharBuffer cv = bb.asCharBuffer();
    cv.put("String");

There's no need to use encoders or decoders here -- the characters are written
to the byte buffer in big-endian order.

-- ###@###.### 2002/3/22
                                     
188-10-03 0



Hardware and Software, Engineered to Work Together