package com.fasterxml.jackson.dataformat.avro.apacheimpl;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Arrays;
import org.apache.avro.io.Decoder;
import org.apache.avro.util.Utf8;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.dataformat.avro.deser.AvroParserImpl;
public class DecoderOverAvroParser extends Decoder {
protected final AvroParserImpl _parser;
public DecoderOverAvroParser(AvroParserImpl parser) {
_parser = parser;
}
protected JsonToken consumeToken(JsonToken... expectedTokens) throws IOException {
JsonToken token = nextValue();
for (JsonToken expectedToken : expectedTokens) {
if (token.equals(expectedToken)) {
_parser.clearCurrentToken();
return token;
}
}
throw new IllegalArgumentException("Expected " + Arrays.toString(expectedTokens) + ", got: " + token);
}
protected JsonToken nextValue() throws IOException {
while (((_parser.currentToken() == JsonToken.START_OBJECT || _parser.currentToken() == JsonToken.END_OBJECT
|| _parser.currentToken() == JsonToken.FIELD_NAME) && _parser.isRecord()) || _parser.currentToken() == null
|| _parser.currentToken() == JsonToken.END_ARRAY) {
_parser.nextToken();
if (_parser.currentToken() == null) {
break;
}
}
return _parser.currentToken();
}
protected JsonToken nextUnionValue() throws IOException {
while (((_parser.currentToken() == JsonToken.END_OBJECT || _parser.currentToken() == JsonToken.FIELD_NAME) && _parser.isRecord())
|| _parser.currentToken() == null || _parser.currentToken() == JsonToken.END_ARRAY) {
_parser.nextToken();
if (_parser.currentToken() == null) {
break;
}
}
return _parser.currentToken();
}
@Override
public void readNull() throws IOException {
consumeToken(JsonToken.VALUE_NULL);
}
@Override
public boolean readBoolean() throws IOException {
return consumeToken(JsonToken.VALUE_FALSE, JsonToken.VALUE_TRUE) == JsonToken.VALUE_TRUE;
}
@Override
public int readInt() throws IOException {
consumeToken(JsonToken.VALUE_NUMBER_INT);
return _parser.getIntValue();
}
@Override
public long readLong() throws IOException {
consumeToken(JsonToken.VALUE_NUMBER_INT);
return _parser.getLongValue();
}
@Override
public float readFloat() throws IOException {
consumeToken(JsonToken.VALUE_NUMBER_FLOAT);
return _parser.getFloatValue();
}
@Override
public double readDouble() throws IOException {
consumeToken(JsonToken.VALUE_NUMBER_FLOAT);
return _parser.getDoubleValue();
}
@Override
public Utf8 readString(Utf8 old) throws IOException {
return new Utf8(readString());
}
@Override
public String readString() throws IOException {
nextValue();
String value = _parser.getText();
_parser.clearCurrentToken();
return value;
}
@Override
public void skipString() throws IOException {
consumeToken(JsonToken.VALUE_STRING);
readString();
}
@Override
public ByteBuffer readBytes(ByteBuffer old) throws IOException {
consumeToken(JsonToken.VALUE_EMBEDDED_OBJECT);
byte[] value = _parser.getBinaryValue();
if ((old != null) && value.length <= old.capacity()) {
old.clear();
old.put(value);
old.flip();
return old;
}
return ByteBuffer.wrap(value);
}
@Override
public void skipBytes() throws IOException {
readBytes(null);
}
@Override
public void readFixed(byte[] bytes, int start, int length) throws IOException {
consumeToken(JsonToken.VALUE_EMBEDDED_OBJECT);
System.arraycopy(_parser.getBinaryValue(), 0, bytes, start, length);
}
@Override
public void skipFixed(int length) throws IOException {
readFixed(new byte[length], 0, length);
}
@Override
public int readEnum() throws IOException {
nextValue();
return _parser.enumIndex();
}
@Override
public long readArrayStart() throws IOException {
consumeToken(JsonToken.START_ARRAY);
return _parser.getRemainingElements();
}
@Override
public long arrayNext() throws IOException {
return _parser.getRemainingElements();
}
@Override
public long skipArray() throws IOException {
consumeToken(JsonToken.START_ARRAY);
_parser.skipValue();
consumeToken(JsonToken.END_ARRAY);
return 0;
}
@Override
public long readMapStart() throws IOException {
consumeToken(JsonToken.START_OBJECT);
return _parser.getRemainingElements();
}
@Override
public long mapNext() throws IOException {
return _parser.getRemainingElements();
}
@Override
public long skipMap() throws IOException {
consumeToken(JsonToken.START_OBJECT);
_parser.skipValue();
consumeToken(JsonToken.END_OBJECT);
return 0;
}
@Override
public int readIndex() throws IOException {
nextUnionValue();
return _parser.branchIndex();
}
}