+ 2

Regarding byte[ ] in java

Hi there everyone ! my question is when we work with streams mostly we deal with byte[ ] and when we init an array of bytes we have to put an arg like... byte [ ] b=new byte[arg]; so how can we deal with the arg ! the arg is diffrent when we use it for file I/O and net I/O and that depends on what ?

4th Aug 2020, 6:45 PM
Naol Dereje
Naol Dereje - avatar
2 ответов
+ 5
I'm assuming that by streams, and because you mention I/O, you mean InputStream and/or OutputStream as Collections are more commonly used with Java 8 Streams. They aren't the same. All byte stream classes are derived from InputStream or OutputStream. InputStream has a method available that will return the amount of bytes that are available in the InputStream. You could use this as the 'arg' to set the size of your byte array for the read(byte[] array) method. For an OutputStream you should have access to the size of the byte array already or you would pass the whole byte array in anyway so would use this info for the 'arg'. https://www.programiz.com/java-programming/inputstream https://www.programiz.com/java-programming/outputstream
4th Aug 2020, 7:13 PM
ChaoticDawg
ChaoticDawg - avatar