• Post Reply Bookmark Topic Watch Topic
  • New Topic

Break this logic down for me?  RSS feed

 
Marcus Hirschbine
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I would really like elaboration on what is going on in here because, though I understand the concept that is taking the data as bytes from the src file to a buffer that is X in size, (an array of bytes with 1024 elements in this case). So it pulls as much as it is allotted from the src, writes what it has, the len var is some sort of counter, once the end of the buffer is reached, it fetches the next segment of bytes, and repeats until it runs out of bytes to get from the src.

Is there a history to this method?

Any where I can get information or insight on to understanding the knowledge that this structure builds upon because it is honestly confusing me.
 
Stephan van Hulst
Saloon Keeper
Posts: 7962
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I/O operations are generally very expensive, so you want to minimize their use. Instead of doing a read and a write for every byte in the source file, it does one for every 1024 bytes, typically improving the performance a lot.

It's not really necessary to write code like this yourself. Classes like BufferedInputStream do this for you, and for copying files you can use the transferTo()/transferFrom() methods of FileChannel.
 
Alex Armenteros
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is exactly your question here? you described perfectly what is happening there.

len is the number of bytes read from the source file, that is used by the writer (FileOutputStream) to not write more bytes (which are junk) than it should.

Also thanks about that "FileChannel" class, did not know about that one.
 
Stephan van Hulst
Saloon Keeper
Posts: 7962
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alex, you're correct that Marcus explained the working of the code well. To clarify, his question was about why it's done in this way.

You might want to take a look at the java.nio package. I prefer the use of good old InputStream and OutputStream, but channels are very useful for bulk data operations.
 
Campbell Ritchie
Marshal
Posts: 56521
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Go through the Java® Tutorials and you will find a section about file input. you should find out about file channels in that tutorial.
 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That code will occasionally fail.

It's a common mistake I've seen, but the while clause should be:


If you use the current '> 0' it is possible for the reader to pick up 0 bytes on a read (for example, something else is using the disk at the time). '0' does not mean EOF, -1 does.
 
Marcus Hirschbine
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:That code will occasionally fail.

It's a common mistake I've seen, but the while clause should be:


If you use the current '> 0' it is possible for the reader to pick up 0 bytes on a read (for example, something else is using the disk at the time). '0' does not mean EOF, -1 does.


Oh thanks for this, will be sure to make amends to the practice.

Also, thanks for the feed back. Helped clear up the assembled concept I had in my head for what was going on in the code.
I suppose it was mostly confirmation I was looking for but without a doubt I'll have to keep things like that FileChannel transfer methods in mind too when revising this code.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another problem with the code is that it may leak resources if it throws an Exception. It should either use a finally block to close the Streams, or even better use a try-with-resources block.
 
Stephan van Hulst
Saloon Keeper
Posts: 7962
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I recommend avoiding reinventing the wheel. Copying a file should be done with java.nio.files.Files.copy().

In writing new applications, drop File in favor of Path.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!