com.google.protobuf.Message.toByteArray()方法的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(10.0k)|赞(0)|评价(0)|浏览(512)

本文整理了Java中com.google.protobuf.Message.toByteArray()方法的一些代码示例,展示了Message.toByteArray()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Message.toByteArray()方法的具体详情如下:
包路径:com.google.protobuf.Message
类名称:Message
方法名:toByteArray

Message.toByteArray介绍

暂无

代码示例

代码示例来源:origin: voldemort/voldemort

public byte[] toBytes(T object) {
  return object.toByteArray();
}

代码示例来源:origin: redisson/redisson

@Override
  public byte[] apply(Object o) {
    Assert.isInstanceOf(Message.class,
              o,
              "Can only serialize Protobuf messages. " +
                  o.getClass().getName() +
                  " is not an instance of " +
                  Message.class.getName());
    return ((Message)o).toByteArray();
  }
};

代码示例来源:origin: a2888409/face2face

public static ByteBuf pack2Client(Message msg) {
  byte[] bytes = msg.toByteArray();
  int length =bytes.length;
  int ptoNum = ParseMap.getPtoNum(msg);
  ByteBuf buf = Unpooled.buffer(8 + length);
  buf.writeInt(length);
  buf.writeInt(ptoNum);
  buf.writeBytes(bytes);
  return buf;
}

代码示例来源:origin: a2888409/face2face

@Override
  protected void encode(ChannelHandlerContext ctx, Message msg, ByteBuf out)
      throws Exception {

    byte[] bytes = msg.toByteArray();// 将对象转换为byte
    int ptoNum = ParseMap.msg2ptoNum.get(msg);
    int length = bytes.length;

    /* 加密消息体
    ThreeDES des = ctx.channel().attr(ClientAttr.ENCRYPT).get();
    byte[] encryptByte = des.encrypt(bytes);
    int length = encryptByte.length;*/

    ByteBuf buf = Unpooled.buffer(8 + length);
    buf.writeInt(length);
    buf.writeInt(ptoNum);
    buf.writeBytes(bytes);
    out.writeBytes(buf);

    logger.info("GateServer Send Message, remoteAddress: {}, content length {}, ptoNum: {}", ctx.channel().remoteAddress(), length, ptoNum);

  }
}

代码示例来源:origin: apache/hbase

@Override
public int compareTo(Row o) {
 int res = Bytes.compareTo(this.getRow(), o.getRow());
 if ((o instanceof RegionCoprocessorServiceExec) && res == 0) {
  RegionCoprocessorServiceExec exec = (RegionCoprocessorServiceExec) o;
  res = method.getFullName().compareTo(exec.getMethod().getFullName());
  if (res == 0) {
   res = Bytes.compareTo(request.toByteArray(), exec.getRequest().toByteArray());
  }
 }
 return res;
}

代码示例来源:origin: pinterest/secor

@Override
public KeyValue next() throws IOException {
  Builder messageBuilder = (Builder) reader.read();
  if (messageBuilder != null) {
    return new KeyValue(offset++, messageBuilder.build().toByteArray());
  }
  return null;
}

代码示例来源:origin: apache/hbase

public static org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.
  CoprocessorServiceResponse getResponse(final Message result, final byte [] regionName) {
 org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.
  CoprocessorServiceResponse.Builder builder =
   org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.CoprocessorServiceResponse.
   newBuilder();
 builder.setRegion(RequestConverter.buildRegionSpecifier(RegionSpecifierType.REGION_NAME,
  regionName));
 // TODO: UGLY COPY IN HERE!!!!
 builder.setValue(builder.getValueBuilder().setName(result.getClass().getName())
   .setValue(org.apache.hbase.thirdparty.com.google.protobuf.ByteString.
     copyFrom(result.toByteArray())));
 return builder.build();
}

代码示例来源:origin: apache/hbase

