Java Code Examples for io.vertx.core.buffer.Buffer#appendByte()

The following examples show how to use io.vertx.core.buffer.Buffer#appendByte() . 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: TestVertxServerRequestToHttpServletRequest.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetInputStream() throws IOException {
  Buffer body = Buffer.buffer();
  body.appendByte((byte) 1);

  new Expectations() {
    {
      context.getBody();
      result = body;
    }
  };

  ServletInputStream is1 = request.getInputStream();
  Assert.assertSame(is1, request.getInputStream());
  int value = is1.read();
  is1.close();
  Assert.assertEquals(1, value);
  Assert.assertSame(is1, request.getInputStream());

  request.setBodyBuffer(Buffer.buffer().appendByte((byte)2));
  ServletInputStream is2 = request.getInputStream();
  Assert.assertNotSame(is1, is2);
}
 
Example 2
Source File: UserHolder.java    From vertx-web with Apache License 2.0 6 votes vote down vote up
@Override
public void writeToBuffer(Buffer buffer) {
  // try to get the user from the context otherwise fall back to any cached version
  final User user;

  synchronized (this) {
    user = context != null ? context.user() : this.user;
    // clear the context as this holder is not in a request anymore
    context = null;
  }

  if (user instanceof ClusterSerializable) {
    buffer.appendByte((byte)1);
    String className = user.getClass().getName();
    if (className == null) {
      throw new IllegalStateException("Cannot serialize " + user.getClass().getName());
    }
    byte[] bytes = className.getBytes(StandardCharsets.UTF_8);
    buffer.appendInt(bytes.length);
    buffer.appendBytes(bytes);
    ClusterSerializable cs = (ClusterSerializable)user;
    cs.writeToBuffer(buffer);
  } else {
    buffer.appendByte((byte)0);
  }
}
 
Example 3
Source File: Packet.java    From besu with Apache License 2.0 5 votes vote down vote up
public Buffer encode() {
  final Bytes encodedSignature = encodeSignature(signature);
  final BytesValueRLPOutput encodedData = new BytesValueRLPOutput();
  data.writeTo(encodedData);

  final Buffer buffer =
      Buffer.buffer(hash.size() + encodedSignature.size() + 1 + encodedData.encodedSize());
  hash.appendTo(buffer);
  encodedSignature.appendTo(buffer);
  buffer.appendByte(type.getValue());
  appendEncoded(encodedData, buffer);
  return buffer;
}
 
Example 4
Source File: TestVertxUtils.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
@Test
public void testgetBytesFastBuffer() {
  Buffer buffer = Buffer.buffer();
  buffer.appendByte((byte) 1);

  byte[] result = VertxUtils.getBytesFast(buffer);
  Assert.assertEquals(1, result[0]);
}
 
Example 5
Source File: TestStandardHttpServletResponseEx.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
@Test
public void flushBuffer() throws IOException {
  Buffer buffer = Buffer.buffer();
  ServletOutputStream output = new ServletOutputStream() {
    @Override
    public boolean isReady() {
      return true;
    }

    @Override
    public void setWriteListener(WriteListener writeListener) {

    }

    @Override
    public void write(int b) throws IOException {
      buffer.appendByte((byte) b);
    }
  };
  response = new MockUp<HttpServletResponse>() {
    @Mock
    ServletOutputStream getOutputStream() {
      return output;
    }
  }.getMockInstance();
  responseEx = new StandardHttpServletResponseEx(response);

  // no body
  responseEx.flushBuffer();
  Assert.assertEquals(0, buffer.length());

  Buffer body = Buffer.buffer().appendString("body");
  responseEx.setBodyBuffer(body);
  responseEx.flushBuffer();
  Assert.assertEquals("body", buffer.toString());
}
 
