org.eclipse.californium.core.coap.Request.getBytes()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(3.0k)|赞(0)|评价(0)|浏览(208)

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

Request.getBytes介绍

暂无

代码示例

代码示例来源:origin: eclipse/californium

/**
 * Serializes a request and caches the result on the request object to skip future serializations.
 * <p>
 * NB: The byte array cached in the message is encoded according to the specific serializer implementation's
 * supported wire format. Any subsequent invocation of this method with the same request object will therefore
 * simply return the cached byte array. This may cause problems when the first invocation was done on a different
 * type of serializer than the second.
 * <p>
 * Clients should use the {@link #getByteArray(Request)} method in order to prevent caching of the resulting
 * byte array.
 * 
 * @param request The request to serialize.
 * @param outboundCallback The callback to invoke once the message's correlation context
 *                         has been established. 
 * @return The object containing the serialized request and the callback.
 */
public final RawData serializeRequest(final Request request, final MessageCallback outboundCallback) {
  if (request.getBytes() == null) {
    request.setBytes(getByteArray(request));
  }
  return RawData.outbound(
          request.getBytes(),
          new InetSocketAddress(request.getDestination(),
          request.getDestinationPort()),
          outboundCallback,
          false);
}

代码示例来源:origin: org.eclipse.californium/californium-core

/**
 * Serializes the specified request. Message identifier, message code,
 * token, options and payload are converted into a byte array and wrapped in
 * a {@link RawData} object. The request's destination address and port are
 * stored as address and port in the RawData object.
 * 
 * @param request
 *            the request
 * @return the request as raw data
 */
public RawData serialize(Request request) {
  byte[] bytes = request.getBytes();
  if (bytes == null)
    bytes = new DataSerializer().serializeRequest(request);
  request.setBytes(bytes);
  return new RawData(bytes, request.getDestination(), request.getDestinationPort());
}

代码示例来源:origin: eclipse/californium

/**
   * Verifies that the serializeRequest() method sets the Message's <em>bytes</em> property.
   */
  @Test
  public void testSerializeRequestStoresBytesInMessage() {

    // GIVEN a CoAP request
    Request req = Request.newGet();
    req.setToken(new byte[]{0x00});
    req.getOptions().setObserve(0);
    req.setDestination(InetAddress.getLoopbackAddress());

    // WHEN serializing the request to a RawData object
    RawData raw = serializer.serializeRequest(req);

    // THEN the serialized byte array is stored in the request's bytes property
    assertNotNull(req.getBytes());
    assertThat(raw.getBytes(), is(req.getBytes()));
  }
}

代码示例来源:origin: eclipse/californium

/**
 * Verifies that the getByteArray() method does not set the Message's <em>bytes</em> property.
 */
@Test
public void testGetByteArrayDoesNotAlterMessage() {
  // GIVEN a CoAP request
  Request req = Request.newGet();
  req.setToken(new byte[]{0x00});
  req.getOptions().setObserve(0);
  req.setDestination(InetAddress.getLoopbackAddress());
  // WHEN serializing the request to a byte array
  serializer.getByteArray(req);
  // THEN the serialized byte array is not written to the request's bytes property
  assertNull(req.getBytes());
}

相关文章