Skip to content

Commit

Permalink
issue FasterXML#15 - readtext in jsonparser
Browse files Browse the repository at this point in the history
Review changes
  • Loading branch information
LokeshN committed May 16, 2016
1 parent b42a4da commit 373588c
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 65 deletions.
7 changes: 4 additions & 3 deletions src/main/java/com/fasterxml/jackson/core/JsonParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -1018,10 +1018,11 @@ public void finishToken() throws IOException {
* Method to read the textual representation of the current token in chunks and
* pass it to the given Writer
*
* @return true, if the text chunks has been processed completely and passed to the given writer
* false, if the text chunks are still being processed
* @return The number of characters written to the Writer
*
* @since 2.5
*/
public abstract boolean readText(Writer writer) throws IOException;
public abstract int readText(Writer writer) throws IOException, UnsupportedOperationException;

/**
* Method similar to {@link #getText}, but that will return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,26 +283,27 @@ public final String getText() throws IOException
}

@Override
public final boolean readText(Writer writer) throws IOException {
public final int readText(Writer writer) throws IOException, UnsupportedOperationException {
JsonToken t = _currToken;
//Stores the length of the bytes read
int len = 0;
if (t == JsonToken.VALUE_STRING) {
if (_tokenIncomplete) {
_tokenIncomplete = false;
_finishString(); // only strings can be incomplete
}
List<char[]> segments = _textBuffer.getCharacterSegments();


//if there are character segments, then use them and write them to the writer
if(segments != null && _readTextBufferIndex < segments.size()) {
writer.write(segments.get(_readTextBufferIndex++));
return false;
while(segments != null && _readTextBufferIndex < segments.size()) {
writer.write(segments.get(_readTextBufferIndex));
len += segments.get(_readTextBufferIndex).length;
_readTextBufferIndex++;
}
//if there are no character segments, then read the string from the current segment, and
//if there are no character segments left, then read the string from the current segment, and
//write them directly to the buffer
else {
writer.write(_textBuffer.getCurrentSegment(), 0, _textBuffer.getCurrentSegmentSize());
}
writer.write(_textBuffer.getCurrentSegment(), 0, _textBuffer.getCurrentSegmentSize());
len += _textBuffer.getCurrentSegmentSize();

}
else if(t != null) {
Expand All @@ -322,7 +323,8 @@ else if(t != null) {
//resetting the text buffer index back to zero, once all the chunks are passed to the
//given writer object
_readTextBufferIndex = 0;
return true;

return len;
}

// // // Let's override default impls for improved performance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,26 +191,27 @@ public String getText() throws IOException
}

@Override
public final boolean readText(Writer writer) throws IOException {
public final int readText(Writer writer) throws IOException, UnsupportedOperationException {
JsonToken t = _currToken;
//Stores the length of the bytes read
int len = 0;
if (t == JsonToken.VALUE_STRING) {
if (_tokenIncomplete) {
_tokenIncomplete = false;
_finishString(); // only strings can be incomplete
}
List<char[]> segments = _textBuffer.getCharacterSegments();


//if there are character segments, then use them and write them to the writer
if(segments != null && _readTextBufferIndex < segments.size()) {
writer.write(segments.get(_readTextBufferIndex++));
return false;
while(segments != null && _readTextBufferIndex < segments.size()) {
writer.write(segments.get(_readTextBufferIndex));
len += segments.get(_readTextBufferIndex).length;
_readTextBufferIndex++;
}
//if there are no character segments, then read the string from the current segment, and
//if there are no character segments left, then read the string from the current segment, and
//write them directly to the buffer
else {
writer.write(_textBuffer.getCurrentSegment(), 0, _textBuffer.getCurrentSegmentSize());
}
writer.write(_textBuffer.getCurrentSegment(), 0, _textBuffer.getCurrentSegmentSize());
len += _textBuffer.getCurrentSegmentSize();

}
else if(t != null) {
Expand All @@ -230,7 +231,8 @@ else if(t != null) {
//resetting the text buffer index back to zero, once all the chunks are passed to the
//given writer object
_readTextBufferIndex = 0;
return true;

return len;
}

// // // Let's override default impls for improved performance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,26 +328,27 @@ public String getText() throws IOException
}

@Override
public final boolean readText(Writer writer) throws IOException {
public final int readText(Writer writer) throws IOException, UnsupportedOperationException {
JsonToken t = _currToken;
//Stores the length of the bytes read
int len = 0;
if (t == JsonToken.VALUE_STRING) {
if (_tokenIncomplete) {
_tokenIncomplete = false;
_finishString(); // only strings can be incomplete
}
List<char[]> segments = _textBuffer.getCharacterSegments();


//if there are character segments, then use them and write them to the writer
if(segments != null && _readTextBufferIndex < segments.size()) {
writer.write(segments.get(_readTextBufferIndex++));
return false;
while(segments != null && _readTextBufferIndex < segments.size()) {
writer.write(segments.get(_readTextBufferIndex));
len += segments.get(_readTextBufferIndex).length;
_readTextBufferIndex++;
}
//if there are no character segments, then read the string from the current segment, and
//if there are no character segments left, then read the string from the current segment, and
//write them directly to the buffer
else {
writer.write(_textBuffer.getCurrentSegment(), 0, _textBuffer.getCurrentSegmentSize());
}
writer.write(_textBuffer.getCurrentSegment(), 0, _textBuffer.getCurrentSegmentSize());
len += _textBuffer.getCurrentSegmentSize();

}
else if(t != null) {
Expand All @@ -367,7 +368,8 @@ else if(t != null) {
//resetting the text buffer index back to zero, once all the chunks are passed to the
//given writer object
_readTextBufferIndex = 0;
return true;

return len;
}

// // // Let's override default impls for improved performance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public JsonParser overrideFormatFeatures(int values, int mask) {
@Override public char[] getTextCharacters() throws IOException { return delegate.getTextCharacters(); }
@Override public int getTextLength() throws IOException { return delegate.getTextLength(); }
@Override public int getTextOffset() throws IOException { return delegate.getTextOffset(); }
@Override public boolean readText(Writer writer) throws IOException { return delegate.readText(writer); }
@Override public int readText(Writer writer) throws IOException { return delegate.readText(writer); }

/*
/**********************************************************
Expand Down
44 changes: 13 additions & 31 deletions src/test/java/com/fasterxml/jackson/core/read/JsonParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -608,12 +608,10 @@ public void testReadText() throws Exception {
parser.nextToken();

Writer writer = new StringWriter();
while(!parser.readText(writer)) {
writer.flush();
assertTrue("String not empty", writer.toString().length() > 0);
}
int len = parser.readText(writer);

assertTrue("String length should be same", writer.toString().length() == "this is a sample text for json parsing using readText() method".length());
assertEquals("Returned length should be same", len, writer.toString().length());

//create parser in stream mode..
parser = _createParser(MODE_INPUT_STREAM, JSON);
Expand All @@ -623,12 +621,10 @@ public void testReadText() throws Exception {
parser.nextToken();

writer = new StringWriter();
while(!parser.readText(writer)) {
writer.flush();
assertTrue("String not empty", writer.toString().length() > 0);
}
len = parser.readText(writer);

assertTrue("String length should be same", writer.toString().length() == "this is a sample text for json parsing using readText() method".length());
assertEquals("Returned length should be same", len, writer.toString().length());

//create parser in data input mode..
parser = createParserForDataInput(JSON_FACTORY, new MockDataInput(JSON));
Expand All @@ -638,12 +634,10 @@ public void testReadText() throws Exception {
parser.nextToken();

writer = new StringWriter();
while(!parser.readText(writer)) {
writer.flush();
assertTrue("String not empty", writer.toString().length() > 0);
}
len = parser.readText(writer);

assertTrue("String length should be same", writer.toString().length() == "this is a sample text for json parsing using readText() method".length());
assertEquals("Returned length should be same", len, writer.toString().length());
}

public void testLongerReadText() throws Exception {
Expand All @@ -661,12 +655,8 @@ public void testLongerReadText() throws Exception {
parser.nextToken();

Writer writer = new StringWriter();
while(!parser.readText(writer)) {
writer.flush();
assertTrue("String not empty", writer.toString().length() > 0);
}

assertTrue("String length should be same", writer.toString().length() == longText.length());
int len = parser.readText(writer);
assertEquals("Returned length should be same", len, writer.toString().length());

//create parser in stream mode..
parser = _createParser(MODE_INPUT_STREAM, JSON);
Expand All @@ -676,27 +666,19 @@ public void testLongerReadText() throws Exception {
parser.nextToken();

writer = new StringWriter();
while(!parser.readText(writer)) {
writer.flush();
assertTrue("String not empty", writer.toString().length() > 0);
}

assertTrue("String length should be same", writer.toString().length() == longText.length());
len = parser.readText(writer);
assertEquals("Returned length should be same", len, writer.toString().length());

//create parser in data input mode..
parser = _createParser(MODE_INPUT_DATA, SAMPLE_DOC_JSON_SPEC);
parser = _createParser(MODE_INPUT_DATA, JSON);
//move the token until the string field
parser.nextToken();
parser.nextToken();
parser.nextToken();

writer = new StringWriter();
while(!parser.readText(writer)) {
writer.flush();
assertTrue("String not empty", writer.toString().length() > 0);
}
//Need to check this...
//assertTrue("String length should be same", writer.toString().length() == SAMPLE_DOC_JSON_SPEC.length());
len = parser.readText(writer);
assertEquals("Returned length should be same", len, writer.toString().length());

}

Expand Down

0 comments on commit 373588c

Please sign in to comment.