Java Code Examples for com.google.protobuf.CodedOutputStream#computeRawVarint32Size()

The following examples show how to use com.google.protobuf.CodedOutputStream#computeRawVarint32Size() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RawSerDes.java    From blueflood with Apache License 2.0 6 votes vote down vote up
private int sizeOf(Object obj) throws SerializationException {
    int sz = sizeOfSize();
    sz += sizeOfType();

    if ( obj instanceof Integer ) {
        sz += CodedOutputStream.computeRawVarint32Size((Integer) obj);
    } else if ( obj instanceof Long ) {
        sz += CodedOutputStream.computeRawVarint64Size((Long)obj);
    } else if ( obj instanceof Double ) {
        sz += CodedOutputStream.computeDoubleSizeNoTag((Double)obj);
    } else if ( obj instanceof Float ) {
        sz += CodedOutputStream.computeDoubleSizeNoTag(((Float)obj).doubleValue());
    } else {
        throw new SerializationException("Unexpected type: " + obj.getClass().getName());
    }
    return sz;
}
 
Example 2
Source File: CodedConstant.java    From jprotobuf with Apache License 2.0 6 votes vote down vote up
/**
 * Compute object size no tag.
 *
 * @param o the o
 * @return the int
 */
public static int computeObjectSizeNoTag(Object o) {
    int size = 0;
    if (o == null) {
        return size;
    }

    Class cls = o.getClass();
    Codec target = ProtobufProxy.create(cls);
    try {
        size = target.size(o);
        size = size + CodedOutputStream.computeRawVarint32Size(size);
        return size;
    } catch (IOException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}
 
Example 3
Source File: FanOutOneBlockAsyncDFSOutputSaslHelper.java    From hbase with Apache License 2.0 6 votes vote down vote up
private void sendSaslMessage(ChannelHandlerContext ctx, byte[] payload,
    List<CipherOption> options) throws IOException {
  DataTransferEncryptorMessageProto.Builder builder =
      DataTransferEncryptorMessageProto.newBuilder();
  builder.setStatus(DataTransferEncryptorStatus.SUCCESS);
  if (payload != null) {
    BuilderPayloadSetter.wrapAndSetPayload(builder, payload);
  }
  if (options != null) {
    builder.addAllCipherOption(PBHelperClient.convertCipherOptions(options));
  }
  DataTransferEncryptorMessageProto proto = builder.build();
  int size = proto.getSerializedSize();
  size += CodedOutputStream.computeRawVarint32Size(size);
  ByteBuf buf = ctx.alloc().buffer(size);
  proto.writeDelimitedTo(new ByteBufOutputStream(buf));
  ctx.write(buf);
}
 
Example 4
Source File: SetSerDes.java    From blueflood with Apache License 2.0 5 votes vote down vote up
private int sizeOf(BluefloodSetRollup setRollup) {
    int sz = sizeOfSize();
    sz += CodedOutputStream.computeRawVarint32Size(setRollup.getCount());
    for (Integer i : setRollup.getHashes()) {
        sz += CodedOutputStream.computeRawVarint32Size(i);
    }
    return sz;
}
 
Example 5
Source File: TimerRollupSerDes.java    From blueflood with Apache License 2.0 5 votes vote down vote up
private int sizeOf(BluefloodTimerRollup bluefloodTimerRollup, byte timerVersion) throws SerializationException {

        int sz = sizeOfSize();

        if (timerVersion == VERSION_1_TIMER) {
            sz += CodedOutputStream.computeRawVarint64Size((long) bluefloodTimerRollup.getSum());
        } else if (timerVersion == VERSION_2_TIMER) {
            sz += CodedOutputStream.computeDoubleSizeNoTag(bluefloodTimerRollup.getSum());
        } else {
            throw new SerializationException(String.format("Unexpected timer serialization version: %d", (int)timerVersion));
        }
        sz += CodedOutputStream.computeRawVarint64Size(bluefloodTimerRollup.getCount());
        sz += CodedOutputStream.computeDoubleSizeNoTag(bluefloodTimerRollup.getRate());
        sz += CodedOutputStream.computeRawVarint32Size(bluefloodTimerRollup.getSampleCount());
        sz += averageStatDeSer.sizeOf(bluefloodTimerRollup.getAverage());
        sz += maxStatDeSer.sizeOf(bluefloodTimerRollup.getMaxValue());
        sz += minStatDeSer.sizeOf(bluefloodTimerRollup.getMinValue());
        sz += varianceStatDeSer.sizeOf(bluefloodTimerRollup.getVariance());

        Map<String, BluefloodTimerRollup.Percentile> percentiles = bluefloodTimerRollup.getPercentiles();
        sz += CodedOutputStream.computeRawVarint32Size(bluefloodTimerRollup.getPercentiles().size());
        for (Map.Entry<String, BluefloodTimerRollup.Percentile> entry : percentiles.entrySet()) {
            sz += CodedOutputStream.computeStringSizeNoTag(entry.getKey());
            Number[] pctComponents = new Number[] {
                    entry.getValue().getMean(),
            };
            for (Number num : pctComponents) {
                sz += sizeOfType();
                if (num instanceof Long || num instanceof Integer) {
                    sz += CodedOutputStream.computeRawVarint64Size(num.longValue());
                } else if (num instanceof Double || num instanceof Float) {
                    sz += CodedOutputStream.computeDoubleSizeNoTag(num.doubleValue());
                }
            }
        }
        return sz;
    }
 
Example 6
Source File: CounterSerDes.java    From blueflood with Apache License 2.0 5 votes vote down vote up
private int sizeOf(BluefloodCounterRollup counterRollup) {
    int sz = sizeOfSize();
    sz += sizeOfType();
    if (counterRollup.getCount() instanceof Long || counterRollup.getCount() instanceof Integer)
        sz += CodedOutputStream.computeRawVarint64Size(counterRollup.getCount().longValue());
    else if (counterRollup.getCount() instanceof Double || counterRollup.getCount() instanceof Float)
        sz += CodedOutputStream.computeDoubleSizeNoTag(counterRollup.getCount().doubleValue());
    sz += CodedOutputStream.computeDoubleSizeNoTag(counterRollup.getRate());
    sz += CodedOutputStream.computeRawVarint32Size(counterRollup.getSampleCount());
    return sz;
}
 
Example 7
Source File: ProtobufVarint32LengthFieldPrepender.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
@Override
protected void encode(
        ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) throws Exception {
    int bodyLen = msg.readableBytes();
    int headerLen = CodedOutputStream.computeRawVarint32Size(bodyLen);
    out.ensureWritable(headerLen + bodyLen);

    CodedOutputStream headerOut =
            CodedOutputStream.newInstance(new ByteBufOutputStream(out), headerLen);
    headerOut.writeRawVarint32(bodyLen);
    headerOut.flush();

    out.writeBytes(msg, msg.readerIndex(), bodyLen);
}
 
Example 8
Source File: ProtobufRpcEngine.java    From big-c with Apache License 2.0 5 votes vote down vote up
@Override
public int getLength() {
  int resLen;
  if (theResponse != null) {
    resLen = theResponse.getSerializedSize();
  } else if (theResponseRead != null ) {
    resLen = theResponseRead.length;
  } else {
    throw new IllegalArgumentException(
        "getLength on uninitialized RpcWrapper");      
  }
  return CodedOutputStream.computeRawVarint32Size(resLen) + resLen;
}
 
Example 9
Source File: ProtobufRpcEngine.java    From big-c with Apache License 2.0 5 votes vote down vote up
@Override
public int getLength() {
  int headerLen = requestHeader.getSerializedSize();
  int reqLen;
  if (theRequest != null) {
    reqLen = theRequest.getSerializedSize();
  } else if (theRequestRead != null ) {
    reqLen = theRequestRead.length;
  } else {
    throw new IllegalArgumentException(
        "getLength on uninitialized RpcWrapper");      
  }
  return CodedOutputStream.computeRawVarint32Size(headerLen) +  headerLen
      + CodedOutputStream.computeRawVarint32Size(reqLen) + reqLen;
}
 
Example 10
Source File: ProtobufRpcEngine.java    From hadoop with Apache License 2.0 5 votes vote down vote up
@Override
public int getLength() {
  int resLen;
  if (theResponse != null) {
    resLen = theResponse.getSerializedSize();
  } else if (theResponseRead != null ) {
    resLen = theResponseRead.length;
  } else {
    throw new IllegalArgumentException(
        "getLength on uninitialized RpcWrapper");      
  }
  return CodedOutputStream.computeRawVarint32Size(resLen) + resLen;
}
 
Example 11
Source File: ProtobufRpcEngine.java    From hadoop with Apache License 2.0 5 votes vote down vote up
@Override
public int getLength() {
  int headerLen = requestHeader.getSerializedSize();
  int reqLen;
  if (theRequest != null) {
    reqLen = theRequest.getSerializedSize();
  } else if (theRequestRead != null ) {
    reqLen = theRequestRead.length;
  } else {
    throw new IllegalArgumentException(
        "getLength on uninitialized RpcWrapper");      
  }
  return CodedOutputStream.computeRawVarint32Size(headerLen) +  headerLen
      + CodedOutputStream.computeRawVarint32Size(reqLen) + reqLen;
}
 
Example 12
Source File: Client.java    From big-c with Apache License 2.0 4 votes vote down vote up
private void receiveRpcResponse() {
  if (shouldCloseConnection.get()) {
    return;
  }
  touch();
  
  try {
    int totalLen = in.readInt();
    RpcResponseHeaderProto header = 
        RpcResponseHeaderProto.parseDelimitedFrom(in);
    checkResponse(header);

    int headerLen = header.getSerializedSize();
    headerLen += CodedOutputStream.computeRawVarint32Size(headerLen);

    int callId = header.getCallId();
    if (LOG.isDebugEnabled())
      LOG.debug(getName() + " got value #" + callId);

    Call call = calls.get(callId);
    RpcStatusProto status = header.getStatus();
    if (status == RpcStatusProto.SUCCESS) {
      Writable value = ReflectionUtils.newInstance(valueClass, conf);
      value.readFields(in);                 // read value
      calls.remove(callId);
      call.setRpcResponse(value);
      
      // verify that length was correct
      // only for ProtobufEngine where len can be verified easily
      if (call.getRpcResponse() instanceof ProtobufRpcEngine.RpcWrapper) {
        ProtobufRpcEngine.RpcWrapper resWrapper = 
            (ProtobufRpcEngine.RpcWrapper) call.getRpcResponse();
        if (totalLen != headerLen + resWrapper.getLength()) { 
          throw new RpcClientException(
              "RPC response length mismatch on rpc success");
        }
      }
    } else { // Rpc Request failed
      // Verify that length was correct
      if (totalLen != headerLen) {
        throw new RpcClientException(
            "RPC response length mismatch on rpc error");
      }
      
      final String exceptionClassName = header.hasExceptionClassName() ?
            header.getExceptionClassName() : 
              "ServerDidNotSetExceptionClassName";
      final String errorMsg = header.hasErrorMsg() ? 
            header.getErrorMsg() : "ServerDidNotSetErrorMsg" ;
      final RpcErrorCodeProto erCode = 
                (header.hasErrorDetail() ? header.getErrorDetail() : null);
      if (erCode == null) {
         LOG.warn("Detailed error code not set by server on rpc error");
      }
      RemoteException re = 
          ( (erCode == null) ? 
              new RemoteException(exceptionClassName, errorMsg) :
          new RemoteException(exceptionClassName, errorMsg, erCode));
      if (status == RpcStatusProto.ERROR) {
        calls.remove(callId);
        call.setException(re);
      } else if (status == RpcStatusProto.FATAL) {
        // Close the connection
        markClosed(re);
      }
    }
  } catch (IOException e) {
    markClosed(e);
  }
}
 
Example 13
Source File: FSImageFormatProtobuf.java    From big-c with Apache License 2.0 4 votes vote down vote up
private static int getOndiskTrunkSize(com.google.protobuf.GeneratedMessage s) {
  return CodedOutputStream.computeRawVarint32Size(s.getSerializedSize())
      + s.getSerializedSize();
}
 
Example 14
Source File: CodedConstant.java    From jprotobuf with Apache License 2.0 4 votes vote down vote up
/**
 * get object size by {@link FieldType}.
 *
 * @param order the order
 * @param o the o
 * @param type the type
 * @param list the list
 * @param debug the debug
 * @param path the path
 * @return the int
 */
public static int computeSize(int order, Object o, FieldType type, boolean list, boolean debug, File path) {
    int size = 0;
    if (o == null) {
        return size;
    }

    if (type == FieldType.OBJECT) {
        Class cls = o.getClass();
        Codec target = ProtobufProxy.create(cls, debug, path);
        try {
            size = target.size(o);
            size = size + CodedOutputStream.computeRawVarint32Size(size);
            return size + CodedOutputStream.computeTagSize(order);
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }

    if (type == FieldType.STRING) {
        size = CodedOutputStream.computeStringSizeNoTag(String.valueOf(o));
    } else if (type == FieldType.BOOL) {
        size = CodedOutputStream.computeBoolSizeNoTag(Boolean.valueOf(String.valueOf(o)));
    } else if (type == FieldType.BYTES) {
        byte[] bb = (byte[]) o;
        size = CodedOutputStream.computeBytesSizeNoTag(ByteString.copyFrom(bb));
    } else if (type == FieldType.DOUBLE) {
        size = CodedOutputStream.computeDoubleSizeNoTag(Double.valueOf(o.toString()));
    } else if (type == FieldType.FIXED32 || type == FieldType.INT32 || type == FieldType.SFIXED32
            || type == FieldType.SINT32 || type == FieldType.UINT32) {
        size = CodedOutputStream.computeInt32SizeNoTag(Integer.valueOf(o.toString()));
    } else if (type == FieldType.FIXED64 || type == FieldType.INT64 || type == FieldType.SFIXED64
            || type == FieldType.SINT64 || type == FieldType.UINT64) {
        size = CodedOutputStream.computeInt64SizeNoTag(Long.valueOf(o.toString()));
    } else if (type == FieldType.FLOAT) {
        size = CodedOutputStream.computeFloatSizeNoTag(Float.valueOf(o.toString()));
    } else if (type == FieldType.ENUM) {
        if (o instanceof EnumReadable) {
            size = CodedOutputStream.computeInt32SizeNoTag(((EnumReadable) o).value());
        } else if (o instanceof Enum) {
            size = CodedOutputStream.computeInt32SizeNoTag(((Enum) o).ordinal());
        }
    }

    return size;
}
 
Example 15
Source File: ReflectiveCodec.java    From jprotobuf with Apache License 2.0 4 votes vote down vote up
private int computeSize(FieldInfo fieldInfo, Object value) throws IOException {
	FieldType fieldType = fieldInfo.getFieldType();

	int size = 0;
	if (value instanceof List) {
		// if list
		size = CodedConstant.computeListSize(fieldInfo.getOrder(), (List) value, fieldInfo.getFieldType(), true, null);
		return size;
	}

	int order = fieldInfo.getOrder();
	switch (fieldType) {
	case DOUBLE:
		size = CodedOutputStream.computeDoubleSize(order, (Double) value);
		break;
	case BYTES:
		ByteString bytes = ByteString.copyFrom((byte[]) value);
		size = CodedOutputStream.computeBytesSize(order, bytes);
		break;
	case STRING:
		ByteString string = ByteString.copyFromUtf8(value.toString());
		size = CodedOutputStream.computeBytesSize(order, string);
		break;
	case BOOL:
		size = CodedOutputStream.computeBoolSize(order, (Boolean) value);
		break;
	case FIXED32:
		size = CodedOutputStream.computeFixed32Size(order, (Integer) value);
		break;
	case SFIXED32:
		size = CodedOutputStream.computeSFixed32Size(order, (Integer) value);
		break;
	case SINT32:
		size = CodedOutputStream.computeSInt32Size(order, (Integer) value);
		break;
	case INT32:
		size = CodedOutputStream.computeInt32Size(order, (Integer) value);
		break;
	case UINT32:
		size = CodedOutputStream.computeUInt32Size(order, (Integer) value);
		break;
	case FIXED64:
		size = CodedOutputStream.computeFixed64Size(order, (Long) value);
		break;
	case SFIXED64:
		size = CodedOutputStream.computeSFixed64Size(order, (Long) value);
		break;
	case SINT64:
		size = CodedOutputStream.computeSInt64Size(order, (Long) value);
		break;
	case INT64:
		size = CodedOutputStream.computeInt64Size(order, (Long) value);
		break;
	case UINT64:
		size = CodedOutputStream.computeUInt64Size(order, (Long) value);
		break;
	case ENUM:
		int i;
		i = getEnumValue(value);
		size = CodedOutputStream.computeEnumSize(order, i);
		break;
	case FLOAT:
		size = CodedOutputStream.computeFloatSize(order, (Float) value);
		break;
	case OBJECT:
		Class c = value.getClass();
		ReflectiveCodec codec = new ReflectiveCodec(c);
		
		int objectSize = codec.size(value);
		
		size = size + CodedOutputStream.computeRawVarint32Size(objectSize);
		size = size + CodedOutputStream.computeTagSize(order);
		
		size += objectSize;
		break;
	default:
		throw new IOException("Unknown field type on field '" + fieldInfo.getField().getName() + "'");
	}

	return size;
}
 
Example 16
Source File: CodedConstant.java    From jprotobuf with Apache License 2.0 4 votes vote down vote up
/**
 * get object size by {@link FieldType}
 * 
 * @param o
 * @param type
 * @return
 */
public static int computeSize(int order, Object o, FieldType type, boolean list, boolean debug, File path) {
    int size = 0;
    if (o == null) {
        return size;
    }

    if (type == FieldType.OBJECT) {
        Class cls = o.getClass();
        Codec target = ProtobufProxy.create(cls);
        try {
            size = target.size(o);
            size = size + CodedOutputStream.computeRawVarint32Size(size);
            return size + CodedOutputStream.computeTagSize(order);
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }

    if (type == FieldType.STRING) {
        size = CodedOutputStream.computeStringSizeNoTag(String.valueOf(o));
    } else if (type == FieldType.BOOL) {
        size = CodedOutputStream.computeBoolSizeNoTag(Boolean.valueOf(String.valueOf(o)));
    } else if (type == FieldType.BYTES) {
        byte[] bb = (byte[]) o;
        size = CodedOutputStream.computeBytesSizeNoTag(ByteString.copyFrom(bb));
    } else if (type == FieldType.DOUBLE) {
        size = CodedOutputStream.computeDoubleSizeNoTag(Double.valueOf(o.toString()));
    } else if (type == FieldType.FIXED32 || type == FieldType.INT32 || type == FieldType.SFIXED32
            || type == FieldType.SINT32 || type == FieldType.UINT32) {
        size = CodedOutputStream.computeInt32SizeNoTag(Integer.valueOf(o.toString()));
    } else if (type == FieldType.FIXED64 || type == FieldType.INT64 || type == FieldType.SFIXED64
            || type == FieldType.SINT64 || type == FieldType.UINT64) {
        size = CodedOutputStream.computeInt64SizeNoTag(Long.valueOf(o.toString()));
    } else if (type == FieldType.FLOAT) {
        size = CodedOutputStream.computeFloatSizeNoTag(Float.valueOf(o.toString()));
    } else if (type == FieldType.ENUM) {
        if (o instanceof EnumReadable) {
            size = CodedOutputStream.computeInt32SizeNoTag(((EnumReadable) o).value());
        } else if (o instanceof Enum) {
            size = CodedOutputStream.computeInt32SizeNoTag(((Enum) o).ordinal());
        }
    }

    return size;
}
 
Example 17
Source File: Client.java    From hadoop with Apache License 2.0 4 votes vote down vote up
private void receiveRpcResponse() {
  if (shouldCloseConnection.get()) {
    return;
  }
  touch();
  
  try {
    int totalLen = in.readInt();
    RpcResponseHeaderProto header = 
        RpcResponseHeaderProto.parseDelimitedFrom(in);
    checkResponse(header);

    int headerLen = header.getSerializedSize();
    headerLen += CodedOutputStream.computeRawVarint32Size(headerLen);

    int callId = header.getCallId();
    if (LOG.isDebugEnabled())
      LOG.debug(getName() + " got value #" + callId);

    Call call = calls.get(callId);
    RpcStatusProto status = header.getStatus();
    if (status == RpcStatusProto.SUCCESS) {
      Writable value = ReflectionUtils.newInstance(valueClass, conf);
      value.readFields(in);                 // read value
      calls.remove(callId);
      call.setRpcResponse(value);
      
      // verify that length was correct
      // only for ProtobufEngine where len can be verified easily
      if (call.getRpcResponse() instanceof ProtobufRpcEngine.RpcWrapper) {
        ProtobufRpcEngine.RpcWrapper resWrapper = 
            (ProtobufRpcEngine.RpcWrapper) call.getRpcResponse();
        if (totalLen != headerLen + resWrapper.getLength()) { 
          throw new RpcClientException(
              "RPC response length mismatch on rpc success");
        }
      }
    } else { // Rpc Request failed
      // Verify that length was correct
      if (totalLen != headerLen) {
        throw new RpcClientException(
            "RPC response length mismatch on rpc error");
      }
      
      final String exceptionClassName = header.hasExceptionClassName() ?
            header.getExceptionClassName() : 
              "ServerDidNotSetExceptionClassName";
      final String errorMsg = header.hasErrorMsg() ? 
            header.getErrorMsg() : "ServerDidNotSetErrorMsg" ;
      final RpcErrorCodeProto erCode = 
                (header.hasErrorDetail() ? header.getErrorDetail() : null);
      if (erCode == null) {
         LOG.warn("Detailed error code not set by server on rpc error");
      }
      RemoteException re = 
          ( (erCode == null) ? 
              new RemoteException(exceptionClassName, errorMsg) :
          new RemoteException(exceptionClassName, errorMsg, erCode));
      if (status == RpcStatusProto.ERROR) {
        calls.remove(callId);
        call.setException(re);
      } else if (status == RpcStatusProto.FATAL) {
        // Close the connection
        markClosed(re);
      }
    }
  } catch (IOException e) {
    markClosed(e);
  }
}
 
Example 18
Source File: FSImageFormatProtobuf.java    From hadoop with Apache License 2.0 4 votes vote down vote up
private static int getOndiskTrunkSize(com.google.protobuf.GeneratedMessage s) {
  return CodedOutputStream.computeRawVarint32Size(s.getSerializedSize())
      + s.getSerializedSize();
}