Example usage for java.nio ByteBuffer put

List of usage examples for java.nio ByteBuffer put

Introduction

In this page you can find the example usage for java.nio ByteBuffer put.

Prototype

public ByteBuffer put(ByteBuffer src) 

Source Link

Document

Writes all the remaining bytes of the src byte buffer to this buffer's current position, and increases both buffers' position by the number of bytes copied.

Usage

From source file:com.github.cambierr.lorawanpacket.semtech.PushData.java

@Override
public void toRaw(ByteBuffer _bb) throws MalformedPacketException {
    super.toRaw(_bb);
    _bb.put(gatewayEui);

    JSONObject json = new JSONObject();
    if (!stats.isEmpty()) {
        JSONArray stat = new JSONArray();
        for (Stat s : stats) {
            stat.put(s.toJson());//from w w  w  . ja v  a  2  s . c o m
        }
        json.put("stat", stats);
    }
    if (!rxpks.isEmpty()) {
        JSONArray rxpk = new JSONArray();
        for (Rxpk s : rxpks) {
            rxpk.put(s.toJson());
        }
        json.put("rxpk", rxpks);
    }

    _bb.put(json.toString().getBytes());
}

From source file:com.mastercard.walletservices.mdes.DeviceInfo.java

/**
 * Return ByteArray of device finger print.
 *
 * @return The Device Finger Pring as String
 *///w ww  . j  a  v a2s  . c o m
public String getDeviceFingerprint() {

    byte[] dataBytes = (this.deviceName + this.deviceType + this.imei + this.msisdn + this.nfcCapable
            + this.osName).getBytes();

    ByteBuffer byteBuffer = ByteBuffer.wrap(new byte[dataBytes.length]);
    byteBuffer.put(dataBytes);

    // Create MessageDigest using SHA-256 algorithm Added required
    MessageDigest messageDigest;

    try {
        messageDigest = MessageDigest.getInstance("SHA-256");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return null;
    }

    // Hash the result
    byte[] hash = messageDigest.digest(byteBuffer.array());

    // Return Hex
    return new String(Hex.encodeHex(hash));
}

From source file:eu.pursuit.core.ItemName.java

public byte[] toByteArray() {
    int length = 0;
    if (scopeId != null) {
        length += scopeId.getLength();/*from w  w w  . j a  v a 2s.  c o m*/
    }

    if (rendezvousId != null) {
        length += rendezvousId.getId().length;
    }

    ByteBuffer buffer = ByteBuffer.allocate(length);
    if (scopeId != null) {
        scopeId.fill(buffer);
    }

    if (rendezvousId != null) {
        buffer.put(rendezvousId.getId());
    }
    return buffer.array();
}

From source file:tachyon.master.RawTables.java

/**
 * Get the metadata of the specified raw table. It will return a duplication.
 *
 * @param tableId The id of the raw table
 * @return null if it has no metadata, or a duplication of the metadata
 *///  w  ww .j a  v a2s  .  com
public synchronized ByteBuffer getMetadata(int tableId) {
    Pair<Integer, ByteBuffer> data = mData.get(tableId);

    if (null == data) {
        return null;
    }

    ByteBuffer ret = ByteBuffer.allocate(data.getSecond().capacity());
    ret.put(data.getSecond().array());
    ret.flip();

    return ret;
}

From source file:com.unister.semweb.drums.file.SpecificEnlargmentTest.java

/** Converts the given {@link List} of {@link TestStorable} to a {@link ByteBuffer}. */
private ByteBuffer toByteBuffer(List<TestStorable> toConvert) {
    ByteBuffer buffer = ByteBuffer.allocate(toConvert.size() * globalParameters.getElementSize());
    for (TestStorable oneElement : toConvert) {
        buffer.put(oneElement.toByteBuffer());
    }// w w w . jav a  2 s .  c o  m
    buffer.flip();
    return buffer;
}

From source file:com.btoddb.trellis.common.serialization.JavaSerializer.java

@Override
public ByteBuffer serialize(final ByteBuffer bb, Serializable obj) {
    OutputStream os = new OutputStream() {
        @Override//from  w  w w.j  av  a2s  .com
        public void write(int oneByte) throws IOException {
            bb.put((byte) oneByte);
        }
    };

    serializeToOutputStream(obj, os);
    return bb;
}

From source file:com.healthmarketscience.jackcess.impl.OleUtil.java

