com.fasterxml.jackson.dataformat.avro.AvroSchema Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jackson-dataformat-avro Show documentation
Show all versions of jackson-dataformat-avro Show documentation
Support for reading and writing AVRO-encoded data via Jackson
abstractions.
package com.fasterxml.jackson.dataformat.avro;
import java.io.*;
import java.lang.ref.SoftReference;
import java.util.concurrent.atomic.AtomicReference;
import com.fasterxml.jackson.core.FormatSchema;
import com.fasterxml.jackson.dataformat.avro.deser.AvroReaderFactory;
import com.fasterxml.jackson.dataformat.avro.deser.AvroStructureReader;
import org.apache.avro.Schema;
import org.apache.avro.io.BinaryDecoder;
import org.apache.avro.io.BinaryEncoder;
import org.apache.avro.io.DecoderFactory;
import org.apache.avro.io.EncoderFactory;
/**
* Wrapper for Schema information needed to encode and decode Avro-format
* data.
*/
public class AvroSchema implements FormatSchema
{
public final static String TYPE_ID = "avro";
protected final static DecoderFactory DECODER_FACTORY = DecoderFactory.get();
protected final static EncoderFactory ENCODER_FACTORY = EncoderFactory.get();
protected final static ThreadLocal> decoderRecycler
= new ThreadLocal>();
protected final static ThreadLocal> encoderRecycler
= new ThreadLocal>();
protected final Schema _avroSchema;
protected final AtomicReference _reader = new AtomicReference();
public AvroSchema(Schema asch)
{
_avroSchema = asch;
}
@Override
public String getSchemaType() {
return TYPE_ID;
}
public Schema getAvroSchema() { return _avroSchema; }
public static BinaryDecoder decoder(InputStream in)
{
SoftReference ref = decoderRecycler.get();
BinaryDecoder prev = (ref == null) ? null : ref.get();
if (prev != null) {
return DECODER_FACTORY.binaryDecoder(in, prev);
}
prev = DECODER_FACTORY.binaryDecoder(in, null);
decoderRecycler.set(new SoftReference(prev));
return prev;
}
public static BinaryDecoder decoder(byte[] buffer, int offset, int len)
{
SoftReference ref = decoderRecycler.get();
BinaryDecoder prev = (ref == null) ? null : ref.get();
if (prev != null) {
return DECODER_FACTORY.binaryDecoder(buffer, offset, len, prev);
}
prev = DECODER_FACTORY.binaryDecoder(buffer, offset, len, null);
decoderRecycler.set(new SoftReference(prev));
return prev;
}
public static BinaryEncoder encoder(OutputStream out)
{
SoftReference ref = encoderRecycler.get();
BinaryEncoder prev = (ref == null) ? null : ref.get();
if (prev != null) {
return ENCODER_FACTORY.binaryEncoder(out, prev);
}
prev = ENCODER_FACTORY.binaryEncoder(out, null);
encoderRecycler.set(new SoftReference(prev));
return prev;
}
public AvroStructureReader getReader()
{
AvroStructureReader r = _reader.get();
if (r == null) {
AvroReaderFactory f = new AvroReaderFactory();
r = f.createReader(_avroSchema);
_reader.set(r);
}
return r;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy