Conceptual confusion about Java IO read() from InputStream and write() function to OutputStream -


i'm picking on java io functions , coding little confused while reading through online tutorials. in reference question posted here: inputstream/outputstream read()/write() function relevance , usage

this seemed hint difference between basic write() function , write(byte[] bytes, int offset, int length) function in time efficiency, didn't quite meaning of that.

in tutorial, stated:

public int read(byte[] bytes, int offset, int length) throws ioexception // read "length" number of bytes, store in bytes array starting offset  of index.  public int read(byte[] bytes) throws ioexception // same read(bytes, 0, bytes.length) 

what these 2 lines of code illustrate read() in java io? first line read length of file's info or file's actual info itself.

to pile on more confusion, write() function outputstream explained follows:

"similar input counterpart, abstract superclass outputstream declares abstract method write() write data-byte output sink. write() takes int. least-significant byte of int argument written out; upper 3 bytes discarded. throws ioexception if i/o error occurs (e.g., output stream has been closed)."

does mean actual info written in or argument? kinda confused paragraph trying 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, bytes array starting offset  of index.  public void write(byte[] bytes) throws ioexception // same write(bytes, 0, bytes.length) 

thanks in advance explanation on this.

this seemed hint difference between basic write() function , write(byte[] bytes, int offset, int length) function in time efficiency, didn't quite meaning of that.

the "basic" write(b) method writes single byte of data.

if wanted use write large amounts of data, making lot of method calls.

that why efficiency's sake, there versions of write can take many bytes in single call. works passing in bytes in array (the "buffer").

in addition reducing number of calls, efficient use (and re-use) of buffer saves lot of work. makes code bit more complicated use: instead of receiving results method calls, have manage these byte arrays.

the same applies read methods.


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

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

so first line read length of file's info or file's actual info ?

  • the method reads length bytes file (it may read less, example when file short)
  • the data reads not returned method, placed array bytes. avoid having copy around (if wanted return data, need make new byte[]).
  • what method returns number of bytes have been read (you need know that, because may fewer asked for, see above).
  • there parameter offset tells method not start writing data buffer (bytes) @ beginning, somewhere else. allows reuse buffer other things (again avoid having copy data around much).

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -