Skip to content

Commit

Permalink
Minor refactoring wrt #400 to make it easier to override behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Oct 14, 2017
1 parent 3756448 commit e0780ca
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 55 deletions.
18 changes: 2 additions & 16 deletions src/main/java/com/fasterxml/jackson/core/JsonFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.fasterxml.jackson.core.sym.ByteQuadsCanonicalizer;
import com.fasterxml.jackson.core.sym.CharsToNameCanonicalizer;
import com.fasterxml.jackson.core.util.BufferRecycler;
import com.fasterxml.jackson.core.util.BufferRecyclers;
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;

/**
Expand Down Expand Up @@ -182,14 +183,6 @@ public static int collectDefaults() {
/**********************************************************
*/

/**
* This <code>ThreadLocal</code> contains a {@link java.lang.ref.SoftReference}
* to a {@link BufferRecycler} used to provide a low-cost
* buffer recycling between reader and writer instances.
*/
final protected static ThreadLocal<SoftReference<BufferRecycler>> _recyclerRef
= new ThreadLocal<SoftReference<BufferRecycler>>();

/**
* Each factory comes equipped with a shared root symbol table.
* It should not be linked back to the original blueprint, to
Expand Down Expand Up @@ -1542,14 +1535,7 @@ public BufferRecycler _getBufferRecycler()
* on Android, for example)
*/
if (Feature.USE_THREAD_LOCAL_FOR_BUFFER_RECYCLING.enabledIn(_factoryFeatures)) {
SoftReference<BufferRecycler> ref = _recyclerRef.get();
BufferRecycler br = (ref == null) ? null : ref.get();

if (br == null) {
br = new BufferRecycler();
_recyclerRef.set(new SoftReference<BufferRecycler>(br));
}
return br;
return BufferRecyclers.getBufferRecycler();
}
return new BufferRecycler();
}
Expand Down
24 changes: 5 additions & 19 deletions src/main/java/com/fasterxml/jackson/core/io/JsonStringEncoder.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.fasterxml.jackson.core.io;

import java.lang.ref.SoftReference;

import com.fasterxml.jackson.core.util.BufferRecycler;
import com.fasterxml.jackson.core.util.BufferRecyclers;
import com.fasterxml.jackson.core.util.ByteArrayBuilder;
import com.fasterxml.jackson.core.util.TextBuffer;

Expand All @@ -28,14 +26,6 @@ public final class JsonStringEncoder
// private final static int INT_BACKSLASH = '\\';
// private final static int INT_U = 'u';
// private final static int INT_0 = '0';

/**
* This <code>ThreadLocal</code> contains a {@link java.lang.ref.SoftReference}
* to a {@link BufferRecycler} used to provide a low-cost
* buffer recycling between reader and writer instances.
*/
final protected static ThreadLocal<SoftReference<JsonStringEncoder>> _threadEncoder
= new ThreadLocal<SoftReference<JsonStringEncoder>>();

/**
* Lazily constructed text buffer used to produce JSON encoded Strings
Expand Down Expand Up @@ -70,16 +60,12 @@ public JsonStringEncoder() {
/**
* Factory method for getting an instance; this is either recycled per-thread instance,
* or a newly constructed one.
*
* @deprecated Since 2.9.2 use {@link BufferRecyclers#getJsonStringEncoder()} instead
*/
@Deprecated
public static JsonStringEncoder getInstance() {
SoftReference<JsonStringEncoder> ref = _threadEncoder.get();
JsonStringEncoder enc = (ref == null) ? null : ref.get();

if (enc == null) {
enc = new JsonStringEncoder();
_threadEncoder.set(new SoftReference<JsonStringEncoder>(enc));
}
return enc;
return BufferRecyclers.getJsonStringEncoder();
}

