Skip to content

Commit

Permalink
Minor changes wrt #17 to use recyclable buffers
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Feb 23, 2017
1 parent 0711081 commit 933f33c
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 77 deletions.
4 changes: 4 additions & 0 deletions release-notes/CREDITS
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,7 @@ Brad Hess (bdhess@github)
(2.9.0)
* Reported #325: `DataInput` backed parser should handle `EOFException` at end of doc
(2.9.0)

Logan Widick (uhhhh2@github)
* Contributed #17: Add 'JsonGenerator.writeString(Reader r, int charLength)'
(2.9.0)
2 changes: 2 additions & 0 deletions release-notes/VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ JSON library.

2.9.0 (not yet released)

#17: Add 'JsonGenerator.writeString(Reader r, int charLength)'
(constributed by Logan W)
#304: Optimize `NumberOutput.outputLong()` method
#312: Add `JsonProcessingException.clearLocation()` to allow clearing
possibly security-sensitive information
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/com/fasterxml/jackson/core/JsonGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
*/
package com.fasterxml.jackson.core;

import com.fasterxml.jackson.core.JsonParser.NumberType;
import com.fasterxml.jackson.core.io.CharacterEscapes;
import com.fasterxml.jackson.core.util.VersionUtil;

import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

import com.fasterxml.jackson.core.JsonParser.NumberType;
import com.fasterxml.jackson.core.io.CharacterEscapes;
import com.fasterxml.jackson.core.util.VersionUtil;

import static com.fasterxml.jackson.core.JsonTokenId.*;

/**
Expand Down Expand Up @@ -941,6 +941,8 @@ public void writeArray(double[] array, int offset, int length) throws IOExceptio
* If the reader is null, then write a null.
* If len is < 0, then write all contents of the reader.
* Otherwise, write only len characters.
*
* @since 2.9
*/
public abstract void writeString(Reader reader, int len) throws IOException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ public int writeBinary(Base64Variant b64variant, InputStream data, int dataLengt
return 0;
}

