Skip to content

Commit

Permalink
Some refactoring pre #479
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Aug 27, 2019
1 parent 4bb9357 commit baa0380
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 21 deletions.
44 changes: 27 additions & 17 deletions src/main/java/com/fasterxml/jackson/core/io/JsonStringEncoder.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.fasterxml.jackson.core.io;

import java.lang.ref.SoftReference;
import java.util.Arrays;

import com.fasterxml.jackson.core.util.ByteArrayBuilder;
import com.fasterxml.jackson.core.util.TextBuffer;
Expand Down Expand Up @@ -52,12 +53,6 @@ public final class JsonStringEncoder
/**********************************************************************
*/

/**
* Lazily constructed text buffer used to produce JSON encoded Strings
* as characters (without UTF-8 encoding)
*/
protected TextBuffer _text;

/**
* Lazily-constructed builder used for UTF-8 encoding of text values
* (quoted and unquoted)
Expand Down Expand Up @@ -109,12 +104,8 @@ public static JsonStringEncoder getInstance() {
*/
public char[] quoteAsString(String input)
{
TextBuffer textBuffer = _text;
if (textBuffer == null) {
// no allocator; can add if we must, shouldn't need to
_text = textBuffer = new TextBuffer(null);
}
char[] outputBuffer = textBuffer.emptyAndGetCurrentSegment();
TextBuffer textBuffer = null;
char[] outputBuffer = new char[100];
final int[] escCodes = CharTypes.get7BitOutputEscapes();
final int escCodeCount = escCodes.length;
int inPtr = 0;
Expand All @@ -130,6 +121,9 @@ public char[] quoteAsString(String input)
break tight_loop;
}
if (outPtr >= outputBuffer.length) {
if (textBuffer == null) {
textBuffer = TextBuffer.fromInitial(outputBuffer);
}
outputBuffer = textBuffer.finishCurrentSegment();
outPtr = 0;
}
Expand All @@ -150,6 +144,9 @@ public char[] quoteAsString(String input)
if (first > 0) {
System.arraycopy(_qbuf, 0, outputBuffer, outPtr, first);
}
if (textBuffer == null) {
textBuffer = TextBuffer.fromInitial(outputBuffer);
}
outputBuffer = textBuffer.finishCurrentSegment();
int second = length - first;
System.arraycopy(_qbuf, first, outputBuffer, 0, second);
Expand All @@ -159,6 +156,10 @@ public char[] quoteAsString(String input)
outPtr += length;
}
}

if (textBuffer == null) {
return Arrays.copyOfRange(outputBuffer, 0, outPtr);
}
textBuffer.setCurrentLength(outPtr);
return textBuffer.contentsAsArray();
}
Expand All @@ -175,11 +176,9 @@ public char[] quoteAsString(CharSequence input)
return quoteAsString((String) input);
}

TextBuffer textBuffer = _text;
if (textBuffer == null) {
_text = textBuffer = new TextBuffer(null);
}
char[] outputBuffer = textBuffer.emptyAndGetCurrentSegment();
TextBuffer textBuffer = null;

char[] outputBuffer = new char[100];
final int[] escCodes = CharTypes.get7BitOutputEscapes();
final int escCodeCount = escCodes.length;
int inPtr = 0;
Expand All @@ -195,6 +194,9 @@ public char[] quoteAsString(CharSequence input)
break tight_loop;
}
if (outPtr >= outputBuffer.length) {
if (textBuffer == null) {
textBuffer = TextBuffer.fromInitial(outputBuffer);
}
outputBuffer = textBuffer.finishCurrentSegment();
outPtr = 0;
}
Expand All @@ -203,6 +205,7 @@ public char[] quoteAsString(CharSequence input)
break outer;
}
}
// something to escape; 2 or 6-char variant?
char d = input.charAt(inPtr++);
int escCode = escCodes[d];
int length = (escCode < 0)
Expand All @@ -214,6 +217,9 @@ public char[] quoteAsString(CharSequence input)
if (first > 0) {
System.arraycopy(_qbuf, 0, outputBuffer, outPtr, first);
}
if (textBuffer == null) {
textBuffer = TextBuffer.fromInitial(outputBuffer);
}
outputBuffer = textBuffer.finishCurrentSegment();
int second = length - first;
System.arraycopy(_qbuf, first, outputBuffer, 0, second);
Expand All @@ -223,6 +229,10 @@ public char[] quoteAsString(CharSequence input)
outPtr += length;
}
}

if (textBuffer == null) {
return Arrays.copyOfRange(outputBuffer, 0, outPtr);
}
textBuffer.setCurrentLength(outPtr);
return textBuffer.contentsAsArray();
}
Expand Down
26 changes: 24 additions & 2 deletions src/main/java/com/fasterxml/jackson/core/util/TextBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ public final class TextBuffer

/**
* Let's start with sizable but not huge buffer, will grow as necessary
*<p>
* Reduced from 1000 down to 500 in 2.10.
*/
final static int MIN_SEGMENT_LEN = 1000;
final static int MIN_SEGMENT_LEN = 500;

/**
* Let's limit maximum segment length to something sensible.
* For 2.10, let's limit to using 64kc chunks (128 kB) -- was 256kC/512kB up to 2.9
Expand Down Expand Up @@ -122,6 +124,26 @@ public TextBuffer(BufferRecycler allocator) {
_allocator = allocator;
}

/**
* @since 2.10
*/
protected TextBuffer(BufferRecycler allocator, char[] initialSegment) {
_allocator = allocator;
_currentSegment = initialSegment;
_currentSize = initialSegment.length;
_inputStart = -1;
}

/**
* Factory method for constructing an instance with no allocator, and
* with initial full segment.
*
* @since 2.10
*/
public static TextBuffer fromInitial(char[] initialSegment) {
return new TextBuffer(null, initialSegment);
}

/**
* Method called to indicate that the underlying buffers should now
* be recycled if they haven't yet been recycled. Although caller
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,11 @@ public void testResetWithAndSetCurrentAndReturn() {
public void testGetCurrentSegment() {
TextBuffer textBuffer = new TextBuffer(null);
textBuffer.emptyAndGetCurrentSegment();
textBuffer.setCurrentAndReturn(1000);
// 26-Aug-2019, tatu: Value depends on "minimum segment size":
textBuffer.setCurrentAndReturn(500);
textBuffer.getCurrentSegment();

assertEquals(1000, textBuffer.size());
assertEquals(500, textBuffer.size());
}

public void testAppendTakingTwoAndThreeInts() {
Expand Down

0 comments on commit baa0380

Please sign in to comment.