Example usage for java.nio ByteBuffer get

List of usage examples for java.nio ByteBuffer get

Introduction

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

Prototype

public abstract byte get(int index);

Source Link

Document

Returns the byte at the specified index and does not change the position.

Usage

From source file:org.brekka.phalanx.core.services.impl.AbstractCryptoService.java

private InternalPrivateKeyToken decodePrivateKey(byte[] encoded, UUID idOfData, CryptoProfile cryptoProfile) {
    ByteBuffer buffer = ByteBuffer.wrap(encoded);
    byte[] marker = new byte[PK_MAGIC_MARKER.length];
    buffer.get(marker);
    if (!Arrays.equals(PK_MAGIC_MARKER, marker)) {
        throw new PhalanxException(PhalanxErrorCode.CP208,
                "CryptoData item '%s' does not appear to contain a private key", idOfData);
    }/*ww w .j  a va 2 s .  c  om*/
    int profileId = buffer.getInt();
    CryptoProfile profile = cryptoProfileService.retrieveProfile(profileId);
    byte[] data = new byte[encoded.length - (SK_MAGIC_MARKER.length + 4)];
    buffer.get(data);

    PrivateKey privateKey = phoenixAsymmetric.toPrivateKey(data, profile);
    return new InternalPrivateKeyToken(privateKey);
}

From source file:name.martingeisse.stackd.server.section.storage.CassandraSectionStorage.java

