Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2006

Tip: Looking for answers? Try searching our database.

MappedByteBuffer

Thread view: 
Claudio! - 20 Sep 2006 16:03 GMT
Hi!
I'm new to the java.nio but I need to use (I guess) the
MappedByteBuffer. This is the case:

In a loop i produce long values to append in a file or, if some
condition apply, I need to update a previous stored value placed
somewhere in the file (random access). I know the exact position of the
value to update as the records in the file are fixed-length (i.e. 8
byte, 1 long).

At the beginning I used RandomAccessFile to update the values, but was
terribly slow. Now I'm using the MappedByteBuffer that seems to work
well. Inside a loop I call a method that contains this code:

1. FileChannel ch = file.getChannel();
2. MappedByteBuffer mbb = ch.map(MapMode.READ_WRITE, recno * 8, 8);
4. if (present)  mbb.putLong(mbb.getLong(0) + 1);  // the update
5. else mbb.putLong(1);  // a new value appended to the file

The line 2. maps 8 byte of the file. The value of "recno" is such that
(1) or a already seen position of the file is accessed or (2) new 8
byte of the file are mapped (i. e. appended to the file and containing
the default long value 1). The flag "present" passed to the method
tells me which is the case.

Now the question: is there a better way to do this? Remeber that the
file grows in size as the loop go on, so it is impossible to map the
entire file before so I need (I guess) to map small parts of the file
at each loop (as i'm doing).

This method works (much) faster than using RandomAccessFile
readLong/writeLong directly.

Can I improve?

Thanks!!
Thomas Hawtin - 20 Sep 2006 16:17 GMT
> Now the question: is there a better way to do this? Remeber that the
> file grows in size as the loop go on, so it is impossible to map the
> entire file before so I need (I guess) to map small parts of the file
> at each loop (as i'm doing).

I'm not expert, but: MappedByteBuffer is a bit nasty. For instance,
there is no unmap (yet). I suggest either extending the file in large
chunks (this may reduce fragmentation as a side-effect) or using plain
old FileChannel.write(ByteBuffer) without mapping.

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Tim Ward - 20 Sep 2006 16:38 GMT
> I'm not expert, but: MappedByteBuffer is a bit nasty.

I found it completely unusable, as it keeps the file locked until the next
garbage collection time, which is unpredictable and could be days after
someone else really wanted to use that file.

Signature

Tim Ward
Brett Ward Limited - www.brettward.co.uk



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.