Example usage for io.vertx.core.buffer Buffer appendShort

List of usage examples for io.vertx.core.buffer Buffer appendShort

Introduction

In this page you can find the example usage for io.vertx.core.buffer Buffer appendShort.

Prototype

@Fluent
Buffer appendShort(short s);

Source Link

Document

Appends the specified short to the end of the Buffer.The buffer will expand as necessary to accommodate any bytes written.

Returns a reference to this so multiple operations can be appended together.

Usage

From source file:io.advantageous.qbit.vertx.BufferUtils.java

License:Apache License

public static void writeString(final Buffer buffer, final String value) {

    byte[] string = value.getBytes(StandardCharsets.UTF_8);
    buffer.appendShort((short) string.length);
    buffer.appendBytes(string);//from  w  w  w.ja v a 2  s  .c  o m

}

From source file:io.advantageous.qbit.vertx.BufferUtils.java

License:Apache License

public static void writeMap(final Buffer buffer, final MultiMap<String, String> params) {
    buffer.appendShort((short) params.size());

    final Set<String> keys = params.keySet();

    for (String key : keys) {

        writeString(buffer, key);//w ww  .  j  a  v a  2s  .c om

        final Collection<String> values = (Collection<String>) params.getAll(key);
        buffer.appendShort((short) values.size());

        for (String value : values) {
            writeString(buffer, value);
        }
    }
}

From source file:shadowsocks.vertxio.ClientHandler.java

License:Apache License

private boolean handleStageAddress() {
    int bufferLength = mBufferQueue.length();
    String addr = null;/*from  www.  j  a va2 s  .c  o  m*/
    // Construct the remote header.
    Buffer remoteHeader = Buffer.buffer();
    int addrType = mBufferQueue.getByte(0);
    if (mConfig.oneTimeAuth) {
        remoteHeader.appendByte((byte) (addrType | OTA_FLAG));
    } else {
        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(mBufferQueue.getBytes(1, 5)).toString();
        } catch (UnknownHostException e) {
            log.error("UnknownHostException.", e);
            return true;
        }
        remoteHeader.appendBytes(mBufferQueue.getBytes(1, 5));
        compactBuffer(5);
    } else if (addrType == ADDR_TYPE_HOST) {
        short hostLength = mBufferQueue.getUnsignedByte(1);
        // addr type(1) + len(1) + host + port(2)
        if (bufferLength < hostLength + 4)
            return false;
        addr = mBufferQueue.getString(2, hostLength + 2);
        remoteHeader.appendByte((byte) hostLength).appendString(addr);
        compactBuffer(hostLength + 2);
    } else {
        log.warn("Unsupport addr type " + addrType);
        return true;
    }
    int port = mBufferQueue.getUnsignedShort(0);
    remoteHeader.appendShort((short) port);
    compactBuffer(2);
    log.info("Connecting to " + addr + ":" + port);
    connectToRemote(mConfig.server, mConfig.serverPort, remoteHeader);
    nextStage();
    return false;
}

From source file:shadowsocks.vertxio.ClientHandler.java

License:Apache License

private void sendToRemote(Buffer buffer) {

    Buffer chunkBuffer = Buffer.buffer();
    try {//from   ww w .j  a  va  2s.  c o m
        if (mConfig.oneTimeAuth) {
            //chunk length 2 bytes
            chunkBuffer.appendShort((short) buffer.length());
            //auth result 10 bytes
            byte[] authKey = SSAuth.prepareKey(mCrypto.getIV(true), mChunkCount++);
            byte[] authData = buffer.getBytes();
            byte[] authResult = mAuthor.doAuth(authKey, authData);
            chunkBuffer.appendBytes(authResult);
        }
        chunkBuffer.appendBuffer(buffer);
        byte[] data = chunkBuffer.getBytes();
        byte[] encryptData = mCrypto.encrypt(data, data.length);
        if (mRemoteSocket.writeQueueFull()) {
            log.warn("-->remote write queue full");
        }
        mRemoteSocket.write(Buffer.buffer(encryptData));
    } catch (CryptoException | AuthException e) {
        log.error("Catch exception", e);
        destory();
    }
}