@Override
public Map<SectionDataId, byte[]> loadSectionRelatedObjects(final Collection<? extends SectionDataId> ids) {
    if (logger.isDebugEnabled()) {
        logger.debug("loading section-related objects: " + StringUtils.join(ids, ", "));
    }/*w  ww.  j  a va  2  s .co  m*/
    try {

        // convert the IDs to an array of strings
        Object[] idTexts = new String[ids.size()];
        {
            int i = 0;
            for (SectionDataId id : ids) {
                idTexts[i] = id.getIdentifierText();
                i++;
            }
        }

        // fetch the rows
        final Map<SectionDataId, byte[]> result = new HashMap<>();
        for (final Row row : fetch(QueryBuilder.in("id", idTexts))) {
            final String id = row.getString("id");
            final ByteBuffer dataBuffer = row.getBytes("data");
            final byte[] data = new byte[dataBuffer.remaining()];
            dataBuffer.get(data);
            result.put(new SectionDataId(id), data);
        }
        return result;

    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.brekka.phalanx.core.services.impl.AbstractCryptoService.java

private InternalSecretKeyToken decodeSecretKey(byte[] encoded, UUID idOfData) {
    ByteBuffer buffer = ByteBuffer.wrap(encoded);
    byte[] marker = new byte[SK_MAGIC_MARKER.length];
    buffer.get(marker);
    if (!Arrays.equals(SK_MAGIC_MARKER, marker)) {
        throw new PhalanxException(PhalanxErrorCode.CP213,
                "CryptoData item '%s' does not appear to contain a secret key", idOfData);
    }//from www.  java  2 s . c om
    int profileId = buffer.getInt();
    byte[] keyId = new byte[16];
    buffer.get(keyId);
    UUID symCryptoDataId = toUUID(keyId);
    CryptoProfile cryptoProfile = cryptoProfileService.retrieveProfile(profileId);
    byte[] data = new byte[encoded.length - (SK_MAGIC_MARKER.length + 20)];
    buffer.get(data);

    SecretKey secretKey = phoenixSymmetric.toSecretKey(data, cryptoProfile);
    SymedCryptoData symedCryptoData = new SymedCryptoData();
    symedCryptoData.setId(symCryptoDataId);
    symedCryptoData.setProfile(profileId);
    return new InternalSecretKeyToken(secretKey, symedCryptoData);
}

From source file:com.offbynull.portmapper.pcp.MapPcpResponse.java

/**
 * Constructs a {@link MapPcpResponse} object by parsing a buffer.
 * @param buffer buffer containing PCP response data
 * @throws NullPointerException if any argument is {@code null}
 * @throws BufferUnderflowException if not enough data is available in {@code buffer}
 * @throws IllegalArgumentException if there's not enough or too much data remaining in the buffer, or if the version doesn't match the
 * expected version (must always be {@code 2}), or if the r-flag isn't set, or if there's an unsuccessful/unrecognized result code,
 * or if the op code doesn't match the MAP opcode, or if the response has a {@code 0} for its {@code internalPort} or
 * {@code assignedExternalPort} field, or if there were problems parsing options
 *///from  w w w.ja  v a 2s . co m
public MapPcpResponse(ByteBuffer buffer) {
    super(buffer);

    Validate.isTrue(super.getOp() == 1);

    mappingNonce = ByteBuffer.allocate(12);
    buffer.get(mappingNonce.array());
    mappingNonce = mappingNonce.asReadOnlyBuffer();
    this.protocol = buffer.get() & 0xFF;

    for (int i = 0; i < 3; i++) { // reserved block
        buffer.get();
    }

    this.internalPort = buffer.getShort() & 0xFFFF;
    this.assignedExternalPort = buffer.getShort() & 0xFFFF;
    byte[] addrArr = new byte[16];
    buffer.get(addrArr);
    try {
        this.assignedExternalIpAddress = InetAddress.getByAddress(addrArr); // should automatically shift down to ipv4 if ipv4-to-ipv6
                                                                            // mapped address
    } catch (UnknownHostException uhe) {
        throw new IllegalArgumentException(uhe); // should never happen, will always be 16 bytes
    }

    Validate.inclusiveBetween(0, 255, protocol); // should never happen
    Validate.inclusiveBetween(0, 65535, internalPort); // can be 0 if referencing all
    Validate.inclusiveBetween(0, 65535, assignedExternalPort); // can be 0 if removing

    parseOptions(buffer);
}

From source file:com.haulmont.yarg.formatters.impl.DocxFormatter.java

private String toString(ByteBuffer bb) throws UnsupportedEncodingException {
    byte[] bytes = new byte[bb.limit()];
    bb.get(bytes);
    return new String(bytes, "UTF-8");
}

From source file:edu.udo.scaffoldhunter.model.db.StringProperty.java

/**
 * Set the length in the BitFingerprint (first two bytes)
 * /*from w  ww  .  ja v a 2s.co m*/
 * @param length
 *            the length
 * @param bitFingerprint
 *            the fingerprint
 */
private void lenghtToBitFingerprint(short length, byte[] bitFingerprint) {
    ByteBuffer bb = ByteBuffer.allocate(2);
    bb.order(ByteOrder.LITTLE_ENDIAN);
    bb.putShort(length);

    bitFingerprint[0] = bb.get(0);
    bitFingerprint[1] = bb.get(1);
}

From source file:com.reactive.hzdfs.utils.Digestor.java

private void updateBytes(ByteBuffer buff) {
    int i = buff.position();
    buff.flip();//from   w  ww .  j a v a  2  s .  c o  m
    byte[] b = new byte[i];
    buff.get(b);
    if (byteArray == null)
        byteArray = b;
    else {
        i = byteArray.length;
        byteArray = Arrays.copyOf(byteArray, i + b.length);
        System.arraycopy(b, 0, byteArray, i, b.length);
    }

}

From source file:com.buaa.cfs.common.oncrpc.XDR.java

@VisibleForTesting
public byte[] getBytes() {
    ByteBuffer d = asReadOnlyWrap().buffer();
    byte[] b = new byte[d.remaining()];
    d.get(b);

    return b;/*from w w w .j a  v  a2  s. co m*/
}

From source file:com.bendb.thrifty.testing.ThriftTestHandler.java

@Override
public ByteBuffer testBinary(ByteBuffer thing) throws TException {
    int count = thing.remaining();
    byte[] data = new byte[count];
    thing.get(data);

    out.printf("testBinary(\"%s\")\n", Hex.encodeHexString(data));

    return ByteBuffer.wrap(data);
}

From source file:com.github.horrorho.inflatabledonkey.data.blob.BlobA6.java

public BlobA6(ByteBuffer blob) {
    super(blob);/*  w w  w .  jav  a 2  s  .  c  o  m*/

    if (type() != 0x000000A6) {
        // Presumed type 0x000000A6, consider throwing exception.
        logger.warn("** RespA6() - unexpected type: 0x{}", Integer.toHexString(type()));
    }

    x = blob.getInt();
    blob.get(tag);
    align(blob);

    list = new BlobLists(blob);
    if (list.size() < 3) {
        throw new IllegalArgumentException("too few blob fields: " + list.size());
    }
}