Example usage for io.netty.buffer ByteBuf array

List of usage examples for io.netty.buffer ByteBuf array

Introduction

In this page you can find the example usage for io.netty.buffer ByteBuf array.

Prototype

public abstract byte[] array();

Source Link

Document

Returns the backing byte array of this buffer.

Usage

From source file:impl.underdark.transport.bluetooth.discovery.ble.transport.peripheral.BlePeripheral.java

License:Open Source License

void onCharacteristicReadRequest(final BluetoothDevice device, final int requestId, final int offset,
        final BluetoothGattCharacteristic characteristic) {
    //Logger.debug("ble peripheral onCharacteristicReadRequest");

    if (BleConfig.charactNodeIdUuid.equals(characteristic.getUuid())) {
        ByteBuf buffer = Unpooled.buffer();
        buffer.order(ByteOrder.BIG_ENDIAN);
        buffer.writeLong(this.nodeId);

        if (offset >= buffer.readableBytes()) {
            Logger.warn("ble peripheral read nodeId failed - invalid offset {}", offset);
            gattServer.sendResponse(device, requestId, BluetoothGatt.GATT_INVALID_OFFSET, offset, null);
            return;
        }//from  w ww. j a  v  a2s.c  o m

        byte[] value = Arrays.copyOfRange(buffer.array(), offset,
                Math.min(buffer.readableBytes() - offset, BleConfig.charactValueSizeMax));
        gattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset, value);
        return;
    } // nodeId

    if (BleConfig.charactAddressUuid.equals(characteristic.getUuid())) {
        byte[] address = BtUtils.getBytesFromAddress(this.adapter.getAddress());
        if (address == null) {
            gattServer.sendResponse(device, requestId, BluetoothGatt.GATT_FAILURE, offset, null);
            return;
        }

        onAddressReadRequest(address, device, requestId, offset);

        return;
    } // address

    gattServer.sendResponse(device, requestId, BluetoothGatt.GATT_REQUEST_NOT_SUPPORTED, offset, null);
}

From source file:impl.underdark.transport.bluetooth.discovery.ble.transport.peripheral.BlePeripheral.java

License:Open Source License

private void onAddressReadRequest(byte[] address, BluetoothDevice device, int requestId, int offset) {
    if (address == null) {
        gattServer.sendResponse(device, requestId, BluetoothGatt.GATT_FAILURE, offset, null);
        return;//from  w w  w  .ja  v a2 s .  c o  m
    }

    ByteBuf buffer = Unpooled.buffer();
    buffer.order(ByteOrder.LITTLE_ENDIAN);
    buffer.writeBytes(address);

    if (offset >= buffer.readableBytes()) {
        Logger.warn("ble peripheral read address failed - invalid offset {}", offset);
        gattServer.sendResponse(device, requestId, BluetoothGatt.GATT_INVALID_OFFSET, offset, null);
        return;
    }

    byte[] value = Arrays.copyOfRange(buffer.array(), offset,
            Math.min(buffer.readableBytes() - offset, BleConfig.charactValueSizeMax));
    gattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset, value);
}

From source file:impl.underdark.transport.bluetooth.discovery.ble.transport.peripheral.BlePeripheral.java

License:Open Source License

private void describeCharacteristic(BluetoothDevice device, int requestId, int offset,
        BluetoothGattDescriptor descriptor) {
    String description = "";

    if (BleConfig.charactAddressUuid.equals(descriptor.getCharacteristic().getUuid()))
        description = "address";
    if (BleConfig.charactNodeIdUuid.equals(descriptor.getCharacteristic().getUuid()))
        description = "nodeId";
    if (BleConfig.charactJackUuid.equals(descriptor.getCharacteristic().getUuid()))
        description = "jack";
    if (BleConfig.charactStreamUuid.equals(descriptor.getCharacteristic().getUuid()))
        description = "stream";

    ByteBuf buffer = Unpooled.buffer();
    buffer.order(ByteOrder.LITTLE_ENDIAN);
    buffer.writeBytes(description.getBytes(Charset.forName("utf-8")));
    if (offset >= buffer.readableBytes()) {
        Logger.warn("ble peripheral describe charact '{}' failed - invalid offset {}", description, offset);
        gattServer.sendResponse(device, requestId, BluetoothGatt.GATT_INVALID_OFFSET, offset, null);
        return;//  w ww  .  j  av  a2  s  .  c  om
    }

    byte[] value = Arrays.copyOfRange(buffer.array(), offset,
            Math.min(buffer.readableBytes() - offset, BleConfig.charactValueSizeMax));
    gattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset, value);
}

From source file:impl.underdark.transport.bluetooth.discovery.ble.transport.peripheral.BlePeripheral.java

License:Open Source License

private void describeCharacteristicFormat(BluetoothDevice device, int requestId, int offset,
        BluetoothGattDescriptor descriptor) {
    // https://developer.bluetooth.org/gatt/descriptors/Pages/DescriptorViewer.aspx?u=org.bluetooth.descriptor.gatt.characteristic_presentation_format.xml
    // https://developer.mbed.org/teams/Bluetooth-Low-Energy/code/BLE_API/file/ecbc3405c66e/public/GattCharacteristic.h
    byte format = 10;
    if (BleConfig.charactAddressUuid.equals(descriptor.getCharacteristic().getUuid()))
        format = 25;/*from   w  w w .  j  av  a 2  s.  c o m*/
    if (BleConfig.charactNodeIdUuid.equals(descriptor.getCharacteristic().getUuid()))
        format = 18;

    ByteBuf buffer = Unpooled.buffer();
    buffer.order(ByteOrder.LITTLE_ENDIAN);
    buffer.writeByte(format); // format
    buffer.writeByte(0); // exponent
    buffer.writeShort(0x2700); // unit
    buffer.writeByte(1); // namespace
    buffer.writeShort(0); // description

    if (offset >= buffer.readableBytes()) {
        Logger.warn("ble peripheral format charact failed - invalid offset {}", offset);
        gattServer.sendResponse(device, requestId, BluetoothGatt.GATT_INVALID_OFFSET, offset, null);
        return;
    }

    byte[] value = Arrays.copyOfRange(buffer.array(), offset,
            Math.min(buffer.readableBytes() - offset, BleConfig.charactValueSizeMax));
    gattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset, value);
}

