• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Overriding a method for a Custom Input Stream

 
Eric Movsessian
Greenhorn
Posts: 14
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello)

So I have the read(read[] ) method overridden on my CustomInputStream which extends the FilterInputStream. The programm works wxactly the way I want it to.
I was wondering how does it even work if it says "return 0"?
What is the concept behind this?


 
Paul Clapham
Marshal
Posts: 26909
82
Eclipse IDE Firefox Browser MySQL Database
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The concept behind it is what the documentation for that method says. In particular the documentation will tell you what the method is supposed to return.

If you didn't know there was documentation, or you haven't ever looked at it, then you should. You can't really be a Java programmer without reading the fine documentation.

Also, it's possible that the code which calls that method is not acting according to what the documentation says it should be doing. But you didn't show us that.
 
Les Morgan
Rancher
Posts: 988
23
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Eric,

the 0 is just a flag saying it has completed, the byte array is an object reference, so it's a reference to the buffer from the super... the InputStream.  The routine is massaging the buffer and substituting the values show into the buffer (b) at the appropriate places and when the routing is done, the byte buffer has the massaged data inside of it--since it is a reference the caller has access to the massaged data.

Les

Eric Movsessian wrote:Hello)

So I have the read(read[] ) method overridden on my CustomInputStream which extends the FilterInputStream. The programm works wxactly the way I want it to.
I was wondering how does it even work if it says "return 0"?
What is the concept behind this?


 
Rob Spoor
Sheriff
Posts: 22504
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That method is incorrectly implemented. It should return the number of bytes that were read. That's what super.read(b) returns. You should also use it in your boundaries:

While you're at it, I'd also override the read(byte[], int, int) method. That looks a lot like this one (in fact, most of the time read(byte[]) simply returns read(b, 0, b.length)). The loop can also be limited to the given bounds combined with the result of the call to super.read.
 
Eric Movsessian
Greenhorn
Posts: 14
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:The concept behind it is what the documentation for that method says. In particular the documentation will tell you what the method is supposed to return.

If you didn't know there was documentation, or you haven't ever looked at it, then you should. You can't really be a Java programmer without reading the fine documentation.

Also, it's possible that the code which calls that method is not acting according to what the documentation says it should be doing. But you didn't show us that.



Thank you very much for your answer,
I have read the documentation for this method and it states that it returns the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.
I am new to this, and was wondering does the method return whatever it is supposed to return when you write the return keyword?
If yes then how did my code work if I told it to return 0? Does it mean that when you write return 0 and not something else it just reads the code above?
I have tested it with return 1, return 2 etc. and it worked exactly the same way.
I am sorry if this sounds stupid.
 
Eric Movsessian
Greenhorn
Posts: 14
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Les Morgan wrote:Eric,

the 0 is just a flag saying it has completed, the byte array is an object reference, so it's a reference to the buffer from the super... the InputStream.  The routine is massaging the buffer and substituting the values show into the buffer (b) at the appropriate places and when the routing is done, the byte buffer has the massaged data inside of it--since it is a reference the caller has access to the massaged data.

Les

Eric Movsessian wrote:Hello)

So I have the read(read[] ) method overridden on my CustomInputStream which extends the FilterInputStream. The programm works wxactly the way I want it to.
I was wondering how does it even work if it says "return 0"?
What is the concept behind this?






Thank you very much for your answer.
I think I understand that but I was wondering that if the wanted result is reached and in this case I absolutely do not use the "0" that it returns then in what cases could the data that is returned be used? Why didn't they make this method void?
 
Paul Clapham
Marshal
Posts: 26909
82
Eclipse IDE Firefox Browser MySQL Database
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Eric Movsessian wrote:I have read the documentation for this method and it states that it returns the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.
I am new to this, and was wondering does the method return whatever it is supposed to return when you write the return keyword?
If yes then how did my code work if I told it to return 0? Does it mean that when you write return 0 and not something else it just reads the code above?
I have tested it with return 1, return 2 etc. and it worked exactly the same way.
I am sorry if this sounds stupid.



Actually the documentation describes what the method is supposed to do. Your responsibility as a person writing that method, then, is to have it do what the documentation says, including returning the appropriate value.

And yes, the return statement just causes the method to terminate and return the value which follows the return keyword. That's all it does. There's no need to imagine weird special cases.

Again, I am assuming that the code you wrote to test your class is ignoring the returned value and just assuming that the array was completely filled with data from the input stream. This will not always happen; before you reach the end of the stream it is possible that the last chunk of data returned will not fill the whole array. But you still haven't showed us that code so we are left to assume.
 
Campbell Ritchie
Marshal
Posts: 74341
334
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because sometimes a user will want to count how many bytes have been read into the buffer. You can ignore that number if you wish.
 
Paul Clapham
Marshal
Posts: 26909
82
Eclipse IDE Firefox Browser MySQL Database
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It looks like you don't understand how the InputStream class works, in which case it's not surprising that you are having trouble writing a subclass of it.

An InputStream is connected to a source of bytes, which can often be a file. Usually that source of bytes will contain only a finite number of bytes, but that isn't a requirement.

So code will want to read from an InputStream and get the bytes from it. It's possible to read them one at a time, using the read() method. One would think that would be the most reasonable thing to do. But often the bytes are coming from a hardware device like a hard drive and it's more efficient to get the bytes in blocks. The writers of InputStream could have internalized this efficiency but instead they outsourced it by allowing the code which uses it to read the blocks themselves. Hence the read(byte[]) method. (There's other reasons to allow the user to specify the block size but that's not important here.)

But there's a complication with that idea -- suppose the file has 1073 bytes in it and you choose to use a block whose size is 1000. Then the first time you read a block from the file, you'll get 1000 bytes. The second time you read a block, you'll get the remaining 73 bytes. Your code will need to know that the byte array isn't "full" and so there's a return value which tells you how many bytes of the array are actually meaningful.
 
Eric Movsessian
Greenhorn
Posts: 14
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much. You have genuinely enhanced my understanding of InputStreams
I have overridden 3 methods in my filterInputStream:


Here is my main method: I have determined the size of the byte array ll as 78 just because I know that in my file "testing.txt" there are only 30 characters. so I just randomly chose the number 78.



 
Paul Clapham
Marshal
Posts: 26909
82
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Eric Movsessian wrote:Here is my main method: I have determined the size of the byte array ll as 78 just because I know that in my file "testing.txt" there are only 30 characters. so I just randomly chose the number 78.



But there's really no point in writing a whole FilteredInputStream class for that, you could just use an ordinary InputStream and do the byte conversion in your main program.

Not to mention, you probably haven't noticed that your output file contains 78 bytes and not 30 bytes. Have you looked at it to see if the changes happened correctly?

But anyway if you're going to write a FilteredInputStream then you should at least write something which will work properly in the case that your input file contains, say, 2 million bytes. So again, the read(byte[]) method's documentation includes this:

Returns:
   the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.



Don't return zero, return that value. And you can tell what it is because the superclass returns it to you. Someone else earlier in the thread showed you how.

And for a better test, use an array which contains only 12 bytes and write a test program which loops until all of the bytes (and no more) have been read and written to the output.
 
Rob Spoor
Sheriff
Posts: 22504
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's a common pattern for reading from input streams; the buffer size may be different (any power of 2 between 1024 and 8192 inclusive are often used).

Use this with a relatively small size (let's say 32) and write your method in such a way that, if you pass it to the above block, your program prints out what you expect. You will definitely need to start using the result of super.read, as it has 3 possible outcomes that you need to handle:
  • -1: the stream is exhausted
  • b.length: there was enough data to fill the array completely
  • some value larger than 0* but smaller than b.length: there was not enough data available. Note that this doesn't have to occur at the end; the original input stream is allowed to fill the array only partially at any time


  • * The specification says that 0 is only allowed with an empty array (or with the other bulk read method, if the len is 0). Otherwise it must wait until at least 1 byte is available, or the stream is exhausted.
     
    Rob Spoor
    Sheriff
    Posts: 22504
    122
    Eclipse IDE Spring VI Editor Chrome Java Windows
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Let's extend that for the other bulk read method. Use the following block to test it:

    Even with a large array, this will read only 5 bytes at a time, and it ignore array indexes 0..4 and 10..buffer.length-1, That means you shouldn't try to change those either.
     
    Eric Movsessian
    Greenhorn
    Posts: 14
    Eclipse IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Rob Spoor wrote:Let's extend that for the other bulk read method. Use the following block to test it:

    Even with a large array, this will read only 5 bytes at a time, and it ignore array indexes 0..4 and 10..buffer.length-1, That means you shouldn't try to change those either.



    Thank you so much.

    I have done a thorough testing and understood everything except for this one thing. So I have a little modified version of the code that you have provided:



    So I have put the  System.out.println(" "+len+" ")   there for testing purposes, to see what the "len" equals at the time of printing.  My file from where it reads contains teh following text "ScuderiaFerrariSF22" and nothing else. Here is the output that I get in the console:  

    Scud 4
    eria 4
    Ferr 4
    ariS 4
    F22 3

    I do not understand why does it return a 3 at the end. I mean I understand that it read 3 bytes hence it returned 3 but when I look at the steps that the program took, I get confused.
    Here is what I see:
    the stream reads the character F and puts it in buffer[2], then it reads the character 2 and puts it in buffer[3], then it reads  the character 2 and puts it in buffer[4], then it sees that there is no characters left and returns -1, but how is that possible if 3 is put inside of the "len" here     System.out.write(buffer,2,len)    instead of -1?
     
    Paul Clapham
    Marshal
    Posts: 26909
    82
    Eclipse IDE Firefox Browser MySQL Database
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Eric Movsessian wrote:...then it sees that there is no characters left and returns -1...



    But your testing clearly indicates that it returns 3 and not -1. Let's look at the documentation again:

    Returns:
       the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.



    So... in your case there were 3 bytes read into the buffer and no more than that because there is now no more data to read. Okay, that's a bit ambiguous. But be practical. The InputStream read 3 bytes into the buffer. If it returns -1 then the calling program is going to assume there is no more data to process, right? I mean, that's how your test program works, and it's how you want your test program to work. Having it ignore the few bytes in the last buffer would be a Bad Thing. So you should interpret that specification in the most reasonable way.
     
    Rob Spoor
    Sheriff
    Posts: 22504
    122
    Eclipse IDE Spring VI Editor Chrome Java Windows
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You can also look at it this way. "ScuderiaFerrariSF22" is 19 characters, and since this is ASCII, also 19 bytes. The first 4 loop iterations each read 4 bytes, for a total of 16. The last loop iteration wants to read 4 bytes but only 3 are left. The next call to read sees nothing left and therefore returns -1.
     
    Jesse Silverman
    Saloon Keeper
    Posts: 1600
    51
    Eclipse IDE Postgres Database C++ Java
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Rob Spoor wrote:You can also look at it this way. "ScuderiaFerrariSF22" is 19 characters, and since this is ASCII, also 19 bytes. The first 4 loop iterations each read 4 bytes, for a total of 16. The last loop iteration wants to read 4 bytes but only 3 are left. The next call to read sees nothing left and therefore returns -1.



    One interesting thing is around "since this is ASCII".

    Eric chose to implement this in such a way that will execute with any file, but is only meaningful for straight ASCII input or a UTF-8 file that contains no characters outside of the first 128.  Otherwise the filter would be changing valid characters to other characters unintentionally.

    Actually, I normally don't consider the values 194 and 233 to be "basic ASCII", we get into issues with code pages, etc.

    What is the code page for the files being read and written?  If it is UTF-8 I think things are okay as long as there are only values below 0x80 in it.

    What would be the most recommended way for doing the transformation he is performing?

    I am always nervous operating in raw bytes on data that is intended to be characters, there's a reason we have Readers and Writers...


     
    Paul Clapham
    Marshal
    Posts: 26909
    82
    Eclipse IDE Firefox Browser MySQL Database
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Jesse Silverman wrote:What would be the most recommended way for doing the transformation he is performing?


    That would depend on the reason for the transformation. If the code is processing bytes which are intended to be treated as characters, I can't imagine any reason for converting all "A" to "Â" anyway, so it's a mystery to me. It's possible that it's just a pure exercise for learning how to write a FilterInputStream.

    I am always nervous operating in raw bytes on data that is intended to be characters, there's a reason we have Readers and Writers...

     
    Jesse Silverman
    Saloon Keeper
    Posts: 1600
    51
    Eclipse IDE Postgres Database C++ Java
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Right.

    I can imagine applications that are transforming raw bytes to other raw bytes, but this would not be the best way of transforming "characters" to "characters" in general, as it breaks in the face of general Unicode character input (either UTF-8 or UTF-16).

    Returning the right number for count (whether characters or bytes) is essential either way, as was demonstrated by the responses to this thread.
     
    Eric Movsessian
    Greenhorn
    Posts: 14
    Eclipse IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Paul Clapham wrote:

    Eric Movsessian wrote:...then it sees that there is no characters left and returns -1...



    But your testing clearly indicates that it returns 3 and not -1. Let's look at the documentation again:

    Returns:
       the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.



    So... in your case there were 3 bytes read into the buffer and no more than that because there is now no more data to read. Okay, that's a bit ambiguous. But be practical. The InputStream read 3 bytes into the buffer. If it returns -1 then the calling program is going to assume there is no more data to process, right? I mean, that's how your test program works, and it's how you want your test program to work. Having it ignore the few bytes in the last buffer would be a Bad Thing. So you should interpret that specification in the most reasonable way.




    Thank you so much for your answer.

    Yes I know that it returns 3, but if after reading the last byte, and then trying to read another one and putting the byte in the position buffer[4], doesn't it return -1? and as the returned value is stored inside the "len" variable, how does this get read:    System.out.write(buffer,2,len); , if len is -1?
    I mean , I totally understand that in that case 3 equals to len, I just do not understand why not -1.  I think I am missing out on something.
    Why doesn't the -1  get stored in the "len"?
     
    Eric Movsessian
    Greenhorn
    Posts: 14
    Eclipse IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Paul Clapham wrote:

    Jesse Silverman wrote:What would be the most recommended way for doing the transformation he is performing?


    That would depend on the reason for the transformation. If the code is processing bytes which are intended to be treated as characters, I can't imagine any reason for converting all "A" to "Â" anyway, so it's a mystery to me. It's possible that it's just a pure exercise for learning how to write a FilterInputStream.

    I am always nervous operating in raw bytes on data that is intended to be characters, there's a reason we have Readers and Writers...



    Yes, I was just doing it to sort of add another layer of complexity.
    Thank you so much.
     
    Paul Clapham
    Marshal
    Posts: 26909
    82
    Eclipse IDE Firefox Browser MySQL Database
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Eric Movsessian wrote:Why doesn't the -1  get stored in the "len"?



    There are 3 bytes in the buffer. The documentation says that the method returns the number of bytes in the buffer. Which is 3.

    You of course are free to return -1 in your overridden method, because the documentation says to return -1 if the end of stream has been reached, but if you do that then those last 3 bytes will not be seen by the calling program and users of your overridden method will be correct to complain.

    (Hopefully you weren't asking why, when your method returned 3, the calling code didn't receive -1 as the return value.)
     
    Eric Movsessian
    Greenhorn
    Posts: 14
    Eclipse IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Paul Clapham wrote:

    Eric Movsessian wrote:Why doesn't the -1  get stored in the "len"?



    There are 3 bytes in the buffer. The documentation says that the method returns the number of bytes in the buffer. Which is 3.

    You of course are free to return -1 in your overridden method, because the documentation says to return -1 if the end of stream has been reached, but if you do that then those last 3 bytes will not be seen by the calling program and users of your overridden method will be correct to complain.

    (Hopefully you weren't asking why, when your method returned 3, the calling code didn't receive -1 as the return value.)



    Thank you very much.
    To be honest, that is exactly what I was asking, because, the end of the stream is reached yet the method returns 3. It is reached right?
     
    Paul Clapham
    Marshal
    Posts: 26909
    82
    Eclipse IDE Firefox Browser MySQL Database
    • Likes 2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I've already pointed out that the documentation is unfortunately ambiguous. The actual behaviour of an InputStream is to return -1 if it doesn't have any more data for the caller to process.

    (If you're asking what value your method is returning, you could put some debugging code in there to find out.)
     
    Eric Movsessian
    Greenhorn
    Posts: 14
    Eclipse IDE Java
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Paul Clapham wrote:I've already pointed out that the documentation is unfortunately ambiguous. The actual behaviour of an InputStream is to return -1 if it doesn't have any more data for the caller to process.

    (If you're asking what value your method is returning, you could put some debugging code in there to find out.)



    Thank you very much for your help, have great rest of your day.
     
    Les Morgan
    Rancher
    Posts: 988
    23
    Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    imo: it is because the author of the routine is an MS programmer and not really a Java programmer--0 means no errors in MS land.

    Eric Movsessian wrote:

    Les Morgan wrote:Eric,

    the 0 is just a flag saying it has completed, the byte array is an object reference, so it's a reference to the buffer from the super... the InputStream.  The routine is massaging the buffer and substituting the values show into the buffer (b) at the appropriate places and when the routing is done, the byte buffer has the massaged data inside of it--since it is a reference the caller has access to the massaged data.

    Les

    Eric Movsessian wrote:Hello)

    So I have the read(read[] ) method overridden on my CustomInputStream which extends the FilterInputStream. The programm works wxactly the way I want it to.
    I was wondering how does it even work if it says "return 0"?
    What is the concept behind this?






    Thank you very much for your answer.
    I think I understand that but I was wondering that if the wanted result is reached and in this case I absolutely do not use the "0" that it returns then in what cases could the data that is returned be used? Why didn't they make this method void?

     
    Carey Brown
    Saloon Keeper
    Posts: 8704
    71
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I'm late to the game but I was surprised that this wasn't offered up as an option.

     
    Les Morgan
    Rancher
    Posts: 988
    23
    Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Eric,

    the more i look at the code, the more i have to think it was put together by an MS programmer, if it's yours, i will quit wondering.

    the exception is thrown if there is an error, the caller or somewhere up the calling chain has to deal with that.  returning 0 is a very Microsoft thing to do as it is standard that 0 indicates no errors in the Microsoft world, as would be the case if no exception is thrown, but if an exception is thrown the caller never sees the return value, but control is given to the exception handler that is available to catch it.

    the data can be used if the Exception is not thrown.  The reason it is not a void return value is, as i have said, 0 is a Microsoft standard return value meaning no errors.

    the other parts of the routine are as i said in my previous posts.

    Les
     
    Les Morgan
    Rancher
    Posts: 988
    23
    Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Eric,

    Another observations and deduction of the original code:

    it was never meant to be a reader as far as read a line and and hand it off.  it is a buffer filler that curates the data into the form that is wanted.  since the buffer is available to the caller, because it's an array making it a reference, the caller can see how much data is there and call again if they feel they need to do so, but the routine is a very Ms way of filling a buffer and returning curated data.  that gives the reader, buffer filler, a layer of abstraction that is not openly visible to all--a very slight obfuscation.

    Les
     
    Eric Movsessian
    Greenhorn
    Posts: 14
    Eclipse IDE Java
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Carey Brown wrote:I'm late to the game but I was surprised that this wasn't offered up as an option.





    Thank you soo much, this is exactly was I was looking for.
    Thanks to everyone who helped. Very, very grateful.
     
    Paul Clapham
    Marshal
    Posts: 26909
    82
    Eclipse IDE Firefox Browser MySQL Database
    • Likes 3
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Les Morgan wrote:it is a buffer filler that curates the data into the form that is wanted.  since the buffer is available to the caller, because it's an array making it a reference, the caller can see how much data is there and call again if they feel they need to do so, but the routine is a very Ms way of filling a buffer and returning curated data.  that gives the reader, buffer filler, a layer of abstraction that is not openly visible to all--a very slight obfuscation.



    Yes; you pass an array to the read(byte[]) method; it puts some bytes into the array (which is visible to you) and returns the number of bytes it put into the array. Or it returns -1 to tell you that there wasn't anything to put into the array and there isn't going to be anything more ever. That was the way people wrote code back then, 25 years ago.

    But modifying the contents of a mutable parameter is now out of fashion ("Side Effects!"). So in Java 11 they added a method



    which you give it a number N and tell it to return to you a byte array containing up to N bytes. And if there's no more data to come, then it simply returns an empty array to tell you that. No need for -1 as a magic number to say "No more data". This would have been heresy 25 years ago: creating new byte arrays all the time? But think of the garbage collection!
     
    Campbell Ritchie
    Marshal
    Posts: 74341
    334
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Just shows how things have changed. I suspect there are still C books around which say that is the standard way to fill an array.
     
    Jesse Silverman
    Saloon Keeper
    Posts: 1600
    51
    Eclipse IDE Postgres Database C++ Java
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    In C they might be concerned that the caller would forget to call free() on the array after they returned it.

    Not being funny, that is one of the issues you are thinking about all the time.

    As is that C arrays don't know what size they are, so if you pass in a 50 byte array and ask for 15000 bytes you are in deep doo-doo.

    Sounds like neither way is as much fun to read/debug/maintain as Java, huh?

    As I said on two recent interviews, I appreciate Java and C# in ways that nobody who hasn't maintained large code bases in C ever could, or someone who hasn't done it recently have forgotten to!
     
    Paul Clapham
    Marshal
    Posts: 26909
    82
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Vivek Sen,
    Your post was moved to a new topic.
    (This informational message will self destruct in two days)
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic