[ 
https://issues.apache.org/jira/browse/JOHNZON-18?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14196604#comment-14196604
 ] 

Thiago Veronezi commented on JOHNZON-18:
----------------------------------------

I thought about the retry too, but it may lead to multiple loops (infinite 
maybe for some obscure reason) or to exceptions in case we cannot load the char 
after a certain number of tries. The solution that I proposed makes sure that 
we have at least the next char; after retrieving it (if any), it tries to load 
the buffer for the next iteration. 

Maybe a good refactoring would be to try to load the buffer first; if the 
stream behaves, load the next char and try to load the buffer for the next 
iteration. This way, only slow buffers would suffer.

{code}
.
.
.
availableCharsInBuffer = in.read(buffer, 0, buffer.length);
if(availableCharsInBuffer == -1) {
  availableCharsInBuffer = 0;
  return EOF;
}
if(availableCharsInBuffer == 0) {
  // this stream is slow and didn't have time to load the a single char into 
this buffer.
  // make sure that we have at least the next char
  int value = in.read();
  availableCharsInBuffer = in.read(buffer, 1, buffer.length - 1);
  if(availableCharsInBuffer == -1) {
    availableCharsInBuffer = 1; // we have at least the "value" above
  } else {
    availableCharsInBuffer += 1; // adding the "value" above to the list of 
available chars
  }
  buffer[0] = (char) value;
}
.
.
.
{code}



> JsonStreamParserImpl not filling up buffer consistently
> -------------------------------------------------------
>
>                 Key: JOHNZON-18
>                 URL: https://issues.apache.org/jira/browse/JOHNZON-18
>             Project: Johnzon
>          Issue Type: Bug
>          Components: Core
>            Reporter: Thiago Veronezi
>             Fix For: 0.2-incubating
>
>         Attachments: 
> JsonStreamParserImpl_not_filling_up_buffer_consistently.patch
>
>
> This is related to the issue discussed 
> [here|https://issues.apache.org/jira/browse/JOHNZON-17]. Basically, the 
> "read" method might not read all the bytes at once. We need to make sure to 
> read at least one until we reach the value "-1".



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to