• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

FBNS: convert readUTF to NIO equivalent

 
Ian Wark
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.

I'm considering changing my file access code to accomodate FileChannels and ByteBuffers, but am having a bit of trouble figuring out how to convert the readUTF method to its NIO equivalent. It doesn't seem very efficient.

ByteBuffer bb = ......
(read info into bb)

//Here reading the first unsigned short value of the UTF that tells
//me how many encoded bytes to get and using this number to create a
//byte array of exactly that size
byte[] buffer = new byte[bb.getShort()];

//Read info from file into the buffer, the size of buffer
bb.get(buffer);

//then right after this instead of db.readUTF() do

new String(buffer, "UTF-8")

Will this work? If it does, is there a better way of doing this?
 
Ken Boyd
Ranch Hand
Posts: 329
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not using NIO as SUN site mention but lot of discussion regarding that already took place.

------------------------------------------------
The following APIs and facilities may not be used:

* Enterprise JavaBeans
* Servlets, JSP technology, or any other web-oriented APIs
* NIO, the New IO facilities
* Java DataBase Connectivity (JDBC) and SQL
* Java IDL API and CORBA
* Third party software libraries or tools (such as browsers)
------------------------------------------------
 
Paul Bourdeaux
Ranch Hand
Posts: 783
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not familiar with NIO, but I just wanted to add the reminder that you can use NIO in your assignment regardless of what it says on the website - as long as it is not explicitl forbidden in your instructions. (Do a search on NIO in this forum for an explanation). Just to be safe, if you are going to use NIO, I would email Sun and make sure for yourself.
 
Ian Wark
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for that. I was not aware of that rule.

It does not mention that in the project directive, and NIO is part of the core Java API. I just assumed it was OK becuase Habibi's book also goes into lots of detail on it.

Ho hum.
 
Paul Bourdeaux
Ranch Hand
Posts: 783
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just assumed it was OK
It is.
 
Ken Boyd
Ranch Hand
Posts: 329
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
someone who pass the exam using NIO can tell us....
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12007
215
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

The situation with NIO is explained in this post.

Regards, Andrew
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic