A colleague posed a question to me and I came up with an answer, but I'm not so sure I think it is a great answer, nor necessarily correct. I'll ask all of you this question. As this question is specifically about the InputStream and BufferedInputStream classes, I thought that those of you after your certification might want to chew on this one a bit. Here's the situtation. We have an InputStream object and we'd like to read the contents of it in a single call to a read method. I suggest that we do something like this:
Now, I have a few questions about this. Will this always read the entire contents of the stream and store them in b, or is there a limit? How does the efficiency of performing an operation like this compare to putting a call to read(), which reads a single byte, in a while loop? Thanks, Corey
Please consult the API. The method you wrote calls repeatedly the basic read(int). So the performance is roughly the same. available() method is very tricky: on a file returns the length of the file, but on a socket connection surely it wont return the total length of the data sent (disjointly) by the other end. Please try the method readFully on DataInput and tell us if allways fills in the array in contrast with the above method.
Sorry Jose, I looked at the API, and I still don't know the answer. readFully has two forms. One involves the length of the file, but the other just "reads some bytes". I can see how the length version might answer reading the file in a single pass. I don't see how this will work with a socket. There is an increasing pattern in this group not to answer questions, but rather to point to documentation or ask the person asking the question, "What do you think?" If I knew the answer, I wouldn't be posting. If you know the answer, please help me. If you know the answer and won't tell me, don't post. Pres
There is an increasing pattern in this group not to answer questions, but rather to point to documentation or ask the person asking the question, "What do you think?"
I think Jose's approach is more than fair. Answering a question right away is worthless from the studying standpoint. Pres, maybe *you* think before posting but lots of people don't. They hit the wall, don't think at all and just post a question like "What does this code do?". If you tell them the answer right away, they haven't learned anything. My opinion is that offering an answer on a golden plate is useless, one has to think before posting and when I say *think* I mean *think very hard* not just for about two minutes. Nothing is granted and SCJP is a personal challenge not a world cup. Well, I don't want to patronize but I felt the need to tell things. Personally, I like that approach and I do know that lots of other bartenders and sheriffs will do the same. You people are adult subjects now, you have a brain, you use it and we help you! [ April 10, 2002: Message edited by: Valentin Crettaz ]
Sidebar: Sometimes I can appear very formal, but it is because my English is like that. I haven't spend a period of time living in any English speaking country. And I don't know informal expressions. I would like to recommend several thinks: Read as much as you can. Especially consult the JLS (authoritative and free). The API is needed before making a program. I have found it very much informational. Much more than anyone can judge at first. Please before posting try the code. Many questions can be avoided doing so. That is because I feel that giving URLs to where great resources are placed, is the correct way. Anyway I have always tried to answer question in my posts(this one included). But sometimes a bit of precaution is needed because I don't know the aswer for certain. Also because of misunderestandings etc.
Now the question: A reading of the API tell us that read(byte,int,int) doesn't ensure the filling of the array. However readFully it does, if no Exception or end of the stream occurs. I had a bit of precaution in my previous post because, sometimes, in very few occations, the API is wrong. I feel anybody should check a technical risk with a little program before doing a greater program (prototyping) . This question IMHO is cleared out following my two previous recommedations: reading API and doing a little program. If there happens to be any problem in any of these previous steps, Ranchers are always happy to try and help.
Joined: Jan 18, 2001
I know that much effort goes into this group, and that it volunteer. Thanks all for your efforts. The answer to every question ever asked to this group is "Check the documentation. Your answer is there." You could just make this into an autoreply and close the doors. I understand that noone offering advice will change their pattern of doing so. As an advice receiver, however, I feel that I have a right to throw an exception when the advice I get passed is a null. If I've read the documentation and been confused by it, telling me to read it again won't help. Write what you want. If you want to help, then perhaps you should listen to what the people asking for help want to get. Your personal opinions on learning theory might be tempered by the needs of those of us that are preparing for the test. Just a thought. Pres
Joined: Jul 03, 2001
The answer to every question ever asked to this group is "Check the documentation. Your answer is there." You could just make this into an autoreply and close the doors.
This is very unfair. I have spend many hours trying code for posting here (Well obviously to learn more as well) . Also, it is not true. Many anwers tells you where to learn more, but in almost all the threads there are post that are a serious attemp to reply well.
I feel that I have a right to throw an exception when the advice I get passed is a null.
You must do it. However have you seen any message with 0 replies by here? Don't you agree that most of the questions are properly aswered?
If I've read the documentation and been confused by it, telling me to read it again won't help.
Nobody has done that. We also add the answer.
then perhaps you should listen to what the people asking for help want to get. Your personal opinions on learning theory might be tempered by the needs of those of us that are preparing for the test.
I feel you are right here. I think The Ranch is the best forum in the net for certification. I have already seen this opinion post by others.
Joined: Aug 26, 2001
Ok the discussion is disgressing. I'll add something as a final note, but then we might let Corey get some answers to his problem and move this discussion to the JavaRanch forum if someone feels the need to. Pres, One thing that is really not self-evident for the answer providers is to know when people are really putting a good deal of effort into their studies. And apparently you are a good student. So I must apologize for having been rude but take my place for a moment and you'll see what I mean. The set of people who think really hard before posting a question is really small. Now, you are on my "good student" list and when you post a question I'll know it is because you had no options left and not because you are lazy. No hard feelings [ April 10, 2002: Message edited by: Valentin Crettaz ]
Originally posted by Corey McGlone: Now, I have a few questions about this. Will this always read the entire contents of the stream and store them in b, or is there a limit? How does the efficiency of performing an operation like this compare to putting a call to read(), which reads a single byte, in a while loop?
Corey, I don't know the answer to your question, but it seems to me that since you are asking about efficiency and performance of I/O, you would probably stand a better chance of getting a good answer in another forum rather than this one -- perhaps the performance forum or possibly I/O and Streams forum.
JavaBeginnersFaq "Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt