This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

IO stream method's meaning  RSS feed

 
Gunta Nukka
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello sir, this is the definitions from the Book "Introduction to java programming". I am able to understand the first one but I am unable to understand the second and third one. Can please help ?


Reads the next byte of data from the input stream. The value byte is returned as an int value in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value –1 is returned.


Reads up to b.length bytes into array b from the input stream and returns the actual number of bytes read. Returns –1 at the end of the stream.


Reads bytes from the input stream and stores them in b[off], b[off+1], . . ., b[off+len-1]. The actual number of bytes read is returned. Returns –1
 
Campbell Ritchie
Sheriff
Posts: 55330
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
Which class is that in? Did you copy the examples in the book exactly, because what you showed is not standard Java® syntax. Is that from a UML diagram? It looks quite like UML.
The DataInputStream class has read methods, but you may have to look for them in the “Methods Inherited” sections. The read methods read a byte from a file or a certain number of bytes into a byte[]. They are intended for binary files, not for text files. Please read the descriptions of the read methods in the link I gave you.You will probably find other similar classes in the java.io package which have read methods too.

You will probably find the Java™ Tutorials easier to understand. There is a section about byte streams.
Please give us fuller details about the book, including its date of publication, since IO changed about 5 years ago with the introduction of the NIO2 classes.

Doesn't NIO2 sound like some poisonous chemical
 
Gunta Nukka
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome to the Ranch
. . .
Doesn't NIO2 sound like some poisonous chemical


Hi Sir, the book is telling, it is from the InputStream class. Yes sir, i copied and pasted from the book. The name of the book and the writer is " Introduction to Java Programming by Y. Daniel Liang 8th edition"
 
Tony Docherty
Bartender
Posts: 3264
81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The explanations in the book are woefully short of detail and the first one is actually misleading because it neglects to give the full documentation from the API docs which are as follows:
"Reads the next byte of data from the input stream. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown."


I suggest you read the API docs https://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html#read(byte[]) and https://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html#read(byte[],%20int,%20int) for a fuller explanation of the other two methods.

If you still don't understand them post here again explaining what you are stuck on/ensure about.
 
Campbell Ritchie
Sheriff
Posts: 55330
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please don't quote the whole of an old post; that simply makes the thread longer for no additional information. I have removed most of it.

I thought Liang's book was better than that. Are you sure you have copied the whole of the description? Did you find anything useful in the Java™ Tutorials link I gave you earlier.
 
Gunta Nukka
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:

I thought Liang's book was better than that. Are you sure you have copied the whole of the description? Did you find anything useful in the Java™ Tutorials link I gave you earlier.


Yes sir, I already said to you, I copied and pasted from Daniel sir's book. It was from page number 652 of that book. I just saw it once, Today I will fully be reading from your link and api also.
 
