-2

I'm currently picking up on Java IO functions and coding but am a little confused while reading through online tutorials. This is in reference to the question posted here: InputStream/OutputStream read()/write() function relevance and usage

This seemed to hint that the difference between a basic write() function and the write(byte[] bytes, int offset, int length) function is in its time efficiency, but i didn't quite get the meaning of that.

In the tutorial, it was stated:

public int read(byte[] bytes, int offset, int length) throws IOException
// Read "length" number of bytes, store in bytes array starting from offset 
of index.

public int read(byte[] bytes) throws IOException
// Same as read(bytes, 0, bytes.length)

What exactly do these two lines of code do to illustrate what read() does in java IO? So does the first line read the length of the file's info OR the file's actual info itself.

To pile on more confusion, the Write() function to OutputStream was explained as follows:

"Similar to the input counterpart, the abstract superclass OutputStream declares an abstract method write() to write a data-byte to the output sink. write() takes an int. The least-significant byte of the int argument is written out; the upper 3 bytes are discarded. It throws an IOException if I/O error occurs (e.g., output stream has been closed)."

Does this mean the actual info is written in or the argument? kinda confused what the paragraph was trying to say.

public void abstract void write(int unsignedByte) throws IOException\

public void write(byte[] bytes, int offset, int length) throws IOException
// Write "length" number of bytes, from the bytes array starting from offset 
of index.

public void write(byte[] bytes) throws IOException
// Same as write(bytes, 0, bytes.length)

Thanks in advance for any explanation on this.

Mack
  • 117
  • 2
  • 9

1 Answers1

0

This seemed to hint that the difference between a basic write() function and the write(byte[] bytes, int offset, int length) function is in its time efficiency, but i didn't quite get the meaning of that.

The "basic" write(b) method writes just a single byte of data.

If you wanted to use it to write large amounts of data, you would be making a lot of method calls.

That is why for efficiency's sake, there are also versions of write that can take many bytes in a single call. This works by passing in those bytes in an array (the "buffer").

In addition to reducing number of calls, efficient use (and re-use) of this buffer also saves a lot of work. But this makes the code a bit more complicated to use: Instead of just receiving results for method calls, you have to manage these byte arrays.

The same applies for the read methods.


public int read(byte[] bytes, int offset, int length) throws IOException

// Read "length" number of bytes, store in bytes array starting from offset of index.

So does the first line read the length of the file's info OR the file's actual info itself ?

  • The method reads up to length bytes from the file (it may read less, for example when the file is too short)
  • The data that it reads is not returned from the method, it is placed into the array bytes. This is too avoid having to copy it around too much (if it wanted to return the data, it would need to make a new byte[]).
  • What the method returns is the number of bytes that have been read (you need to know that, because it may be fewer than what you asked for, see above).
  • There is also a parameter offset that tells the method to not start writing data into the buffer (bytes) at the beginning, but somewhere else. This allows you to reuse the buffer for other things (again to avoid having to copy the data around too much).
Thilo
  • 257,207
  • 101
  • 511
  • 656
  • So after it's placed into the array bytes i can use a println statement on bytes to show what the file contains? Thanks for the info! – Mack Sep 11 '17 at 04:46