Skip to content

Commit

Permalink
Merge branch '2.16'
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Aug 22, 2023
2 parents 4417b3e + c5f647e commit c0e6b2a
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 20 deletions.
48 changes: 36 additions & 12 deletions src/main/java/tools/jackson/core/TSFBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import java.util.Objects;

import tools.jackson.core.util.BufferRecyclerProvider;
import tools.jackson.core.util.BufferRecyclers;

/**
* Since factory instances are immutable, a Builder class is needed for creating
* configurations for differently configured factory instances.
Expand Down Expand Up @@ -34,6 +37,11 @@ public abstract class TSFBuilder<F extends TokenStreamFactory,
*/
protected int _formatWriteFeatures;

/**
* Buffer recycler provider to use.
*/
protected BufferRecyclerProvider _bufferRecyclerProvider;

/**
* StreamReadConstraints to use.
*/
Expand All @@ -54,32 +62,32 @@ public abstract class TSFBuilder<F extends TokenStreamFactory,
protected TSFBuilder(StreamReadConstraints src, StreamWriteConstraints swc,
ErrorReportConfiguration erc,
int formatReadF, int formatWriteF) {
_streamReadConstraints = Objects.requireNonNull(src);
_streamWriteConstraints = Objects.requireNonNull(swc);
_errorReportConfiguration = Objects.requireNonNull(erc);

_factoryFeatures = TokenStreamFactory.DEFAULT_FACTORY_FEATURE_FLAGS;
_streamReadFeatures = TokenStreamFactory.DEFAULT_STREAM_READ_FEATURE_FLAGS;
_streamWriteFeatures = TokenStreamFactory.DEFAULT_STREAM_WRITE_FEATURE_FLAGS;
_formatReadFeatures = formatReadF;
_formatWriteFeatures = formatWriteF;
this(BufferRecyclers.defaultProvider(),
src, swc, erc,
TokenStreamFactory.DEFAULT_FACTORY_FEATURE_FLAGS,
TokenStreamFactory.DEFAULT_STREAM_READ_FEATURE_FLAGS,
TokenStreamFactory.DEFAULT_STREAM_WRITE_FEATURE_FLAGS,
formatReadF, formatWriteF);
}

protected TSFBuilder(TokenStreamFactory base)
{
this(base._streamReadConstraints, base._streamWriteConstraints,
this(base._bufferRecyclerProvider,
base._streamReadConstraints, base._streamWriteConstraints,
base._errorReportConfiguration,
base._factoryFeatures,
base._streamReadFeatures, base._streamWriteFeatures,
base._formatReadFeatures, base._formatWriteFeatures);
}

protected TSFBuilder(StreamReadConstraints src, StreamWriteConstraints swc,
protected TSFBuilder(BufferRecyclerProvider brp,
StreamReadConstraints src, StreamWriteConstraints swc,
ErrorReportConfiguration erc,
int factoryFeatures,
int streamReadFeatures, int streamWriteFeatures,
int formatReadFeatures, int formatWriteFeatures)
{
_bufferRecyclerProvider = Objects.requireNonNull(brp);
_streamReadConstraints = Objects.requireNonNull(src);
_streamWriteConstraints = Objects.requireNonNull(swc);
_errorReportConfiguration = Objects.requireNonNull(erc);
Expand All @@ -99,6 +107,10 @@ protected TSFBuilder(StreamReadConstraints src, StreamWriteConstraints swc,
public int formatReadFeaturesMask() { return _formatReadFeatures; }
public int formatWriteFeaturesMask() { return _formatWriteFeatures; }

public BufferRecyclerProvider bufferRecyclerProvider() {
return _bufferRecyclerProvider;
}

// // // Factory features

public B enable(TokenStreamFactory.Feature f) {
Expand Down Expand Up @@ -179,7 +191,7 @@ public B configure(StreamWriteFeature f, boolean state) {
return state ? enable(f) : disable(f);
}

// // // Other configuration
// // // Other configuration, constraints

/**
* Sets the constraints for streaming reads.
Expand Down Expand Up @@ -216,6 +228,18 @@ public B errorReportConfiguration(ErrorReportConfiguration errorReportConfigurat
return _this();
}

// // // Other configuration, helper objects

/**
* @param p BufferRecyclerProvider to use for buffer allocation
*
* @return this builder (for call chaining)
*/
public B bufferRecyclerProvider(BufferRecyclerProvider p) {
_bufferRecyclerProvider = Objects.requireNonNull(p);
return _this();
}

// // // Other methods

/**
Expand Down
30 changes: 24 additions & 6 deletions src/main/java/tools/jackson/core/TokenStreamFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import tools.jackson.core.sym.PropertyNameMatcher;
import tools.jackson.core.sym.SimpleNameMatcher;
import tools.jackson.core.util.BufferRecycler;
import tools.jackson.core.util.BufferRecyclerProvider;
import tools.jackson.core.util.BufferRecyclers;
import tools.jackson.core.util.JacksonFeature;
import tools.jackson.core.util.Named;
Expand Down Expand Up @@ -167,8 +168,6 @@ public static int collectDefaults() {
public int getMask() { return (1 << ordinal()); }
}



/*
/**********************************************************************
/* Constants
Expand All @@ -194,7 +193,7 @@ public static int collectDefaults() {

/*
/**********************************************************************
/* Configuration
/* Configuration, simple features
/**********************************************************************
*/

Expand Down Expand Up @@ -223,6 +222,20 @@ public static int collectDefaults() {
*/
protected final int _formatWriteFeatures;

/*
/**********************************************************************
/* Configuration, providers
/**********************************************************************
*/

protected final BufferRecyclerProvider _bufferRecyclerProvider;

/*
/**********************************************************************
/* Configuration, constraints
/**********************************************************************
*/

/**
* Active StreamReadConstraints to use.
*/
Expand Down Expand Up @@ -264,6 +277,7 @@ protected TokenStreamFactory(StreamReadConstraints src, StreamWriteConstraints s
_streamReadConstraints = Objects.requireNonNull(src);
_streamWriteConstraints = Objects.requireNonNull(swc);
_errorReportConfiguration = Objects.requireNonNull(erc);
_bufferRecyclerProvider = BufferRecyclers.defaultProvider();
_factoryFeatures = DEFAULT_FACTORY_FEATURE_FLAGS;
_streamReadFeatures = DEFAULT_STREAM_READ_FEATURE_FLAGS;
_streamWriteFeatures = DEFAULT_STREAM_WRITE_FEATURE_FLAGS;
Expand All @@ -287,6 +301,8 @@ protected TokenStreamFactory(TSFBuilder<?,?> baseBuilder)
_streamReadConstraints = Objects.requireNonNull(baseBuilder._streamReadConstraints);
_streamWriteConstraints = Objects.requireNonNull(baseBuilder._streamWriteConstraints);
_errorReportConfiguration = Objects.requireNonNull(baseBuilder._errorReportConfiguration);
_bufferRecyclerProvider = Objects.requireNonNull(baseBuilder._bufferRecyclerProvider);

_factoryFeatures = baseBuilder.factoryFeaturesMask();
_streamReadFeatures = baseBuilder.streamReadFeaturesMask();
_streamWriteFeatures = baseBuilder.streamWriteFeaturesMask();
Expand All @@ -305,6 +321,8 @@ protected TokenStreamFactory(TokenStreamFactory src)
_streamReadConstraints = src._streamReadConstraints;
_streamWriteConstraints = src._streamWriteConstraints;
_errorReportConfiguration = src._errorReportConfiguration;
_bufferRecyclerProvider = src._bufferRecyclerProvider;

_factoryFeatures = src._factoryFeatures;
_streamReadFeatures = src._streamReadFeatures;
_streamWriteFeatures = src._streamWriteFeatures;
Expand Down Expand Up @@ -1204,10 +1222,10 @@ public BufferRecycler _getBufferRecycler()
// 23-Apr-2015, tatu: Let's allow disabling of buffer recycling
// scheme, for cases where it is considered harmful (possibly
// on Android, for example)
if (Feature.USE_THREAD_LOCAL_FOR_BUFFER_RECYCLING.enabledIn(_factoryFeatures)) {
return BufferRecyclers.getBufferRecycler();
if (!Feature.USE_THREAD_LOCAL_FOR_BUFFER_RECYCLING.enabledIn(_factoryFeatures)) {
return new BufferRecycler();
}
return new BufferRecycler();
return _bufferRecyclerProvider.acquireBufferRecycler(this);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package tools.jackson.core.json;

import static tools.jackson.core.JsonTokenId.*;

import java.io.*;

import tools.jackson.core.*;
Expand All @@ -12,6 +10,8 @@
import tools.jackson.core.sym.ByteQuadsCanonicalizer;
import tools.jackson.core.util.*;

import static tools.jackson.core.JsonTokenId.*;

/**
* This is a concrete implementation of {@link JsonParser}, which is
* based on a {@link java.io.DataInput} as the input source.
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/tools/jackson/core/util/BufferRecyclerProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package tools.jackson.core.util;

import tools.jackson.core.TokenStreamFactory;

/**
* Provider for {@link BufferRecycler}s to allow pluggable and optional
* recycling of underlying input/output buffers.
*/
public interface BufferRecyclerProvider
extends java.io.Serializable
{
public abstract BufferRecycler acquireBufferRecycler(TokenStreamFactory forFactory);
}
33 changes: 33 additions & 0 deletions src/main/java/tools/jackson/core/util/BufferRecyclers.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.lang.ref.SoftReference;

import tools.jackson.core.TokenStreamFactory;

/**
* Helper entity used to control access to simple buffer recycling scheme used for
* some encoding, decoding tasks.
Expand Down Expand Up @@ -55,7 +57,11 @@ public class BufferRecyclers
* Main accessor to call for accessing possibly recycled {@link BufferRecycler} instance.
*
* @return {@link BufferRecycler} to use
*
* @deprecated Since 2.16 should use {@link BufferRecyclerProvider} abstraction instead
* of calling static methods of this class
*/
@Deprecated // since 2.16
public static BufferRecycler getBufferRecycler()
{
SoftReference<BufferRecycler> ref = _recyclerRef.get();
Expand Down Expand Up @@ -92,4 +98,31 @@ public static int releaseBuffers() {
}
return -1;
}

/*
/**********************************************************************
/* Default BufferRecyclerProvider implementations
/**********************************************************************
*/

public static BufferRecyclerProvider defaultProvider() {
return ThreadLocalBufferRecyclerProvider.INSTANCE;
}

/**
* Default {@link BufferRecyclerProvider} implementation that uses
* {@link ThreadLocal} for recycling instances.
*/
public static class ThreadLocalBufferRecyclerProvider
implements BufferRecyclerProvider
{
private static final long serialVersionUID = 1L;

public final static ThreadLocalBufferRecyclerProvider INSTANCE = new ThreadLocalBufferRecyclerProvider();

@Override
public BufferRecycler acquireBufferRecycler(TokenStreamFactory forFactory) {
return getBufferRecycler();
}
}
}

0 comments on commit c0e6b2a

Please sign in to comment.