Package org.apache.avro.io

Source Code of org.apache.avro.io.ValidatingEncoder

/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.avro.io;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.util.BitSet;

import org.apache.avro.AvroTypeException;
import org.apache.avro.Schema;
import org.apache.avro.io.parsing.ValidatingGrammarGenerator;
import org.apache.avro.io.parsing.Parser;
import org.apache.avro.io.parsing.Symbol;
import org.apache.avro.util.Utf8;

/**
* An implementation of {@link Encoder} that ensures that the sequence
* of operations conforms to a schema.
*/
public class ValidatingEncoder extends ParsingEncoder
  implements Parser.ActionHandler {
  protected Encoder out;
  protected final Parser parser;
  /**
   * Has anything been written into the collections?
   */
  protected BitSet isEmpty = new BitSet();

  ValidatingEncoder(Symbol root, Encoder out) throws IOException {
    this.out = out;
    this.parser = new Parser(root, this);
  }

  public ValidatingEncoder(Schema schema, Encoder in) throws IOException {
    this(new ValidatingGrammarGenerator().generate(schema), in);
  }

  @Override
  public void flush() throws IOException {
    out.flush();
  }

  @Override
  public void init(OutputStream out) throws IOException {
    flush();
    parser.reset();
    this.out.init(out);
  }

  @Override
  public void writeNull() throws IOException {
    parser.advance(Symbol.NULL);
    out.writeNull();
  }

  @Override
  public void writeBoolean(boolean b) throws IOException {
    parser.advance(Symbol.BOOLEAN);
    out.writeBoolean(b);
  }

  @Override
  public void writeInt(int n) throws IOException {
    parser.advance(Symbol.INT);
    out.writeInt(n);
  }

  @Override
  public void writeLong(long n) throws IOException {
    parser.advance(Symbol.LONG);
    out.writeLong(n);
  }

  @Override
  public void writeFloat(float f) throws IOException {
    parser.advance(Symbol.FLOAT);
    out.writeFloat(f);
  }

  @Override
  public void writeDouble(double d) throws IOException {
    parser.advance(Symbol.DOUBLE);
    out.writeDouble(d);
  }

  @Override
  public void writeString(Utf8 utf8) throws IOException {
    parser.advance(Symbol.STRING);
    out.writeString(utf8);
  }

  @Override
  public void writeBytes(ByteBuffer bytes) throws IOException {
    parser.advance(Symbol.BYTES);
    out.writeBytes(bytes);
  }

  @Override
  public void writeBytes(byte[] bytes, int start, int len) throws IOException {
    parser.advance(Symbol.BYTES);
    out.writeBytes(bytes, start, len);
  }

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

  @Override
  public void writeEnum(int e) throws IOException {
    parser.advance(Symbol.ENUM);
    Symbol.IntCheckAction top = (Symbol.IntCheckAction) parser.popSymbol();
    if (e < 0 || e >= top.size) {
      throw new AvroTypeException(
          "Enumeration out of range: max is " +
          top.size + " but received " + e);
    }
    out.writeEnum(e);
  }

  @Override
  public void writeArrayStart() throws IOException {
    push();
    parser.advance(Symbol.ARRAY_START);
    out.writeArrayStart();
  }

  @Override
  public void writeArrayEnd() throws IOException {
    parser.advance(Symbol.ARRAY_END);
    out.writeArrayEnd();
    pop();
  }

  @Override
  public void writeMapStart() throws IOException {
    push();
    parser.advance(Symbol.MAP_START);
    out.writeMapStart();
  }

  @Override
  public void writeMapEnd() throws IOException {
    parser.advance(Symbol.MAP_END);
    out.writeMapEnd();
    pop();
  }

  @Override
  public void setItemCount(long itemCount) throws IOException {
    super.setItemCount(itemCount);
    out.setItemCount(itemCount);
  }

  @Override
  public void startItem() throws IOException {
    super.startItem();
    out.startItem();
  }

  @Override
  public void writeIndex(int unionIndex) throws IOException {
    parser.advance(Symbol.UNION);
    Symbol.Alternative top = (Symbol.Alternative) parser.popSymbol();
    parser.pushSymbol(top.getSymbol(unionIndex));
    out.writeIndex(unionIndex);
  }

  @Override
  public Symbol doAction(Symbol input, Symbol top) throws IOException {
    return Symbol.CONTINUE;
  }

  /** Have we written at least one item into the current collection? */
  protected final boolean isTopEmpty() {
    return isEmpty.get(pos);
  }
}
TOP

Related Classes of org.apache.avro.io.ValidatingEncoder

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.