Example 6
Source File: DataTypeCodec.java    From vertx-sql-client with Apache License 2.0 5 votes vote down vote up
/**
 * Decode the specified {@code buff} formatted as an hex string starting at the buffer readable index
 * with the specified {@code length} to a {@link Buffer}.
 *
 * @param len the hex string length
 * @param buff the byte buff to read from
 * @return the decoded value as a Buffer
 */
private static Buffer decodeHexStringToBytes(int index, int len, ByteBuf buff) {
  len = len >> 1;
  Buffer buffer = Buffer.buffer(len);
  for (int i = 0; i < len; i++) {
    byte b0 = decodeHexChar(buff.getByte(index++));
    byte b1 = decodeHexChar(buff.getByte(index++));
    buffer.appendByte((byte) (b0 * 16 + b1));
  }
  return buffer;
}
 
Example 7
Source File: DataTypeCodec.java    From vertx-sql-client with Apache License 2.0 5 votes vote down vote up
private static Buffer decodeEscapeByteaStringToBuffer(int index, int len, ByteBuf buff) {
  Buffer buffer = Buffer.buffer();

  int pos = 0;
  while (pos < len) {
    byte current = buff.getByte(pos + index);

    if (current == '\\') {
      if (pos + 2 <= len && buff.getByte(pos + index + 1) == '\\') {
        // check double backslashes
        buffer.appendByte((byte) '\\');
        pos += 2;
      } else if (pos + 4 <= len) {
        // a preceded backslash with three-digit octal value
        int high = Character.digit(buff.getByte(pos + index + 1), 8) << 6;
        int medium = Character.digit(buff.getByte(pos + index + 2), 8) << 3;
        int low = Character.digit(buff.getByte(pos + index + 3), 8);
        int escapedValue = high + medium + low;

        buffer.appendByte((byte) escapedValue);
        pos += 4;
      } else {
        throw new DecoderException("Decoding unexpected BYTEA escape format");
      }
    } else {
      // printable octets
      buffer.appendByte(current);
      pos++;
    }
  }

  return buffer;
}
 
Example 8
Source File: UtilTest.java    From vertx-sql-client with Apache License 2.0 5 votes vote down vote up
@Test
public void testWriteHexString() {
  assertWriteHexString("00", (byte) 0);
  assertWriteHexString("01", (byte) 1);
  assertWriteHexString("0a", (byte) 10);
  assertWriteHexString("10", (byte) 16);
  assertWriteHexString("ff", (byte) 255);
  assertWriteHexString("ff0a0a", (byte) 255, (byte)10, (byte)10);
  Buffer buff = Buffer.buffer();
  for (int i = 0; i < 512;i++) {
    buff.appendByte((byte)('A' + i % 26));
  }
}
 
Example 9
Source File: AuthenticationConstants.java    From hono with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Parses the SASL response and extracts the authzid, authcid and pwd from the response.
 * <p>
 * <a href="https://tools.ietf.org/html/rfc4616">The specification for the SASL PLAIN mechanism</a> mandates the format
 * of the credentials to be of the form: {@code [authzid] UTF8NUL authcid UTF8NUL passwd}.
 *
 * @param saslResponse The SASL response to parse.
 * @return A String array containing the elements in the SASL response.
 *
 * @throws CredentialException If one the elements (authzid, authcid and pwd) is missing from the SASL response.
 */
public static String[] parseSaslResponse(final byte[] saslResponse) throws CredentialException {
    final List<String> fields = new ArrayList<>();
    int pos = 0;
    Buffer b = Buffer.buffer();
    while (pos < saslResponse.length) {
        final byte val = saslResponse[pos];
        if (val == 0x00) {
            fields.add(b.toString(StandardCharsets.UTF_8));
            b = Buffer.buffer();
        } else {
            b.appendByte(val);
        }
        pos++;
    }
    fields.add(b.toString(StandardCharsets.UTF_8));

    if (fields.size() != 3) {
        throw new CredentialException("client provided malformed PLAIN response");
    } else if (fields.get(1) == null || fields.get(1).length() == 0) {
        throw new CredentialException("PLAIN response must contain an authentication ID");
    } else if (fields.get(2) == null || fields.get(2).length() == 0) {
        throw new CredentialException("PLAIN response must contain a password");
    } else {
        return fields.toArray(new String[fields.size()]);
    }
}
 