@Override
public CoprocessorServiceResponse execService(final RpcController controller,
  final CoprocessorServiceRequest request) throws ServiceException {
 try {
  checkOpen();
  requestCount.increment();
  HRegion region = getRegion(request.getRegion());
  com.google.protobuf.Message result = execServiceOnRegion(region, request.getCall());
  CoprocessorServiceResponse.Builder builder = CoprocessorServiceResponse.newBuilder();
  builder.setRegion(RequestConverter.buildRegionSpecifier(
   RegionSpecifierType.REGION_NAME, region.getRegionInfo().getRegionName()));
  // TODO: COPIES!!!!!!
  builder.setValue(builder.getValueBuilder().setName(result.getClass().getName()).
   setValue(org.apache.hbase.thirdparty.com.google.protobuf.ByteString.
     copyFrom(result.toByteArray())));
  return builder.build();
 } catch (IOException ie) {
  throw new ServiceException(ie);
 }
}

代码示例来源:origin: apache/hbase

private static CoprocessorServiceCall getCoprocessorServiceCall(
  final Descriptors.MethodDescriptor method, final Message request, final byte [] row) {
 return CoprocessorServiceCall.newBuilder()
 .setRow(org.apache.hbase.thirdparty.com.google.protobuf.UnsafeByteOperations.unsafeWrap(row))
 .setServiceName(CoprocessorRpcUtils.getServiceName(method.getService()))
 .setMethodName(method.getName())
 // TODO!!!!! Come back here after!!!!! This is a double copy of the request if I read
 // it right copying from non-shaded to shaded version!!!!!! FIXXXXX!!!!!
 .setRequest(org.apache.hbase.thirdparty.com.google.protobuf.UnsafeByteOperations.
   unsafeWrap(request.toByteArray())).build();
}

代码示例来源:origin: apache/hbase