/*
Expand Down
21 changes: 11 additions & 10 deletions src/main/java/com/fasterxml/jackson/core/io/SerializedString.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.nio.ByteBuffer;

import com.fasterxml.jackson.core.SerializableString;
import com.fasterxml.jackson.core.util.BufferRecyclers;

/**
* String token that can lazily serialize String contained and then reuse that
Expand Down Expand Up @@ -98,7 +99,7 @@ protected Object readResolve() {
public final char[] asQuotedChars() {
char[] result = _quotedChars;
if (result == null) {
result = JsonStringEncoder.getInstance().quoteAsString(_value);
result = BufferRecyclers.getJsonStringEncoder().quoteAsString(_value);
_quotedChars = result;
}
return result;
Expand All @@ -112,7 +113,7 @@ public final char[] asQuotedChars() {
public final byte[] asUnquotedUTF8() {
byte[] result = _unquotedUTF8Ref;
if (result == null) {
result = JsonStringEncoder.getInstance().encodeAsUTF8(_value);
result = BufferRecyclers.getJsonStringEncoder().encodeAsUTF8(_value);
_unquotedUTF8Ref = result;
}
return result;
Expand All @@ -126,7 +127,7 @@ public final byte[] asUnquotedUTF8() {
public final byte[] asQuotedUTF8() {
byte[] result = _quotedUTF8Ref;
if (result == null) {
result = JsonStringEncoder.getInstance().quoteAsUTF8(_value);
result = BufferRecyclers.getJsonStringEncoder().quoteAsUTF8(_value);
_quotedUTF8Ref = result;
}
return result;
Expand All @@ -142,7 +143,7 @@ public final byte[] asQuotedUTF8() {
public int appendQuotedUTF8(byte[] buffer, int offset) {
byte[] result = _quotedUTF8Ref;
if (result == null) {
result = JsonStringEncoder.getInstance().quoteAsUTF8(_value);
result = BufferRecyclers.getJsonStringEncoder().quoteAsUTF8(_value);
_quotedUTF8Ref = result;
}
final int length = result.length;
Expand All @@ -157,7 +158,7 @@ public int appendQuotedUTF8(byte[] buffer, int offset) {
public int appendQuoted(char[] buffer, int offset) {
char[] result = _quotedChars;
if (result == null) {
result = JsonStringEncoder.getInstance().quoteAsString(_value);
result = BufferRecyclers.getJsonStringEncoder().quoteAsString(_value);
_quotedChars = result;
}
final int length = result.length;
Expand All @@ -172,7 +173,7 @@ public int appendQuoted(char[] buffer, int offset) {
public int appendUnquotedUTF8(byte[] buffer, int offset) {
byte[] result = _unquotedUTF8Ref;
if (result == null) {
result = JsonStringEncoder.getInstance().encodeAsUTF8(_value);
result = BufferRecyclers.getJsonStringEncoder().encodeAsUTF8(_value);
_unquotedUTF8Ref = result;
}
final int length = result.length;
Expand All @@ -198,7 +199,7 @@ public int appendUnquoted(char[] buffer, int offset) {
public int writeQuotedUTF8(OutputStream out) throws IOException {
byte[] result = _quotedUTF8Ref;
if (result == null) {
result = JsonStringEncoder.getInstance().quoteAsUTF8(_value);
result = BufferRecyclers.getJsonStringEncoder().quoteAsUTF8(_value);
_quotedUTF8Ref = result;
}
final int length = result.length;
Expand All @@ -210,7 +211,7 @@ public int writeQuotedUTF8(OutputStream out) throws IOException {
public int writeUnquotedUTF8(OutputStream out) throws IOException {
byte[] result = _unquotedUTF8Ref;
if (result == null) {
result = JsonStringEncoder.getInstance().encodeAsUTF8(_value);
result = BufferRecyclers.getJsonStringEncoder().encodeAsUTF8(_value);
_unquotedUTF8Ref = result;
}
final int length = result.length;
Expand All @@ -222,7 +223,7 @@ public int writeUnquotedUTF8(OutputStream out) throws IOException {
public int putQuotedUTF8(ByteBuffer buffer) {
byte[] result = _quotedUTF8Ref;
if (result == null) {
result = JsonStringEncoder.getInstance().quoteAsUTF8(_value);
result = BufferRecyclers.getJsonStringEncoder().quoteAsUTF8(_value);
_quotedUTF8Ref = result;
}
final int length = result.length;
Expand All @@ -237,7 +238,7 @@ public int putQuotedUTF8(ByteBuffer buffer) {
public int putUnquotedUTF8(ByteBuffer buffer) {
byte[] result = _unquotedUTF8Ref;
if (result == null) {
result = JsonStringEncoder.getInstance().encodeAsUTF8(_value);
result = BufferRecyclers.getJsonStringEncoder().encodeAsUTF8(_value);
_unquotedUTF8Ref = result;
}
final int length = result.length;
Expand Down
64 changes: 64 additions & 0 deletions src/main/java/com/fasterxml/jackson/core/util/BufferRecyclers.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.fasterxml.jackson.core.util;

import java.lang.ref.SoftReference;

import com.fasterxml.jackson.core.io.JsonStringEncoder;

/**
* Helper entity used to further
*
* @since 2.9.2
*/
public class BufferRecyclers
{
/*
/**********************************************************
/* BufferRecyclers for parsers, generators
/**********************************************************
*/

/**
* This <code>ThreadLocal</code> contains a {@link java.lang.ref.SoftReference}
* to a {@link BufferRecycler} used to provide a low-cost
* buffer recycling between reader and writer instances.
*/
final protected static ThreadLocal<SoftReference<BufferRecycler>> _recyclerRef
= new ThreadLocal<SoftReference<BufferRecycler>>();

public static BufferRecycler getBufferRecycler()
{
SoftReference<BufferRecycler> ref = _recyclerRef.get();
BufferRecycler br = (ref == null) ? null : ref.get();

if (br == null) {
br = new BufferRecycler();
_recyclerRef.set(new SoftReference<BufferRecycler>(br));
}
return br;
}

/*
/**********************************************************
/* JsonStringEncoder
/**********************************************************
*/

/**
* This <code>ThreadLocal</code> contains a {@link java.lang.ref.SoftReference}
* to a {@link BufferRecycler} used to provide a low-cost
* buffer recycling between reader and writer instances.
*/
final protected static ThreadLocal<SoftReference<JsonStringEncoder>> _encoderRef
= new ThreadLocal<SoftReference<JsonStringEncoder>>();

public static JsonStringEncoder getJsonStringEncoder() {
SoftReference<JsonStringEncoder> ref = _encoderRef.get();
JsonStringEncoder enc = (ref == null) ? null : ref.get();

if (enc == null) {
enc = new JsonStringEncoder();
_encoderRef.set(new SoftReference<JsonStringEncoder>(enc));
}
return enc;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import static org.junit.Assert.*;

import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.util.BufferRecyclers;

public class TestJsonStringEncoder
extends com.fasterxml.jackson.core.BaseTest
Expand Down Expand Up @@ -110,7 +111,7 @@ public void testEncodeAsUTF8() throws Exception
public void testCtrlChars() throws Exception
{
char[] input = new char[] { 0, 1, 2, 3, 4 };
char[] quoted = JsonStringEncoder.getInstance().quoteAsString(new String(input));
char[] quoted = BufferRecyclers.getJsonStringEncoder().quoteAsString(new String(input));
assertEquals("\\u0000\\u0001\\u0002\\u0003\\u0004", new String(quoted));
}

Expand All @@ -121,7 +122,7 @@ public void testCharSequenceWithCtrlChars() throws Exception
StringBuilder builder = new StringBuilder();
builder.append(input);
StringBuilder output = new StringBuilder();
JsonStringEncoder.getInstance().quoteAsString(builder, output);
BufferRecyclers.getJsonStringEncoder().quoteAsString(builder, output);
assertEquals("\\u0000\\u0001\\u0002\\u0003\\u0004", output.toString());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,10 @@
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;

import org.junit.Test;

import static org.junit.Assert.*;
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.core.io.IOContext;
import com.fasterxml.jackson.core.sym.CharsToNameCanonicalizer;
import com.fasterxml.jackson.core.util.BufferRecycler;
import java.io.FileDescriptor;
import java.io.IOException;
import java.io.PipedReader;

/**
* Unit tests for class {@link JsonReadContext}.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.fasterxml.jackson.core.util;

import com.fasterxml.jackson.core.io.NumberInput;
import org.junit.Test;

public class TestTextBuffer
extends com.fasterxml.jackson.core.BaseTest
Expand Down

0 comments on commit e0780ca

Please sign in to comment.