Example 10
Source File: ServiceExceptionMessageCodec.java    From vertx-service-proxy with Apache License 2.0 5 votes vote down vote up
@Override
public void encodeToWire(Buffer buffer, ServiceException body) {
  buffer.appendInt(body.failureCode());
  if (body.getMessage() == null) {
    buffer.appendByte((byte)0);
  } else {
    buffer.appendByte((byte)1);
    byte[] encoded = body.getMessage().getBytes(CharsetUtil.UTF_8);
    buffer.appendInt(encoded.length);
    buffer.appendBytes(encoded);
  }
  body.getDebugInfo().writeToBuffer(buffer);
}
 
Example 11
Source File: ClientHandler.java    From shadowsocks-vertx with Apache License 2.0 4 votes vote down vote up
private boolean handleStageAddress() {
    int bufferLength = mPlainTextBufferQ.length();
    String addr = null;
    // Construct the remote header.
    Buffer remoteHeader = Buffer.buffer();
    int addrType = mPlainTextBufferQ.getByte(0);

    remoteHeader.appendByte((byte)(addrType));

    if (addrType == ADDR_TYPE_IPV4) {
        // addr type (1) + ipv4(4) + port(2)
        if (bufferLength < 7)
            return false;
        try{
            addr = InetAddress.getByAddress(mPlainTextBufferQ.getBytes(1, 5)).toString();
        }catch(UnknownHostException e){
            log.error("UnknownHostException:" + e.toString());
            return true;
        }
        remoteHeader.appendBytes(mPlainTextBufferQ.getBytes(1,5));
        compactPlainTextBufferQ(5);
    }else if (addrType == ADDR_TYPE_HOST) {
        short hostLength = mPlainTextBufferQ.getUnsignedByte(1);
        // addr type(1) + len(1) + host + port(2)
        if (bufferLength < hostLength + 4)
            return false;
        addr = mPlainTextBufferQ.getString(2, hostLength + 2);
        remoteHeader.appendByte((byte)hostLength).appendString(addr);
        compactPlainTextBufferQ(hostLength + 2);
    }else {
        log.warn("Unsupport addr type " + addrType);
        return true;
    }
    int port = mPlainTextBufferQ.getUnsignedShort(0);
    remoteHeader.appendShort((short)port);
    compactPlainTextBufferQ(2);
    log.info("Connecting to " + addr + ":" + port);
    connectToRemote(mConfig.server, mConfig.serverPort, remoteHeader);
    nextStage();
    return false;
}
 
Example 12
Source File: Bytes.java    From incubator-tuweni with Apache License 2.0 3 votes vote down vote up
/**
 * Append the bytes of this value to the provided Vert.x {@link Buffer}.
 *
 * <p>
 * Note that since a Vert.x {@link Buffer} will grow as necessary, this method never fails.
 *
 * @param buffer The {@link Buffer} to which to append this value.
 */
default void appendTo(Buffer buffer) {
  checkNotNull(buffer);
  for (int i = 0; i < size(); i++) {
    buffer.appendByte(get(i));
  }
}
 
Example 13
Source File: Bytes.java    From cava with Apache License 2.0 3 votes vote down vote up
/**
 * Append the bytes of this value to the provided Vert.x {@link Buffer}.
 *
 * <p>
 * Note that since a Vert.x {@link Buffer} will grow as necessary, this method never fails.
 *
 * @param buffer The {@link Buffer} to which to append this value.
 */
default void appendTo(Buffer buffer) {
  checkNotNull(buffer);
  for (int i = 0; i < size(); i++) {
    buffer.appendByte(get(i));
  }
}