private static byte[] writePackageHeader(OleBlob.Builder oleBuilder, long contentLen) {

    byte[] prettyNameBytes = getZeroTermStrBytes(oleBuilder.getPrettyName());
    String className = oleBuilder.getClassName();
    String typeName = oleBuilder.getTypeName();
    if (className == null) {
        className = typeName;//  w  ww .ja  v  a 2  s  .  c o  m
    } else if (typeName == null) {
        typeName = className;
    }
    byte[] classNameBytes = getZeroTermStrBytes(className);
    byte[] typeNameBytes = getZeroTermStrBytes(typeName);

    int packageHeaderLen = 20 + prettyNameBytes.length + classNameBytes.length;

    int oleHeaderLen = 24 + typeNameBytes.length;

    byte[] headerBytes = new byte[packageHeaderLen + oleHeaderLen];

    ByteBuffer bb = PageChannel.wrap(headerBytes);

    // write outer package header
    bb.putShort((short) PACKAGE_SIGNATURE);
    bb.putShort((short) packageHeaderLen);
    bb.putInt(PACKAGE_OBJECT_TYPE);
    bb.putShort((short) prettyNameBytes.length);
    bb.putShort((short) classNameBytes.length);
    int prettyNameOff = bb.position() + 8;
    bb.putShort((short) prettyNameOff);
    bb.putShort((short) (prettyNameOff + prettyNameBytes.length));
    bb.putInt(-1);
    bb.put(prettyNameBytes);
    bb.put(classNameBytes);

    // put ole header
    bb.putInt(OLE_VERSION);
    bb.putInt(OLE_FORMAT);
    bb.putInt(typeNameBytes.length);
    bb.put(typeNameBytes);
    bb.putLong(0L);
    bb.putInt((int) contentLen);

    return headerBytes;
}

From source file:org.jmangos.sniffer.handler.PKTLogHandler.java

/**
 * (non-Javadoc)//w  w  w  .j  a va  2 s. c om
 * 
 * @see org.jmangos.sniffer.handler.PacketLogHandler#onDecodePacket(org.jmangos
 *      .sniffer.network.model.NetworkChannel,
 *      org.jmangos.sniffer.enums.Direction, java.lang.Integer,
 *      java.lang.Integer, byte[], int)
 */
@Override
public void onDecodePacket(final NetworkChannel channel, final Direction direction, final Integer size,
        final Integer opcode, final byte[] data, final int frame) {
    if (!isInit()) {
        init();
    }
    try {
        final ByteBuffer buffer = ByteBuffer.allocate(4 + 4 + 4 + 4 + 4 + data.length + 4);
        buffer.order(ByteOrder.LITTLE_ENDIAN);
        buffer.put(direction.getValue());
        buffer.putInt(channel.hashCode());
        buffer.putInt(frame);
        buffer.putInt(0);
        buffer.putInt(data.length + 4);
        buffer.putInt(opcode);
        buffer.put(data);
        this.fous.write(buffer.array());
    } catch (final IOException e) {
        e.printStackTrace();
    }
}

From source file:rascal.object.AbstractObjectFactoryTest.java

private void headerReadExpectation(final String header) throws Exception {
    context.checking(new Expectations() {
        {//from   w  w  w  .j a va  2  s .c  om
            oneOf(readableByteChannelMock).read(with(new TypeSafeMatcher<ByteBuffer>() {
                public boolean matchesSafely(ByteBuffer byteBuffer) {
                    return byteBuffer.limit() == HEADER_BUFFER_LENGTH;
                }

                public void describeTo(Description description) {
                    description.appendText(
                            String.format("Buffer for header should be %d bytes length", HEADER_BUFFER_LENGTH));
                }
            }));
            will(new CustomAction("writes header to buffer") {
                public Object invoke(Invocation invocation) throws Throwable {
                    ByteBuffer buffer = (ByteBuffer) invocation.getParameter(0);
                    buffer.put(header.getBytes());
                    buffer.put((byte) 0);
                    return HEADER_BUFFER_LENGTH;
                }
            });

            oneOf(readableByteChannelMock).close();
        }
    });
}

From source file:net.jenet.Packet.java

/**
 * Copies this packet's data into the given buffer.
 * @param buffer/*ww w  .  ja v  a  2  s  .co m*/
 *              Destination buffer
 */
public void toBuffer(ByteBuffer buffer) {
    data.flip();
    for (int i = 0; i < dataLength; i++) {
        buffer.put(data.get());
    }
}