exec.getRequest().toByteArray());
if (cpBuilder == null) {
 cpBuilder = ClientProtos.CoprocessorServiceCall.newBuilder();

代码示例来源:origin: apache/hbase

exec.getRequest().toByteArray());
if (cpBuilder == null) {
 cpBuilder = ClientProtos.CoprocessorServiceCall.newBuilder();

代码示例来源:origin: apache/hbase

.setName(result.getClass().getName())
    .setValue(UnsafeByteOperations.unsafeWrap(result.toByteArray()))));
} else if (action.hasMutation()) {
 MutationType type = action.getMutation().getMutateType();

代码示例来源:origin: sixt/ja-micro

public byte[] getProtobufData() {
  byte[] envelopeData = getEnvelope().toByteArray();
  byte[] payloadData = getPayload().toByteArray();
  int size = envelopeData.length + payloadData.length + 8;
  byte[] retval = new byte[size];
  int offset = 0;
  System.arraycopy(Ints.toByteArray(envelopeData.length), 0, retval, offset, 4);
  offset += 4;
  System.arraycopy(envelopeData, 0, retval, offset, envelopeData.length);
  offset += envelopeData.length;
  System.arraycopy(Ints.toByteArray(payloadData.length), 0, retval, offset, 4);
  offset += 4;
  System.arraycopy(payloadData, 0, retval, offset, payloadData.length);
  return retval;
}

代码示例来源:origin: org.apache.hbase/hbase-client

@Override
public int compareTo(Row o) {
 int res = Bytes.compareTo(this.getRow(), o.getRow());
 if ((o instanceof RegionCoprocessorServiceExec) && res == 0) {
  RegionCoprocessorServiceExec exec = (RegionCoprocessorServiceExec) o;
  res = method.getFullName().compareTo(exec.getMethod().getFullName());
  if (res == 0) {
   res = Bytes.compareTo(request.toByteArray(), exec.getRequest().toByteArray());
  }
 }
 return res;
}

代码示例来源:origin: sixt/ja-micro

private void sendSuccessfulResponse(HttpServletResponse response,
                  RpcEnvelope.Request rpcRequest,
                  Message pbResponse) throws IOException {
  response.setStatus(HttpServletResponse.SC_OK);
  RpcEnvelope.Response rpcResponse = RpcEnvelope.Response.newBuilder().
      setServiceMethod(rpcRequest.getServiceMethod()).
      setSequenceNumber(rpcRequest.getSequenceNumber()).build();
  byte responseHeader[] = rpcResponse.toByteArray();
  byte responseBody[];
  if (pbResponse == null) {
    responseBody = new byte[0];
  } else {
    responseBody = pbResponse.toByteArray();
  }
  try {
    ServletOutputStream out = response.getOutputStream();
    out.write(Ints.toByteArray(responseHeader.length));
    out.write(responseHeader);
    out.write(Ints.toByteArray(responseBody.length));
    out.write(responseBody);
  } catch (IOException ioex) {
    //there is nothing we can do, client probably went away
    logger.debug("Caught IOException, assuming client disconnected");
  }
}

代码示例来源:origin: palantir/atlasdb

@SuppressWarnings("unchecked")
public byte[] persistJsonToBytes(ClassLoader classLoader, String str) throws ParseException {
  final byte[] bytes;
  if (format == Format.PERSISTABLE) {
    throw new IllegalArgumentException("Tried to pass json into a persistable type.");
  } else if (format == Format.PERSISTER) {
    Persister<?> persister = getPersister();
    if (JsonNode.class == persister.getPersistingClassType()) {
      try {
        JsonNode jsonNode = new ObjectMapper().readValue(str, JsonNode.class);
        return ((Persister<JsonNode>) persister).persistToBytes(jsonNode);
      } catch (IOException e) {
        throw Throwables.throwUncheckedException(e);
      }
    } else {
      throw new IllegalArgumentException("Tried to write json to a Persister that isn't for JsonNode.");
    }
  } else if (format == Format.PROTO) {
    Message.Builder builder = createBuilder(classLoader);
    // This will have issues with base64 blobs
    JsonFormat.merge(str, builder);
    bytes = builder.build().toByteArray();
  } else {
    bytes = type.convertFromString(str);
  }
  return CompressionUtils.compress(bytes, compression);
}

代码示例来源:origin: org.apache.hbase/hbase-client

public static org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.
  CoprocessorServiceResponse getResponse(final Message result, final byte [] regionName) {
 org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.
  CoprocessorServiceResponse.Builder builder =
   org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.CoprocessorServiceResponse.
   newBuilder();
 builder.setRegion(RequestConverter.buildRegionSpecifier(RegionSpecifierType.REGION_NAME,
  regionName));
 // TODO: UGLY COPY IN HERE!!!!
 builder.setValue(builder.getValueBuilder().setName(result.getClass().getName())
   .setValue(org.apache.hbase.thirdparty.com.google.protobuf.ByteString.
     copyFrom(result.toByteArray())));
 return builder.build();
}

代码示例来源:origin: org.apache.hbase/hbase-client

private static CoprocessorServiceCall getCoprocessorServiceCall(
  final Descriptors.MethodDescriptor method, final Message request, final byte [] row) {
 return CoprocessorServiceCall.newBuilder()
 .setRow(org.apache.hbase.thirdparty.com.google.protobuf.UnsafeByteOperations.unsafeWrap(row))
 .setServiceName(CoprocessorRpcUtils.getServiceName(method.getService()))
 .setMethodName(method.getName())
 // TODO!!!!! Come back here after!!!!! This is a double copy of the request if I read
 // it right copying from non-shaded to shaded version!!!!!! FIXXXXX!!!!!
 .setRequest(org.apache.hbase.thirdparty.com.google.protobuf.UnsafeByteOperations.
   unsafeWrap(request.toByteArray())).build();
}

代码示例来源:origin: palantir/atlasdb

public static byte[] deserializeVal(ColumnValueDescription description,
    JsonNode node) {
  byte[] bytes;
  switch (description.getFormat()) {
    case PERSISTABLE:
    case PERSISTER:
      bytes = node.asToken().asByteArray();
      break;
    case PROTO:
      Message.Builder builder = DynamicMessage.newBuilder(description.getProtoDescriptor());
      try {
        ForkedJsonFormat.merge(node.toString(), builder);
      } catch (ForkedJsonFormat.ParseException e) {
        throw Throwables.rewrapAndThrowUncheckedException(e);
      }
      bytes = builder.build().toByteArray();
      break;
    case VALUE_TYPE:
      bytes = description.getValueType().convertFromJson(node.toString());
      break;
    default:
      throw new EnumConstantNotPresentException(Format.class, description.getFormat().name());
  }
  return CompressionUtils.compress(bytes, description.getCompression());
}

代码示例来源:origin: org.apache.hbase/hbase-client

exec.getRequest().toByteArray());
if (cpBuilder == null) {
 cpBuilder = ClientProtos.CoprocessorServiceCall.newBuilder();

相关文章