From source file:impl.underdark.transport.nsd.NsdLink.java

License:Open Source License

private void inputLoop() {
    // Input thread.
    final int bufferSize = 4096;
    ByteBuf inputData = Unpooled.buffer(bufferSize);
    inputData.order(ByteOrder.BIG_ENDIAN);

    try {//from  w  ww .j a  va  2  s  .  c o  m
        int len;
        while (true) {
            inputData.ensureWritable(bufferSize, true);
            len = inputStream.read(inputData.array(), inputData.writerIndex(), bufferSize);
            if (len <= 0)
                break;

            inputData.writerIndex(inputData.writerIndex() + len);

            if (!formFrames(inputData))
                break;

            inputData.discardReadBytes();
            inputData.capacity(inputData.writerIndex() + bufferSize);
        } // while
    } catch (InterruptedIOException ex) {
        Logger.warn("nsd input timeout: {}", ex);
        try {
            inputStream.close();
        } catch (IOException ioex) {
        }

        notifyDisconnect();
        return;
    } catch (Exception ex) {
        Logger.warn("nsd input read failed: {}", ex);
        try {
            inputStream.close();
        } catch (IOException ioex) {
        }

        notifyDisconnect();
        return;
    }

    Logger.debug("nsd input read end");
    notifyDisconnect();
}

From source file:io.atomix.cluster.messaging.impl.MessageDecoder.java

License:Apache License

static String readString(ByteBuf buffer, int length, Charset charset) {
    if (buffer.isDirect()) {
        final String result = buffer.toString(buffer.readerIndex(), length, charset);
        buffer.skipBytes(length);/*from   ww w .  ja  v  a  2 s  . co  m*/
        return result;
    } else if (buffer.hasArray()) {
        final String result = new String(buffer.array(), buffer.arrayOffset() + buffer.readerIndex(), length,
                charset);
        buffer.skipBytes(length);
        return result;
    } else {
        final byte[] array = new byte[length];
        buffer.readBytes(array);
        return new String(array, charset);
    }
}

From source file:io.datty.aerospike.support.AerospikeValueUtil.java

License:Apache License

public static Value toValue(ByteBuf bufferOrNull) {

    if (bufferOrNull == null) {
        return new NullValue();
    } else if (bufferOrNull.hasArray()) {
        int start = bufferOrNull.readerIndex();
        int length = bufferOrNull.readableBytes();
        if (start != 0 || length != bufferOrNull.capacity()) {
            int baseOffset = bufferOrNull.arrayOffset() + start;
            return new ByteSegmentValue(bufferOrNull.array(), baseOffset, baseOffset + length);
        } else {//from w  w  w  . j a v  a 2 s  .c o  m
            return new BytesValue(bufferOrNull.array());
        }
    } else {
        byte[] bytes = new byte[bufferOrNull.readableBytes()];
        bufferOrNull.getBytes(bufferOrNull.readerIndex(), bytes);
        return new BytesValue(bytes);
    }

}

From source file:io.datty.msgpack.core.AbstractMessageReader.java

License:Apache License

public String readString(ByteBuf buffer, int length, Charset charset) {
    if (length > buffer.readableBytes()) {
        throw new MessageParseException(
                "insufficient buffer length: " + buffer.readableBytes() + ", required length: " + length);
    }//w  w w .  ja  va2  s.c  o m
    if (buffer.hasArray()) {
        int start = buffer.readerIndex();
        int baseOffset = buffer.arrayOffset() + start;
        String str = new String(buffer.array(), baseOffset, length, charset);
        buffer.readerIndex(start + length);
        return str;
    } else {
        byte[] bytes = new byte[length];
        buffer.readBytes(bytes);
        return new String(bytes, charset);
    }
}

From source file:io.gatling.netty.util.ahc.ByteBufUtils.java

License:Apache License

public static byte[] byteBuf2Bytes(ByteBuf buf) {
    int readable = buf.readableBytes();
    int readerIndex = buf.readerIndex();
    if (buf.hasArray()) {
        byte[] array = buf.array();
        if (buf.arrayOffset() == 0 && readerIndex == 0 && array.length == readable) {
            return array;
        }/*from   w  ww .ja v  a  2 s.c  o m*/
    }
    byte[] array = new byte[readable];
    buf.getBytes(readerIndex, array);
    return array;
}

From source file:io.jsql.mysql.mysql.PreparedOkPacket.java

License:Open Source License

@Override
public void write(Channel channel) {
    int size = calcPacketSize();
    ByteBuf buffer = Unpooled.buffer(size);
    //        buffer = c.checkWriteBuffer(buffer, c.getPacketHeaderSize() + size,writeSocketIfFull);
    MBufferUtil.writeUB3(buffer, size);//from ww  w.  j a va  2s.  com
    buffer.writeByte(packetId);
    buffer.writeByte(flag);
    MBufferUtil.writeUB4(buffer, statementId);
    MBufferUtil.writeUB2(buffer, columnsNumber);
    MBufferUtil.writeUB2(buffer, parametersNumber);
    buffer.writeByte(filler);
    MBufferUtil.writeUB2(buffer, warningCount);
    channel.writeAndFlush(buffer.array());
}