From 323373de56346ede8d2642c5b59df88136ff66d6 Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Wed, 11 Oct 2023 19:15:14 -0700 Subject: [PATCH] Continue refactoring to help #400 --- .../avro/apacheimpl/ApacheAvroParserImpl.java | 18 ++++++++++++++---- .../avro/apacheimpl/ApacheCodecRecycler.java | 17 ++--------------- 2 files changed, 16 insertions(+), 19 deletions(-) diff --git a/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/apacheimpl/ApacheAvroParserImpl.java b/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/apacheimpl/ApacheAvroParserImpl.java index 82c47ef32..4c49115d9 100644 --- a/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/apacheimpl/ApacheAvroParserImpl.java +++ b/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/apacheimpl/ApacheAvroParserImpl.java @@ -5,6 +5,7 @@ import java.io.Writer; import org.apache.avro.io.BinaryDecoder; +import org.apache.avro.io.DecoderFactory; import com.fasterxml.jackson.core.*; import com.fasterxml.jackson.core.io.IOContext; @@ -17,6 +18,11 @@ */ public class ApacheAvroParserImpl extends AvroParserImpl { + /** + * @since 2.16 + */ + protected final static DecoderFactory DECODER_FACTORY = DecoderFactory.get(); + /* /********************************************************** /* Input source config @@ -74,17 +80,21 @@ public ApacheAvroParserImpl(IOContext ctxt, int parserFeatures, int avroFeatures _inputEnd = 0; _bufferRecyclable = true; - _decoder = ApacheCodecRecycler.decoder(in, - Feature.AVRO_BUFFERING.enabledIn(avroFeatures)); + final boolean buffering = Feature.AVRO_BUFFERING.enabledIn(avroFeatures); + BinaryDecoder decoderToReuse = ApacheCodecRecycler.acquireDecoder(); + _decoder = buffering + ? DECODER_FACTORY.binaryDecoder(in, decoderToReuse) + : DECODER_FACTORY.directBinaryDecoder(in, decoderToReuse); } public ApacheAvroParserImpl(IOContext ctxt, int parserFeatures, int avroFeatures, ObjectCodec codec, - byte[] data, int offset, int len) + byte[] buffer, int offset, int len) { super(ctxt, parserFeatures, avroFeatures, codec); _inputStream = null; - _decoder = ApacheCodecRecycler.decoder(data, offset, len); + BinaryDecoder decoderToReuse = ApacheCodecRecycler.acquireDecoder(); + _decoder = DECODER_FACTORY.binaryDecoder(buffer, offset, len, decoderToReuse); } @Override diff --git a/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/apacheimpl/ApacheCodecRecycler.java b/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/apacheimpl/ApacheCodecRecycler.java index e25603336..a655d217b 100644 --- a/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/apacheimpl/ApacheCodecRecycler.java +++ b/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/apacheimpl/ApacheCodecRecycler.java @@ -1,6 +1,5 @@ package com.fasterxml.jackson.dataformat.avro.apacheimpl; -import java.io.InputStream; import java.lang.ref.SoftReference; import java.util.concurrent.atomic.AtomicReference; @@ -16,8 +15,6 @@ */ public final class ApacheCodecRecycler { - protected final static DecoderFactory DECODER_FACTORY = DecoderFactory.get(); - protected final static ThreadLocal> _recycler = new ThreadLocal>(); @@ -32,18 +29,8 @@ private ApacheCodecRecycler() { } /********************************************************** */ - public static BinaryDecoder decoder(InputStream in, boolean buffering) - { - BinaryDecoder prev = _recycler().claimDecoder(); - return buffering - ? DECODER_FACTORY.binaryDecoder(in, prev) - : DECODER_FACTORY.directBinaryDecoder(in, prev); - } - - public static BinaryDecoder decoder(byte[] buffer, int offset, int len) - { - BinaryDecoder prev = _recycler().claimDecoder(); - return DECODER_FACTORY.binaryDecoder(buffer, offset, len, prev); + public static BinaryDecoder acquireDecoder() { + return _recycler().claimDecoder(); } public static BinaryEncoder acquireEncoder() {