All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.adgear.anoa.read.ProtobufStreams Maven / Gradle / Ivy

package com.adgear.anoa.read;

import com.google.protobuf.Message;
import com.google.protobuf.MessageLite;

import com.adgear.anoa.Anoa;
import com.adgear.anoa.AnoaHandler;
import com.fasterxml.jackson.core.JsonParser;

import java.io.InputStream;
import java.util.stream.Stream;

/**
 * Utility class for deserializing Protobuf records in a {@link java.util.stream.Stream}.
 */
final public class ProtobufStreams {

  private ProtobufStreams() {
  }

  /**
   * Stream from Protobuf delimited binary representations, i.e. as written by {@code
   * MessageLite#writeDelimitedTo(OutputStream)}.
   *
   * @param recordClass Protobuf record class object
   * @param inputStream stream from which to deserialize
   * @param          Protobuf record type
   */
  static public  Stream binary(
      Class recordClass,
      InputStream inputStream) {
    return LookAheadIteratorFactory.protobuf(inputStream, recordClass, false)
        .asStream();
  }


  /**
   * Stream from Protobuf delimited binary representations, i.e. as written by {@code
   * MessageLite#writeDelimitedTo(OutputStream)}. An exception is raised whenever a required field
   * is missing
   *
   * @param recordClass Protobuf record class object
   * @param inputStream stream from which to deserialize
   * @param          Protobuf record type
   */
  @Deprecated
  static public  Stream binaryStrict(
      Class recordClass,
      InputStream inputStream) {
    return LookAheadIteratorFactory.protobuf(inputStream, recordClass, true)
        .asStream();
  }

  /**
   * Stream from Protobuf delimited binary representations, i.e. as written by {@code
   * MessageLite#writeDelimitedTo(OutputStream)}.
   *
   * @param anoaHandler {@code AnoaHandler} instance to use for exception handling
   * @param recordClass Protobuf record class object
   * @param inputStream stream from which to deserialize
   * @param          Protobuf record type
   * @param          Metadata type
   */
  static public  Stream> binary(
      AnoaHandler anoaHandler,
      Class recordClass,
      InputStream inputStream) {
    return LookAheadIteratorFactory.protobuf(anoaHandler, inputStream, recordClass, false)
        .asStream();
  }

  /**
   * Stream from Protobuf delimited binary representations, i.e. as written by {@code
   * MessageLite#writeDelimitedTo(OutputStream)}.
   *
   * @param anoaHandler {@code AnoaHandler} instance to use for exception handling
   * @param recordClass Protobuf record class object
   * @param inputStream stream from which to deserialize
   * @param          Protobuf record type
   * @param          Metadata type
   */
  @Deprecated
  static public  Stream> binaryStrict(
      AnoaHandler anoaHandler,
      Class recordClass,
      InputStream inputStream) {
    return LookAheadIteratorFactory.protobuf(anoaHandler, inputStream, recordClass, true)
        .asStream();
  }

  /**
   * Stream with 'natural' object-mapping from JsonParser instance.
   *
   * @param recordClass   Protobuf record class object
   * @param jacksonParser JsonParser instance from which to read
   * @param            Protobuf record type
   */
  static public  Stream jackson(
      Class recordClass,
      JsonParser jacksonParser) {
    return new ProtobufReader<>(recordClass).stream(jacksonParser);
  }


  /**
   * Stream with 'natural' object-mapping from JsonParser instance, with strict type checking.
   *
   * @param recordClass   Protobuf record class object
   * @param jacksonParser JsonParser instance from which to read
   * @param            Protobuf record type
   */
  static public  Stream jacksonStrict(
      Class recordClass,
      JsonParser jacksonParser) {
    return new ProtobufReader<>(recordClass).streamStrict(jacksonParser);
  }

  /**
   * Stream with 'natural' object-mapping from JsonParser instance.
   *
   * @param anoaHandler   {@code AnoaHandler} instance to use for exception handling
   * @param recordClass   Protobuf record class object
   * @param jacksonParser JsonParser instance from which to read
   * @param            Protobuf record type
   * @param            Metadata type
   */
  static public  Stream> jackson(
      AnoaHandler anoaHandler,
      Class recordClass,
      JsonParser jacksonParser) {
    return new ProtobufReader<>(recordClass).stream(anoaHandler, jacksonParser);
  }

  /**
   * Stream with 'natural' object-mapping from JsonParser instance, with strict type checking.
   *
   * @param anoaHandler   {@code AnoaHandler} instance to use for exception handling
   * @param recordClass   Protobuf record class object
   * @param jacksonParser JsonParser instance from which to read
   * @param            Protobuf record type
   * @param            Metadata type
   */
  static public  Stream> jacksonStrict(
      AnoaHandler anoaHandler,
      Class recordClass,
      JsonParser jacksonParser) {
    return new ProtobufReader<>(recordClass).streamStrict(anoaHandler, jacksonParser);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy