@Override
    public boolean next(LongWritable key, ProtobufWritable<M> value) throws IOException {
      if (pos_ > end_ || fileIn_.available() <= 0) {
        return false;
      }
      key.set(pos_);

      if (Protobufs.KNOWN_GOOD_POSITION_MARKER.length + pos_ <= end_)
        fileIn_.skipBytes(Protobufs.KNOWN_GOOD_POSITION_MARKER.length);

      pos_ = fileIn_.getPos();

      try {
        if (protoBuilder == null) {
          protoBuilder = Protobufs.getMessageBuilder(typeRef_.getRawClass());
        }

        Message.Builder builder = protoBuilder.clone();
        final boolean success = builder.mergeDelimitedFrom(fileIn_);
        if (success) {
          value.set((M) builder.build());
        }
        return success;
      } catch (InvalidProtocolBufferException e) {
        LOG.error(
            "Invalid Protobuf exception while building " + typeRef_.getRawClass().getName(), e);
      } catch (UninitializedMessageException ume) {
        LOG.error(
            "Uninitialized Message Exception while building " + typeRef_.getRawClass().getName(),
            ume);
      }
      return false;
    }
示例#2
0
  protected static void filterValue(
      boolean clearEmpty,
      Predicate<FieldDescriptor> filter,
      Message.Builder builder,
      Map.Entry<FieldDescriptor, Object> entry) {
    FieldDescriptor fd = entry.getKey();
    Object value = entry.getValue();

    if (fd.getType() == FieldDescriptor.Type.MESSAGE) {
      if (fd.isRepeated()) {
        for (Object obj : ((Iterable<?>) value)) {
          Message child = filter((Message) obj, clearEmpty, filter);
          if (child != null) {
            builder.addRepeatedField(fd, child);
          }
        }
      } else {
        Message child = filter((Message) value, clearEmpty, filter);
        if (child != null) {
          builder.setField(fd, child);
        }
      }
    } else {
      builder.setField(fd, value);
    }
  }
 public static void setFieldByName(Message.Builder builder, String name, Object value) {
   FieldDescriptor fieldDescriptor = builder.getDescriptorForType().findFieldByName(name);
   if (value == null) {
     builder.clearField(fieldDescriptor);
   } else {
     builder.setField(fieldDescriptor, value);
   }
 }
示例#4
0
 @Override
 public final void addBinary(Binary value) {
   try {
     ProtobufDatumFactory factory = ProtobufDatumFactory.get(dataType.getCode());
     Message.Builder builder = factory.newBuilder();
     builder.mergeFrom(value.getBytes());
     parent.add(factory.createDatum(builder));
   } catch (InvalidProtocolBufferException e) {
     throw new RuntimeException(e);
   }
 }
 /** Get request protobuf for the RPC method. */
 private Message getRequestProto(SocketRpcProtos.Request rpcRequest, Message requestPrototype)
     throws RpcException {
   Message.Builder builder;
   try {
     builder = requestPrototype.newBuilderForType().mergeFrom(rpcRequest.getRequestProto());
     if (!builder.isInitialized()) {
       throw new RpcException(ErrorReason.BAD_REQUEST_PROTO, "Invalid request proto", null);
     }
   } catch (InvalidProtocolBufferException e) {
     throw new RpcException(ErrorReason.BAD_REQUEST_PROTO, "Invalid request proto", e);
   }
   return builder.build();
 }
 public void deserializeFromByteArray(Message.Builder builder, byte[] array) {
   try {
     builder.mergeFrom(array);
   } catch (InvalidProtocolBufferException e) {
     log.error(e.getMessage());
   }
 }
示例#7
0
 /**
  * Decodes protobuf messages of given type from buffer. If not enough data has been presented
  * delegates to the {@link CumulativeProtocolDecoder} base class to read more data from the wire.
  *
  * <p>It uses instance of internal {@link SizeContext} class to calculate size of buffer expected
  * by the given type of message. The size of every message that arrives on the wire is specified
  * by the prepending varint value.
  *
  * @param session The session used to store internal {@link SizeContext}.
  * @param in The buffer used to read messages if contains enough data.
  * @param out The output for messages decoded from data provided.
  * @see ProtobufEncoder
  * @see SizeContext
  */
 @Override
 protected boolean doDecode(IoSession session, IoBuffer in, ProtocolDecoderOutput out)
     throws Exception {
   SizeContext ctx = SizeContext.get(session, in);
   if (ctx.hasEnoughData(in)) {
     try {
       Message.Builder builder = prototype.newBuilderForType();
       ctx.getInputStream(in).readMessage(builder, extentions);
       out.write(builder.build());
       return true;
     } finally {
       ctx.shiftPositionAndReset(session, in);
     }
   }
   return false;
 }
示例#8
0
  @Nullable
  private static <M extends Message> M filterFrom(
      M msg, boolean clearEmpty, Predicate<FieldDescriptor> filter, int firstDiscarded) {

    // At least one field was discarded; we have to work harder and maybe create
    // a new message that will contain only the retained filters. Use a lazy-allocated
    // builder to also optimize the scenario of all fields being discarded.

    Message.Builder builder = (firstDiscarded == 0) ? null : msg.newBuilderForType();
    Iterator<Map.Entry<FieldDescriptor, Object>> iter = msg.getAllFields().entrySet().iterator();

    for (int i = 0; i < firstDiscarded; ++i) {
      filterValue(clearEmpty, filter, builder, iter.next());
    }

    iter.next(); // Ignore object at firstDiscarded position

    while (iter.hasNext()) {
      Map.Entry<FieldDescriptor, Object> entry = iter.next();

      if (filter.test(entry.getKey())) {
        builder = (builder == null) ? msg.newBuilderForType() : builder;
        filterValue(clearEmpty, filter, builder, entry);
      }
    }

    if (builder == null) {
      if (clearEmpty) {
        return null;
      } else {
        @SuppressWarnings("unchecked")
        M ret = (M) msg.getDefaultInstanceForType();
        return ret;
      }
    } else {
      @SuppressWarnings("unchecked")
      M ret = (M) builder.build();
      return ret;
    }
  }
 public static Message addField(Message m, String name, Object value) {
   Message.Builder builder = m.toBuilder();
   setFieldByName(builder, name, value);
   return builder.build();
 }
 public static Message instantiateFromClassName(String canonicalClassName) {
   Class<? extends Message> protoClass = getInnerProtobufClass(canonicalClassName);
   Message.Builder builder = getMessageBuilder(protoClass);
   return builder.build();
 }