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.mozilla.bagheera.util.IdUtil.java

/**
 * Takes a given id and prefixes it with a byte character and the date in a non-random fashion
 * This method expects id to be something like a UUID consisting only of hex characters.  If id 
 * is something else this will produce unpredictable results.
 * @param id//from   ww  w.  j a v a  2s  . c om
 * @param d
 * @return
 * @throws IOException
 */
public static byte[] nonRandByteBucketizeId(String id, Date d) throws IOException {
    if (StringUtils.isBlank(id)) {
        throw new IllegalArgumentException("id cannot be null or empty");
    }
    if (d == null) {
        throw new IllegalArgumentException("date cannot be null");
    }

    // bucket byte + SDF bytes + id bytes
    ByteBuffer buf = ByteBuffer.allocate(9 + id.length());
    int bucket = 0;
    if (id.length() >= 2) {
        // Munge two hex characters into the range of a single byte
        bucket = Integer.parseInt(id.substring(0, 2), 16) - 128;
    } else {
        bucket = Integer.parseInt(id, 16) - 128;
    }
    buf.put((byte) bucket);
    buf.put(SDF.format(d).getBytes());
    buf.put(id.getBytes());

    return buf.array();
}

From source file:Main.java

public static ReadableByteChannel asReadableByteChannel(final ByteBuffer buffer) {

    return new ReadableByteChannel() {

        private boolean open = true;

        public int read(ByteBuffer dst) throws IOException {
            if (open == false) {
                throw new ClosedChannelException();
            }//from   w  w w .j a v a2s.  c  om

            final int p = buffer.position();
            final int l = buffer.limit();
            final int r = dst.remaining();

            buffer.limit(p + r);

            dst.put(buffer);

            buffer.limit(l);

            return r;
        }

        public void close() throws IOException {
            open = false;
        }

        public boolean isOpen() {
            return open;
        }

    };
}

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

public static void writeShortLength(ByteBuffer bb, int length) {
    bb.put((byte) ((length >> 8) & 0xFF));
    bb.put((byte) (length & 0xFF));
}

From source file:Main.java

public static ByteBuffer cloneByteBuffer(final ByteBuffer original) {
    // Create clone with same capacity as original.
    final ByteBuffer clone = (original.isDirect()) ? ByteBuffer.allocateDirect(original.capacity())
            : ByteBuffer.allocate(original.capacity());

    // Create a read-only copy of the original.
    // This allows reading from the original without modifying it.
    final ByteBuffer readOnlyCopy = original.asReadOnlyBuffer();

    // Flip and read from the original.
    readOnlyCopy.flip();//from  w  w w  .  j a  va 2 s.co  m
    clone.put(readOnlyCopy);
    clone.position(original.position());
    clone.limit(original.limit());
    clone.order(original.order());
    return clone;
}

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
 *//*from  ww w .  j  av a  2  s  . co 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:com.github.woz_dialog.ros_woz_dialog_project.TTSHTTPClient.java

private static byte[] addWavHeader(byte[] bytes) throws IOException {

    ByteBuffer bufferWithHeader = ByteBuffer.allocate(bytes.length + 44);
    bufferWithHeader.order(ByteOrder.LITTLE_ENDIAN);
    bufferWithHeader.put("RIFF".getBytes());
    bufferWithHeader.putInt(bytes.length + 36);
    bufferWithHeader.put("WAVE".getBytes());
    bufferWithHeader.put("fmt ".getBytes());
    bufferWithHeader.putInt(16);/*from   w w  w  .  j a  v  a 2 s .  c o m*/
    bufferWithHeader.putShort((short) 1);
    bufferWithHeader.putShort((short) 1);
    bufferWithHeader.putInt(16000);
    bufferWithHeader.putInt(32000);
    bufferWithHeader.putShort((short) 2);
    bufferWithHeader.putShort((short) 16);
    bufferWithHeader.put("data".getBytes());
    bufferWithHeader.putInt(bytes.length);
    bufferWithHeader.put(bytes);
    return bufferWithHeader.array();
}

From source file:io.github.dsheirer.record.wave.WaveWriter.java

/**
 * Creates a data chunk header with the chunk size initialized to zero
 *///  ww w.j a  v a 2  s . c o m
public static ByteBuffer getDataHeader() {
    ByteBuffer header = ByteBuffer.allocate(8);
    header.put(DATA_CHUNK_ID.getBytes());
    header.position(0);

    return header;
}

From source file:com.palantir.atlasdb.keyvalue.cassandra.CassandraKeyValueServices.java

static ByteBuffer makeCompositeBuffer(byte[] colName, long ts) {
    assert colName.length <= 1 << 16 : "Cannot use column names larger than 64KiB, was " + colName.length;

    ByteBuffer buffer = ByteBuffer.allocate(6 /* misc */ + 8 /* timestamp */ + colName.length)
            .order(ByteOrder.BIG_ENDIAN);

    buffer.put((byte) ((colName.length >> 8) & 0xFF));
    buffer.put((byte) (colName.length & 0xFF));
    buffer.put(colName);//from w  ww .j  a  v a 2  s .c  om
    buffer.put((byte) 0);

    buffer.put((byte) 0);
    buffer.put((byte) (8 & 0xFF));
    buffer.putLong(~ts);
    buffer.put((byte) 0);

    buffer.flip();

    return buffer;
}

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);
        buf.putLong(blockNumber);/*from w  ww  .j a  v  a  2s . c  o  m*/

        // 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:io.github.dsheirer.record.wave.WaveWriter.java

/**
 * Creates a little-endian 4-byte buffer containing an unsigned 32-bit
 * integer value derived from the 4 least significant bytes of the argument.
 *
 * The buffer's position is set to 0 to prepare it for writing to a channel.
 *//*from w ww .ja v  a2s .  c  o  m*/
protected static ByteBuffer getUnsignedIntegerBuffer(long size) {
    ByteBuffer buffer = ByteBuffer.allocate(4);

    buffer.put((byte) (size & 0xFFl));
    buffer.put((byte) (Long.rotateRight(size & 0xFF00l, 8)));
    buffer.put((byte) (Long.rotateRight(size & 0xFF0000l, 16)));

    /* This side-steps an issue with right shifting a signed long by 32
     * where it produces an error value.  Instead, we right shift in two steps. */
    buffer.put((byte) Long.rotateRight(Long.rotateRight(size & 0xFF000000l, 16), 8));

    buffer.position(0);

    return buffer;
}