How come 'FileChannel.map()' returns a DirectByteBufferR ?...

Discussion in 'Java' started by Spendius, Jul 1, 2003.

  1. Spendius

    Spendius Guest

    .... that has no underlying ("backing" as they say in the doc)
    array ? And how come this java.nio.DirectByteBufferR class
    is not documented (does not appear in the doc, but can be
    found in the src.zip file) ?!?

    It's pretty annoying actually, I'd really like to be able to
    play with the underlying array of a reference returned by
    FileChannel.map() and be able to play with its whole contents...

    Thanks for you enlightenments...
    Regards,
    Spendius
     
    Spendius, Jul 1, 2003
    #1
    1. Advertising

  2. Spendius wrote:
    > ... that has no underlying ("backing" as they say in the doc)
    > array ?


    Why shouldn't it? The spec guarantees that the object you get is a
    MappedByteBuffer. Because MappedByteBuffers are always direct, Java
    uses the native I/O system to manipulate the buffer data wherever
    possible. It is conceivable that an implementation might find a way to
    satisfy that requirement while still providing a backing array, but at
    the moment I can't think how it might be done.

    > And how come this java.nio.DirectByteBufferR class
    > is not documented (does not appear in the doc, but can be
    > found in the src.zip file) ?!?


    Presumably because it is package-private in java.nio. The documentation
    does not promise to give you an instance of a class that appears in the
    public API. Indeed, you will find many, many cases where the same kind
    of thing is done elsewhere in the platform API.

    > It's pretty annoying actually, I'd really like to be able to
    > play with the underlying array of a reference returned by
    > FileChannel.map() and be able to play with its whole contents...


    There is no underlying array, at least not in the Java sense. That's
    the complaint you began with. I don't understand why you are annoyed;
    the behavior you describe complies fully with the API documentation. Do
    not make assumptions about the platform API that are not backed by the
    API docs, otherwise your software is likely to break, somewhere, sometime.

    If you can't do what you want with the facilities that Java provides
    then use a lower-level language. If you are adventuresome and also just
    can't live without Java then delve into JNI.


    John Bollinger
     
    John C. Bollinger, Jul 1, 2003
    #2
    1. Advertising

  3. Spendius

    Harald Hein Guest

    "Spendius" wrote:

    > ... that has no underlying ("backing" as they say in the doc)
    > array ?


    That's the whole idea of memory-mapped IO. Data from the disk is
    directly mapped into the system's virtual memory via the MMU, and
    transfered via the low-level disk driver and DMA to/from physical
    memory when needed (when the MMU detects a page-miss). This reduces the
    overhead for reading/writing disk data as much as possible.

    HH
     
    Harald Hein, Jul 1, 2003
    #3
  4. Spendius

    Spendius Guest

    Anyway I found out a way to get a *copy* (if I'm not
    mistaken) of the underlying array with the following:
    # ByteBuffer rMap = rCh.map(FileChannel.MapMode.READ_ONLY, 0, x);
    # byte[] rArray = new byte[(int)x];
    # rMap.get(rArray);
    now 'rArray' is filled with the contents of my file and
    I can process it as I like it...

    Harald Hein <> wrote...
    > "Spendius" wrote:
    >
    > > ... that has no underlying ("backing" as they say in the doc)
    > > array ?

    >
    > That's the whole idea of memory-mapped IO. Data from the disk is
    > directly mapped into the system's virtual memory via the MMU, and
    > transfered via the low-level disk driver and DMA to/from physical
    > memory when needed (when the MMU detects a page-miss). This reduces the
    > overhead for reading/writing disk data as much as possible.
    >
    > HH
     
    Spendius, Jul 3, 2003
    #4
  5. Spendius

    Harald Hein Guest

    "Spendius" wrote:

    > Anyway I found out a way to get a *copy* (if I'm not
    > mistaken)


    So you throw away all the advantage of memory-mapped I/O. What you are
    doing doesn't make much sense.

    HH
     
    Harald Hein, Jul 4, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roedy Green
    Replies:
    3
    Views:
    3,260
    Roedy Green
    Aug 14, 2003
  2. Spendius
    Replies:
    0
    Views:
    421
    Spendius
    Sep 7, 2003
  3. Christopher Dean

    RandomAccessFile vs. FileChannel

    Christopher Dean, Nov 18, 2003, in forum: Java
    Replies:
    3
    Views:
    1,201
    Michael Borgwardt
    Nov 19, 2003
  4. Andrew Rollings
    Replies:
    1
    Views:
    2,285
    John C. Bollinger
    Feb 2, 2004
  5. Paul J. Lucas
    Replies:
    12
    Views:
    1,497
    Paul J. Lucas
    Aug 19, 2005
Loading...

Share This Page