@Override
@Override // since 2.9
public void writeString(Reader reader, int len) throws IOException {
// Let's implement this as "unsupported" to make it easier to add new parser impls
_reportUnsupportedOperation();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
package com.fasterxml.jackson.core.json;

import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;

import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.io.CharTypes;
import com.fasterxml.jackson.core.io.CharacterEscapes;
import com.fasterxml.jackson.core.io.IOContext;
import com.fasterxml.jackson.core.io.NumberOutput;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.math.BigDecimal;
import java.math.BigInteger;

public class UTF8JsonGenerator
extends JsonGeneratorImpl
{
Expand Down Expand Up @@ -476,7 +473,6 @@ public void writeString(Reader reader, int len) throws IOException {

int toRead = (len >= 0) ? len : Integer.MAX_VALUE;

//TODO: Check that this use is OK
final char[] buf = _charBuffer;

//Add leading quote
Expand All @@ -486,11 +482,8 @@ public void writeString(Reader reader, int len) throws IOException {
_outputBuffer[_outputTail++] = _quoteChar;

//read
while(toRead > 0){
while (toRead > 0){
int toReadNow = Math.min(toRead, buf.length);
if(toRead <= 0){
break;
}
int numRead = reader.read(buf, 0, toReadNow);
if(numRead <= 0){
break;
Expand All @@ -499,7 +492,6 @@ public void writeString(Reader reader, int len) throws IOException {
_flushBuffer();
}
_writeStringSegments(buf, 0, numRead);

//decrease tracker
toRead -= numRead;
}
Expand All @@ -510,7 +502,7 @@ public void writeString(Reader reader, int len) throws IOException {
}
_outputBuffer[_outputTail++] = _quoteChar;

if(toRead > 0 && len >= 0){
if (toRead > 0 && len >= 0){
_reportError("Didn't read enough from reader");
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
package com.fasterxml.jackson.core.json;

import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;

import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.io.CharTypes;
import com.fasterxml.jackson.core.io.CharacterEscapes;
import com.fasterxml.jackson.core.io.IOContext;
import com.fasterxml.jackson.core.io.NumberOutput;

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.Writer;
import java.math.BigDecimal;
import java.math.BigInteger;

/**
* {@link JsonGenerator} that outputs JSON content using a {@link java.io.Writer}
* which handles character encoding.
Expand Down Expand Up @@ -75,23 +72,20 @@ public class WriterBasedJsonGenerator
*/
protected char[] _entityBuffer;

/**
* Intermediate buffer in which characters of a String are copied
* before being encoded.
*/
protected char[] _charBuffer;

/**
* Length of <code>_charBuffer</code>
*/
protected final int _charBufferLength;

/**
* When custom escapes are used, this member variable is used
* internally to hold a reference to currently used escape
*/
protected SerializableString _currentEscape;

/**
* Intermediate buffer in which characters of a String are copied
* before being encoded.
*
* @since 2.9
*/
protected char[] _charBuffer;

/*
/**********************************************************
/* Life-cycle
Expand All @@ -105,16 +99,14 @@ public WriterBasedJsonGenerator(IOContext ctxt, int features,
_writer = w;
_outputBuffer = ctxt.allocConcatBuffer();
_outputEnd = _outputBuffer.length;
_charBuffer = new char[_outputBuffer.length];
_charBufferLength = _charBuffer.length;
}

/*
/**********************************************************
/* Overridden configuration, introspection methods
/**********************************************************
*/

@Override
public Object getOutputTarget() {
return _writer;
Expand Down Expand Up @@ -395,46 +387,38 @@ public void writeString(Reader reader, int len) throws IOException {
_verifyValueWrite(WRITE_STRING);
if (reader == null) {
_reportError("null reader");
} else {
int toRead = (len >= 0) ? len : Integer.MAX_VALUE;

//TODO: Check that this use is OK
final char[] buf = _charBuffer;

//Add leading quote
if ((_outputTail + len) >= _outputEnd) {
_flushBuffer();
}
_outputBuffer[_outputTail++] = _quoteChar;

//read
while (toRead > 0) {
int toReadNow = Math.min(toRead, buf.length);
if (toRead <= 0) {
break;
}
int numRead = reader.read(buf, 0, toReadNow);
if (numRead <= 0) {
break;
}
if ((_outputTail + len) >= _outputEnd) {
_flushBuffer();
}
_writeString(buf, 0, numRead);
}
int toRead = (len >= 0) ? len : Integer.MAX_VALUE;
final char[] buf = _allocateCopyBuffer();
//Add leading quote
if ((_outputTail + len) >= _outputEnd) {
_flushBuffer();
}
_outputBuffer[_outputTail++] = _quoteChar;

//decrease tracker
toRead -= numRead;
//read
while (toRead > 0) {
int toReadNow = Math.min(toRead, buf.length);
int numRead = reader.read(buf, 0, toReadNow);
if (numRead <= 0) {
break;
}

//Add trailing quote
if ((_outputTail + len) >= _outputEnd) {
_flushBuffer();
}
_outputBuffer[_outputTail++] = _quoteChar;
_writeString(buf, 0, numRead);

if (toRead > 0 && len >= 0) {
_reportError("Didn't read enough from reader");
}
//decrease tracker
toRead -= numRead;
}
//Add trailing quote
if ((_outputTail + len) >= _outputEnd) {
_flushBuffer();
}
_outputBuffer[_outputTail++] = _quoteChar;

if (toRead > 0 && len >= 0) {
_reportError("Didn't read enough from reader");
}
}

Expand Down Expand Up @@ -957,7 +941,11 @@ protected void _releaseBuffers()
_outputBuffer = null;
_ioContext.releaseConcatBuffer(buf);
}
_charBuffer = null;
buf = _charBuffer;
if (buf != null) {
_charBuffer = null;
_ioContext.releaseNameCopyBuffer(buf);
}
}

/*
Expand Down Expand Up @@ -1938,7 +1926,17 @@ private char[] _allocateEntityBuffer()
_entityBuffer = buf;
return buf;
}


/**
* @since 2.9
*/
private char[] _allocateCopyBuffer() {
if (_charBuffer == null) {
_charBuffer = _ioContext.allocNameCopyBuffer(2000);
}
return _charBuffer;
}

protected void _flushBuffer() throws IOException
{
int len = _outputTail - _outputHead;
Expand Down

0 comments on commit 933f33c

Please sign in to comment.