Gunta Nukka
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:The explanations in the book are woefully short of detail and the first one is actually misleading because it neglects to give the full documentation from the API docs which are as follows:
[

Tony sir, some explanations are good in some books, and some other topics are good in other books. I am referring to many other books also



Reads up to len bytes of data from the input stream into an array of bytes

Sir, I read this from the API you gave me. I do not understand, why it is having three arguments, and why two of them is named Len and off ?
 
Campbell Ritchie
Sheriff
Posts: 55330
157
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The byte[] array is the target whither the information will be written. The len is how many bytes you are going to read (maximum). The off parameter should be set to how many bytes you have already read into the array, so you are adding them to a vacant part rather than overwriting the array. You should find the details here. It also tells you what the return value means.

You will have to get into the habit of looking in the API documentation; it is much better if you can say, “the API says XYZ. What does that mean?” than simply saying, “What does method X do?”
 
Gunta Nukka
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote: The off parameter should be set to how many bytes you have already read into the array, so you are adding them to a vacant part rather than overwriting the array.


Hello sir, how can we set the off parameter like you mention. I am thinking like this, that the reading of bytes is happening during the execution time. So while the execution
is happening, in the middle, how can we set to how many bytes are already read into the array ?..Thanking you
 
Tony Docherty
Bartender
Posts: 3264
81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The 'off' parameter sets the index in the array at which you want to start adding the read in bytes so assuming your code was reading in chunks of bytes in a loop and started adding bytes at the beginning of the array this value would be how many bytes you have already read in at that moment in time.

The 'len' parameter sets how many bytes you want to read. Note this is the maximum number of bytes as there may not be enough bytes available to get 'len' bytes. Note you must check the methods return value to check how many bytes were actually read in.
 
Gunta Nukka
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:. . .


Thanks sir, I understood Len and Byte array, but I want to confirm more about the 'off '. From your explaining, this is what I am getting.For eg: If there is one array of size 10,
the off parameter will set one index, let us think it is index '5' for example, then 'off' will start adding the bytes from the 5th Index..is it correct sir? or wrong?.

If what I am telling to you is correct, who is passing the value 5? is it us or Automatically done by JVM or something. Because we can set the array size , and maximum value while coding.But who can set the 'off' parameter to a value(of a Index) in the middle of execution is where I am confused.


 
Campbell Ritchie
Sheriff
Posts: 55330
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gunta Nukka wrote:. . . Thanks sir . . .
On Tony's behalf: that's a pleasure.

I am not sure you have quite understood everything. You would not use a 10‑element array. Maybe a 1000000‑element array or 10²° = 1048576 elements. You might read 2¹² bytes at a time, so len is 0x0400 0x1000←[Edit: got hex number wrong.] or 4096. Every time you iterate your loop (if you use a loop at Tony suggested) you would increase the offset:- off += len;
You must pass all arguments to methods yourself; you cannot expect the JVM to calculate such things automatically. Remember that index 5 points to the 6th element of an array.

Actually: forget everything you have been told in this thread. The only thing you really need to know is what it says in the Java™ Tutorials. I gave you a link before and this is the relevant page:-
That Java™ Tutorials page wrote:CopyBytes seems like a normal program, but it actually represents a kind of low-level I/O that you should avoid.
Avoid it. You should instead use methods to read lines or to read the next int or find a particular type of datum from the files. You can find out about them in the remainder of that Java™ Tutorials “trail”, and you can avoid the read method. Other classes might use the read method but they hide it from you as an implementation detail.
 
Gunta Nukka
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Gunta Nukka wrote:. . . Thanks sir . . .
On Tony's behalf: that's a pleasure.

I am not sure you have quite understood everything. You would not use a 10‑element array. Maybe a 1000000‑.......


Good to know that Tony sir feels pleasure for answering.

Campbell sir, sorry for long gap, I was busy. I have only given that 10 element array as example, I know in Real life the array will be huge. Yes, I by mistake
called it 5th element instead of 6th. I will look in the link you gave now.
 
Gunta Nukka
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Declaring...The below program belong to Oracle company and it is not mine



Sir, as you suggest I take look at the link you given me. In Line 20 its say's ..


First we say initialize the FileInputStream variable to null , above try block. In case of

FileInputStream variable 'in' not being equal to null, is It mean that it has no more stream of bytes inside it ?
 
Henry Wong
author
Sheriff
Posts: 23275
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gunta Nukka wrote:
First we say initialize the FileInputStream variable to null , above try block. In case of

FileInputStream variable 'in' not being equal to null, is It mean that it has no more stream of bytes inside it ?


There is a difference between a reference variable, and the instance that the reference variable points to. The FileInputStream being declared and set to null (at line 8), doesn't open the file, or even have the capacity of having "bytes inside it". The instance doesn't get instantiated until line 12.

So, basically, the clean up code that you quoted, is to check if the instance exists. So, for example, something goes wrong, such as the file doesn't exist. And the finally clause is being executed as part of an exception, then you don't actually want to close something that doesn't exist (ie. you can't call the close() method when there isn't an instance).

Henry

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!