Example usage for java.nio ByteBuffer array

List of usage examples for java.nio ByteBuffer array

Introduction

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

Prototype

public final byte[] array() 

Source Link

Document

Returns the byte array which this buffer is based on, if there is one.

Usage

From source file:edu.harvard.iq.dvn.unf.Base64Encoding.java

/**
 *
 * @param digest byte array for encoding in base 64,
 * @param  chngByteOrd boolean indicating if to change byte order
 * @return String the encoded base64 of digest
 *///w  ww. j  a  v a 2 s.  c o m
public static String tobase64(byte[] digest, boolean chngByteOrd) {

    byte[] tobase64 = null;
    ByteOrder local = ByteOrder.nativeOrder();
    String ordbyte = local.toString();
    mLog.finer("Native byte order is: " + ordbyte);
    ByteBuffer btstream = ByteBuffer.wrap(digest);
    btstream.order(ByteOrder.BIG_ENDIAN);
    byte[] revdigest = null;
    if (chngByteOrd) {
        revdigest = changeByteOrder(digest, local);
    }
    if (revdigest != null) {
        btstream.put(revdigest);
    } else {
        btstream.put(digest);
    }

    tobase64 = Base64.encodeBase64(btstream.array());

    return new String(tobase64);

}

From source file:io.mycat.util.ByteBufferUtil.java

/**
 * You should almost never use this.  Instead, use the write* methods to avoid copies.
 *//*from  w ww  .java  2s.  c  o  m*/
public static byte[] getArray(ByteBuffer buffer) {
    int length = buffer.remaining();

    if (buffer.hasArray()) {
        int boff = buffer.arrayOffset() + buffer.position();
        return Arrays.copyOfRange(buffer.array(), boff, boff + length);
    }
    // else, DirectByteBuffer.get() is the fastest route
    byte[] bytes = new byte[length];
    buffer.duplicate().get(bytes);

    return bytes;
}

From source file:com.icloud.framework.core.util.FBUtilities.java

public static int byteBufferToInt(ByteBuffer bytes) {
    if (bytes.remaining() < 4) {
        throw new IllegalArgumentException("An integer must be 4 bytes in size.");
    }/*from w  w w .  ja  v a 2 s  .  c o  m*/
    int n = 0;
    for (int i = 0; i < 4; ++i) {
        n <<= 8;
        n |= bytes.array()[bytes.position() + bytes.arrayOffset() + i] & 0xFF;
    }
    return n;
}

From source file:com.easemob.dataexport.utils.JsonUtils.java

public static Object fromByteBuffer(ByteBuffer byteBuffer, Class<?> clazz) {
    if ((byteBuffer == null) || !byteBuffer.hasRemaining()) {
        return null;
    }//from  w  w w.  java  2 s  .  c  om
    if (clazz == null) {
        clazz = Object.class;
    }

    Object obj = null;
    try {
        obj = smileMapper.readValue(byteBuffer.array(), byteBuffer.arrayOffset() + byteBuffer.position(),
                byteBuffer.remaining(), clazz);
    } catch (Exception e) {
        LOG.error("Error parsing SMILE bytes", e);
    }
    return obj;
}

From source file:com.icloud.framework.core.util.FBUtilities.java

public static void writeShortByteArray(ByteBuffer name, DataOutput out) {
    int length = name.remaining();
    assert 0 <= length && length <= MAX_UNSIGNED_SHORT;
    try {//from  w  ww  .  ja  v a2  s .c  o  m
        out.writeByte((length >> 8) & 0xFF);
        out.writeByte(length & 0xFF);
        out.write(name.array(), name.position() + name.arrayOffset(), name.remaining());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.arm.connector.bridge.core.Utils.java

public static String bytesToHexString(ByteBuffer bytes) {
    return Utils.bytesToHexString(bytes.array());
}

From source file:burstcoin.jminer.core.round.Round.java

private static int calcScoopNumber(long blockNumber, byte[] generationSignature) {
    if (blockNumber > 0 && generationSignature != null) {
        ByteBuffer buf = ByteBuffer.allocate(32 + 8);
        buf.put(generationSignature);//from w ww.  ja  va  2 s .  c  o m
        buf.putLong(blockNumber);

        // generate new scoop number
        Shabal256 md = new Shabal256();
        md.update(buf.array());

        BigInteger hashnum = new BigInteger(1, md.digest());
        return hashnum.mod(BigInteger.valueOf(MiningPlot.SCOOPS_PER_PLOT)).intValue();
    }
    return 0;
}

From source file:com.openteach.diamond.network.waverider.network.Packet.java

public static void dump(ByteBuffer buffer) {
    logger.info(//from w  ww  .ja v  a 2s  . c om
            "==========================================================================dump data==========================================================================");
    byte[] bytes = buffer.array();
    StringBuilder sb = new StringBuilder("");
    for (int i = 0; i < bytes.length; i++) {
        sb.append(bytes[i]);
        if (i % 80 == 0 && i > 0) {
            logger.info(sb);
            sb.delete(0, sb.length());
        }
    }
    logger.info(
            "==========================================================================dump data end======================================================================");
}

From source file:de.nx42.maps4cim.map.texture.osm.OsmHash.java

protected static String getQueryHashLocation(Area bounds) throws IOException {

    /*/*from   w  ww  .  ja v a  2  s .c o m*/
     * - base64 encode, every char holds 6 bits (2^6 = 64)
     * - 3 chars per float = 18bit precision = enough for 3 sigificant digits
     *   for numbers <256 (which is the case in WGS84)
     * - 4 values -> 12 chars. 72bit or 8 byte of information
     * - use URL safe encoding, or file name failures are expected!
     */

    // calculate size:  4 values, 8 bits per byte
    int bufSize = (int) Math.ceil(4 * locationPrecision / 8.0);

    ByteBuffer byteBuf = ByteBuffer.allocate(bufSize);
    BitOutput bitOut = BitOutput.newInstance(byteBuf); // direct

    storeCoordinate(bounds.getMinLat(), bitOut);
    storeCoordinate(bounds.getMaxLat(), bitOut);
    storeCoordinate(bounds.getMinLon(), bitOut);
    storeCoordinate(bounds.getMaxLon(), bitOut);

    // get array, return as Base64 (URL safe)
    byte[] ar = byteBuf.array();
    return Base64.encodeBase64URLSafeString(ar);

}

From source file:com.amazonaws.services.dynamodbv2.transactions.Request.java

protected static Request deserialize(String txId, ByteBuffer rawRequest) {
    byte[] requestBytes = rawRequest.array();
    try {/*from w  w w  .  java 2s . c  o  m*/
        return MAPPER.readValue(requestBytes, 0, requestBytes.length, Request.class);
    } catch (JsonParseException e) {
        throw new TransactionAssertionException(txId, "Failed to deserialize request " + rawRequest + " " + e);
    } catch (JsonMappingException e) {
        throw new TransactionAssertionException(txId, "Failed to deserialize request " + rawRequest + " " + e);
    } catch (IOException e) {
        throw new TransactionAssertionException(txId, "Failed to deserialize request " + rawRequest + " " + e);
    }
}