Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is NIO API really worth a try?

 
Filipe Fedalto
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I don't mean to state that NIO is or isn't better than traditional IO. I am asking it regarding the SCJD choices path that one can make.

So, I started my project (URLyBird, for instance) using traditional IO. So far so good. However, I am somehow intrigued with the possibility that there could be some benefits in New IO that I could be missing. Most important, that these left-out benefits could cost me some points.

Could that happen? Is there some invaluable benefit (as to the SCJD specification) that could only be accomplished with NIO?

I am profficient with traditional IO and this would be my choice. However, as you may see, I am not so good at NIO. Therefore this uncertainty is ringing some chimes and raising some doubts as to whether I could be losing something.

What could you say about it?

Thank you all in advance.

Filipe Fedalto
Cafe Javanes - www.cafejavanes.com.br
 
peter wooster
Ranch Hand
Posts: 1033
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Filipe Fedalto:
Hi all,

I don't mean to state that NIO is or isn't better than traditional IO. I am asking it regarding the SCJD choices path that one can make.

So, I started my project (URLyBird, for instance) using traditional IO. So far so good. However, I am somehow intrigued with the possibility that there could be some benefits in New IO that I could be missing. Most important, that these left-out benefits could cost me some points.

Could that happen? Is there some invaluable benefit (as to the SCJD specification) that could only be accomplished with NIO?

I am profficient with traditional IO and this would be my choice. However, as you may see, I am not so good at NIO. Therefore this uncertainty is ringing some chimes and raising some doubts as to whether I could be losing something.

What could you say about it?

Thank you all in advance.

Filipe Fedalto
Cafe Javanes - www.cafejavanes.com.br

If you have recently started the project NIO is forbidden according to the instructions on Suned's web page. That said, NIO is very useful for networking and provides some additional benefits when used for files. I'm quite familiar with NIO SocketChannels and would use them if they were allowed and if there was a simple way to use them to transfer serialized objects. Since they are not stream oriented, the standard ObjectOutputStream can't be used. You can use streams over NIO, but then you lose the biggest benefit, non-blocking reads.

I suspect that NIO will be a choice, or may even be required on the Java5 version on SCJD. The other problem with NIO, that may explain the recent prohibition, is that in early versions of 1.4 NIO had a couple of show stopper bugs. These are fixed in 1.4.2_05 and 1.5 beta.
 
Eben Hewitt
Author
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Filipe

Unless the instructions you received in your assignment download explicitly state otherwise, you _are_ free to use NIO for reading and writing your data file.

Peter is correct that there is a statement on Sun Ed's web page (at http://suned.sun.com/AU/catalog/courses/AU-CX-310-252A.html) that NIO is not allowed. But that is a general statement, is inconsistent with other communications from Sun (such as some instructions that come with the assignment), and since it is not necessary to have read that page on Sun's site before taking the test, they can't really hold you to that. And therefore, Sun doesn't hold you to that.

I agree with Peter that it is probably not a good idea to use NIO for the networking component.

However, NIO for reading and writing the data file on the server side was not explicitly forbidden in my instructions, and so I did use NIO for fast reading and writing, had a lovely time doing so, and got full credit on the data and server sections where it was used.

It depends on what your aims are in doing the SCJD. If you want to use it as an opportunity to learn about NIO, and have some time, that is probably not a bad idea. If you don't want to deal with another unknown in an important aspect of your project, blow it off.

But make sure you check your instructions if you think you might want to go that direction. I had Bodgitt and Scarper, which is newer.

I feel pretty comfortable asserting that you will defintely not _lose_ points for using traditional IO.
 
Filipe Fedalto
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Eben,

This is exactly what I expected. I was somehow confused with the Not-Tos in the suned web site. Especially because my assignment doesn't state anything forbidding NIO.

Also, as I am yet learning NIO, I wondered if it was or wasn't so good an idea to use it in my assignment.

One thing I've decided and I am happy to see that you took the same approach: not to use NIO in the network layer, but only use it in the data access layer.

One annoying thing I am experiencing while still implementing the data access layer is that it would just be so easy if we had C-like struts in Java. As we don't, it is annoying to first read raw bytes just to sort then into the data structures. The readXXX methods in the IO classes just doesn't do it, they are too simple. So I've got this feeling that I'm reinventing the wheel.

NIO seemed to me to have something in the Buffer classes that could more directly map raw bytes into the data structures, but again, I'm not sure so far, as I don't know that much of NIO.

What I really wanted was something like that (in C)

struct data {
int len;
char[40] name;
char[10] phone;
char[50] addr;
int age;
};

then read all 164 bytes directly into a variable the type of this structure.
Something like that.

What do you think? Is there such a thing in NIO?


Thank you in advance,

Filipe Fedalto
www.cafejavanes.com.br
Brasil
 
Filipe Fedalto
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And yes, I am willing to use my SCJD assignment to learn.

I know WEB, JSP, Servlets, EJB and stuff, but I'm not very much pleased at server programming.

What really pelases is Swing, Networking, IO, Streams, Sockets, Java 2D, JMF and other desktop technologies, such as game development.

Therefore, I am definetely glad to use my SCJD assignment to learn other not-so-likeable technologies, such as RMI or even this NIO stuff.

Thank you again,

Filipe Fedalto
 
Julian Kennedy
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ha! Nice one Filipe! You'll be wanting to use unions next! I do occasionally miss some of the old C constructs. On the plus side we're getting enums, varargs and printf-like output formatting with Tiger (Java 5) so I don't think I'm alone.

I'm not aware of any convenient way of doing what you want. I had a brief go at implementing a struct-like class with a constructor that accepted an array of bytes but came completely unstuck! I think that may have been a little naive. :roll:

I think the key words on my assignment are (paraphrasing):
You may develop your own code using any [production] implementation of the Java 2 platform ... [that has not] been superseded by a new production version for more than 18 months by the time [of] your submission.


Anyway, on with the assignment (mine's B&S). Have fun.

Jules
 
peter wooster
Ranch Hand
Posts: 1033
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Julian Kennedy:
Ha! Nice one Filipe! You'll be wanting to use unions next! I do occasionally miss some of the old C constructs. On the plus side we're getting enums, varargs and printf-like output formatting with Tiger (Java 5) so I don't think I'm alone.

I'm not aware of any convenient way of doing what you want. I had a brief go at implementing a struct-like class with a constructor that accepted an array of bytes but came completely unstuck! I think that may have been a little naive. :roll:

I think the key words on my assignment are (paraphrasing):

Anyway, on with the assignment (mine's B&S). Have fun.

Jules


There is a VERY GOOD REASON that structs, pointers and unions are not available. That is that no two environments have the same implementation of primitive data types. Some are big-endian, some little-endian, there once were systems with 6 bit bytes and 9 bit bytes, and there will be systems with 128 bit integers (the US government will need them to measure its debt). Java provides a uniform defintion of this and requires the equivalent of getters and setters to access it.
 
Filipe Fedalto
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

It's just like you stated, Peter Wooster. Not having a standard and unique integer-precision definition valid for all platforms is a very good reason not to have these C-like constructs at all.

In C and CPP, that said.

In Java, where a 32-bit integer will always be a 32-bit integer, regardless of platform, such constructs would show themselves invaluable.

Nevertheless, I am only wondering about what-ifs. Let's just stick to reality.

Thank you all, anyways. This is exactly what I expected by starting this discussion.

Filipe Fedalto
Cafe' Javanes
www.cafejavanes.com.br
Brasil
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic