package com.flurry.org.apache.avro.io;

import com.flurry.org.apache.avro.AvroTypeException;
import com.flurry.org.apache.avro.Schema;
import com.flurry.org.apache.avro.io.parsing.JsonGrammarGenerator;
import com.flurry.org.apache.avro.io.parsing.Parser;
import com.flurry.org.apache.avro.io.parsing.Symbol;
import com.flurry.org.apache.avro.util.Utf8;
import com.flurry.org.codehaus.jackson.JsonEncoding;
import com.flurry.org.codehaus.jackson.JsonFactory;
import com.flurry.org.codehaus.jackson.JsonGenerator;
import com.flurry.org.codehaus.jackson.util.MinimalPrettyPrinter;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.util.BitSet;

/* loaded from: /storage/emulated/0/Documents/jadec/sources/com.UBI.A90.WW/dex-files/0.dex */
public class JsonEncoder extends ParsingEncoder implements Parser.ActionHandler {
    protected BitSet isEmpty;
    private JsonGenerator out;
    final Parser parser;

    /* JADX INFO: Access modifiers changed from: package-private */
    public JsonEncoder(Schema sc, OutputStream out) throws IOException {
        this(sc, getJsonGenerator(out));
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public JsonEncoder(Schema sc, JsonGenerator out) throws IOException {
        this.isEmpty = new BitSet();
        configure(out);
        this.parser = new Parser(new JsonGrammarGenerator().generate(sc), this);
    }

    @Override // java.io.Flushable
    public void flush() throws IOException {
        this.parser.processImplicitActions();
        if (this.out != null) {
            this.out.flush();
        }
    }

    private static JsonGenerator getJsonGenerator(OutputStream out) throws IOException {
        if (out == null) {
            throw new NullPointerException("OutputStream cannot be null");
        }
        JsonGenerator g = new JsonFactory().createJsonGenerator(out, JsonEncoding.UTF8);
        MinimalPrettyPrinter pp = new MinimalPrettyPrinter();
        pp.setRootValueSeparator(System.getProperty("line.separator"));
        g.setPrettyPrinter(pp);
        return g;
    }

    public JsonEncoder configure(OutputStream out) throws IOException {
        configure(getJsonGenerator(out));
        return this;
    }

    public JsonEncoder configure(JsonGenerator generator) throws IOException {
        if (generator == null) {
            throw new NullPointerException("JsonGenerator cannot be null");
        }
        if (this.parser != null) {
            flush();
        }
        this.out = generator;
        return this;
    }

    @Override // com.flurry.org.apache.avro.io.Encoder
    public void writeNull() throws IOException {
        this.parser.advance(Symbol.NULL);
        this.out.writeNull();
    }

    @Override // com.flurry.org.apache.avro.io.Encoder
    public void writeBoolean(boolean b) throws IOException {
        this.parser.advance(Symbol.BOOLEAN);
        this.out.writeBoolean(b);
    }

    @Override // com.flurry.org.apache.avro.io.Encoder
    public void writeInt(int n) throws IOException {
        this.parser.advance(Symbol.INT);
        this.out.writeNumber(n);
    }

    @Override // com.flurry.org.apache.avro.io.Encoder
    public void writeLong(long n) throws IOException {
        this.parser.advance(Symbol.LONG);
        this.out.writeNumber(n);
    }

    @Override // com.flurry.org.apache.avro.io.Encoder
    public void writeFloat(float f) throws IOException {
        this.parser.advance(Symbol.FLOAT);
        this.out.writeNumber(f);
    }

    @Override // com.flurry.org.apache.avro.io.Encoder
    public void writeDouble(double d) throws IOException {
        this.parser.advance(Symbol.DOUBLE);
        this.out.writeNumber(d);
    }

    @Override // com.flurry.org.apache.avro.io.Encoder
    public void writeString(Utf8 utf8) throws IOException {
        writeString(utf8.toString());
    }

    @Override // com.flurry.org.apache.avro.io.Encoder
    public void writeString(String str) throws IOException {
        this.parser.advance(Symbol.STRING);
        if (this.parser.topSymbol() == Symbol.MAP_KEY_MARKER) {
            this.parser.advance(Symbol.MAP_KEY_MARKER);
            this.out.writeFieldName(str);
            return;
        }
        this.out.writeString(str);
    }

    @Override // com.flurry.org.apache.avro.io.Encoder
    public void writeBytes(ByteBuffer bytes) throws IOException {
        if (bytes.hasArray()) {
            writeBytes(bytes.array(), bytes.position(), bytes.remaining());
            return;
        }
        byte[] b = new byte[bytes.remaining()];
        for (int i = 0; i < b.length; i++) {
            b[i] = bytes.get();
        }
        writeBytes(b);
    }

    @Override // com.flurry.org.apache.avro.io.Encoder
    public void writeBytes(byte[] bytes, int start, int len) throws IOException {
        this.parser.advance(Symbol.BYTES);
        writeByteArray(bytes, start, len);
    }

    private void writeByteArray(byte[] bytes, int start, int len) throws IOException {
        this.out.writeString(new String(bytes, start, len, "ISO-8859-1"));
    }

    @Override // com.flurry.org.apache.avro.io.Encoder
    public void writeFixed(byte[] bytes, int start, int len) throws IOException {
        this.parser.advance(Symbol.FIXED);
        Symbol.IntCheckAction top = (Symbol.IntCheckAction) this.parser.popSymbol();
        if (len != top.size) {
            throw new AvroTypeException("Incorrect length for fixed binary: expected " + top.size + " but received " + len + " bytes.");
        }
        writeByteArray(bytes, start, len);
    }

    @Override // com.flurry.org.apache.avro.io.Encoder
    public void writeEnum(int e) throws IOException {
        this.parser.advance(Symbol.ENUM);
        Symbol.EnumLabelsAction top = (Symbol.EnumLabelsAction) this.parser.popSymbol();
        if (e < 0 || e >= top.size) {
            throw new AvroTypeException("Enumeration out of range: max is " + top.size + " but received " + e);
        }
        this.out.writeString(top.getLabel(e));
    }

    @Override // com.flurry.org.apache.avro.io.Encoder
    public void writeArrayStart() throws IOException {
        this.parser.advance(Symbol.ARRAY_START);
        this.out.writeStartArray();
        push();
        this.isEmpty.set(depth());
    }

    @Override // com.flurry.org.apache.avro.io.Encoder
    public void writeArrayEnd() throws IOException {
        if (!this.isEmpty.get(this.pos)) {
            this.parser.advance(Symbol.ITEM_END);
        }
        pop();
        this.parser.advance(Symbol.ARRAY_END);
        this.out.writeEndArray();
    }

    @Override // com.flurry.org.apache.avro.io.Encoder
    public void writeMapStart() throws IOException {
        push();
        this.isEmpty.set(depth());
        this.parser.advance(Symbol.MAP_START);
        this.out.writeStartObject();
    }

    @Override // com.flurry.org.apache.avro.io.Encoder
    public void writeMapEnd() throws IOException {
        if (!this.isEmpty.get(this.pos)) {
            this.parser.advance(Symbol.ITEM_END);
        }
        pop();
        this.parser.advance(Symbol.MAP_END);
        this.out.writeEndObject();
    }

    @Override // com.flurry.org.apache.avro.io.ParsingEncoder, com.flurry.org.apache.avro.io.Encoder
    public void startItem() throws IOException {
        if (!this.isEmpty.get(this.pos)) {
            this.parser.advance(Symbol.ITEM_END);
        }
        super.startItem();
        this.isEmpty.clear(depth());
    }

    @Override // com.flurry.org.apache.avro.io.Encoder
    public void writeIndex(int unionIndex) throws IOException {
        this.parser.advance(Symbol.UNION);
        Symbol.Alternative top = (Symbol.Alternative) this.parser.popSymbol();
        Symbol symbol = top.getSymbol(unionIndex);
        if (symbol != Symbol.NULL) {
            this.out.writeStartObject();
            this.out.writeFieldName(top.getLabel(unionIndex));
            this.parser.pushSymbol(Symbol.UNION_END);
        }
        this.parser.pushSymbol(symbol);
    }

    @Override // com.flurry.org.apache.avro.io.parsing.Parser.ActionHandler
    public Symbol doAction(Symbol input, Symbol top) throws IOException {
        if (top instanceof Symbol.FieldAdjustAction) {
            Symbol.FieldAdjustAction fa = (Symbol.FieldAdjustAction) top;
            this.out.writeFieldName(fa.fname);
            return null;
        } else if (top == Symbol.RECORD_START) {
            this.out.writeStartObject();
            return null;
        } else if (top == Symbol.RECORD_END || top == Symbol.UNION_END) {
            this.out.writeEndObject();
            return null;
        } else if (top != Symbol.FIELD_END) {
            throw new AvroTypeException("Unknown action symbol " + top);
        } else {
            return null;
        }
    }
}
