Skip to content

Commit

Permalink
Start prep work for #400
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Oct 12, 2023
1 parent f4e22ed commit 0ff5f9f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.math.BigInteger;

import org.apache.avro.io.BinaryEncoder;
import org.apache.avro.io.EncoderFactory;

import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.base.GeneratorBase;
Expand Down Expand Up @@ -88,6 +89,11 @@ private Feature(boolean defaultState) {
/**********************************************************
*/

/**
* @since 2.16
*/
protected final static EncoderFactory ENCODER_FACTORY = EncoderFactory.get();

/**
* @since 2.16
*/
Expand Down Expand Up @@ -146,7 +152,12 @@ public AvroGenerator(IOContext ctxt, int jsonFeatures, int avroFeatures,
_formatFeatures = avroFeatures;
_output = output;
_avroContext = AvroWriteContext.nullContext();
_encoder = ApacheCodecRecycler.encoder(_output, isEnabled(Feature.AVRO_BUFFERING));

final boolean buffering = isEnabled(Feature.AVRO_BUFFERING);
BinaryEncoder encoderToReuse = ApacheCodecRecycler.acquireEncoder();
_encoder = buffering
? ENCODER_FACTORY.binaryEncoder(output, encoderToReuse)
: ENCODER_FACTORY.directBinaryEncoder(output, encoderToReuse);
}

public void setSchema(AvroSchema schema)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.fasterxml.jackson.dataformat.avro.apacheimpl;

import java.io.InputStream;
import java.io.OutputStream;
import java.lang.ref.SoftReference;
import java.util.concurrent.atomic.AtomicReference;

Expand All @@ -19,8 +18,6 @@ public final class ApacheCodecRecycler
{
protected final static DecoderFactory DECODER_FACTORY = DecoderFactory.get();

protected final static EncoderFactory ENCODER_FACTORY = EncoderFactory.get();

protected final static ThreadLocal<SoftReference<ApacheCodecRecycler>> _recycler
= new ThreadLocal<SoftReference<ApacheCodecRecycler>>();

Expand Down Expand Up @@ -49,12 +46,8 @@ public static BinaryDecoder decoder(byte[] buffer, int offset, int len)
return DECODER_FACTORY.binaryDecoder(buffer, offset, len, prev);
}

public static BinaryEncoder encoder(OutputStream out, boolean buffering)
{
BinaryEncoder prev = _recycler().claimEncoder();
return buffering
? ENCODER_FACTORY.binaryEncoder(out, prev)
: ENCODER_FACTORY.directBinaryEncoder(out, prev);
public static BinaryEncoder acquireEncoder() {
return _recycler().claimEncoder();
}

public static void release(BinaryDecoder dec) {
Expand Down

0 comments on commit 0ff5f9f

Please sign in to comment.