• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

FBNS: convert readUTF to NIO equivalent

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 329
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
------------------------------------------------
 
Ranch Hand
Posts: 783
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
someone who pass the exam using NIO can tell us....
 
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,

The situation with NIO is explained in this post.

Regards, Andrew
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic