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:Main.java

static void memcpy(ByteBuffer dstBuffer, int dstByteOffset, ByteBuffer srcBuffer, int srcByteOffset,
        int length) {
    ByteBuffer dstDup = dstBuffer.duplicate();
    dstDup.position(dstByteOffset);/*from ww  w  . ja va  2 s  .c  o m*/
    dstDup.limit(dstByteOffset + length);
    ByteBuffer srcDup = srcBuffer.duplicate();
    srcDup.position(srcByteOffset);
    srcDup.limit(srcByteOffset + length);
    dstDup.put(srcDup);
}

From source file:com.icloud.framework.core.nio.ByteBufferUtil.java

public static ByteBuffer clone(ByteBuffer o) {
    assert o != null;

    if (o.remaining() == 0)
        return ByteBuffer.wrap(ArrayUtils.EMPTY_BYTE_ARRAY);

    ByteBuffer clone = ByteBuffer.allocate(o.remaining());

    if (o.isDirect()) {
        for (int i = o.position(); i < o.limit(); i++) {
            clone.put(o.get(i));
        }//from ww  w. ja  v a  2  s  . c om
        clone.flip();
    } else {
        System.arraycopy(o.array(), o.arrayOffset() + o.position(), clone.array(), 0, o.remaining());
    }

    return clone;
}

From source file:com.log4ic.compressor.utils.FileUtils.java

/**
 * ?/*w  w w  .  java  2s. c  o  m*/
 *
 * @param content
 * @param filePath
 * @return
 */
public static File writeFile(byte[] content, String filePath) {
    FileOutputStream out = null;
    FileChannel outChannel = null;
    File file = new File(filePath);

    if (file.exists()) {
        file.delete();
    }

    if (!file.getParentFile().exists()) {
        file.getParentFile().mkdirs();
    }

    ByteBuffer outBuffer = ByteBuffer.allocate(content.length);
    outBuffer.put(content);
    outBuffer.flip();
    try {
        out = new FileOutputStream(file);

        outChannel = out.getChannel();

        outChannel.write(outBuffer);

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (outChannel != null) {
            try {
                outChannel.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (out != null) {
            try {
                out.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    return file.exists() ? file : null;
}

From source file:org.lable.rfc3881.auditlogger.adapter.hbase.HBaseAdapter.java

static byte[] columnQualifierSuffixFor(Identifiable identifiable) {
    List<String> parts = identifiable.identifyingStack();
    // Account for the separator bytes.
    int targetLength = parts.size() - 1;
    for (String part : parts) {
        if (part != null) {
            targetLength += part.length();
        }//w ww . j a va  2s . c  om
    }

    ByteBuffer buffer = ByteBuffer.allocate(targetLength);
    boolean first = true;
    for (String part : parts) {
        if (!first) {
            buffer.put(NULL_BYTE);
        } else {
            first = false;
        }

        if (part != null) {
            buffer.put(toBytes(part));
        }
    }

    return buffer.array();
}

From source file:com.chicm.cmraft.rpc.PacketUtils.java

private static int writeRpc(AsynchronousSocketChannel channel, Message header, Message body, int totalSize)
        throws IOException, InterruptedException, ExecutionException {
    // writing total size so that server can read all request data in one read
    //LOG.debug("total size:" + totalSize);
    long t = System.currentTimeMillis();

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    writeIntToStream(totalSize, bos);/*from   w  w w .jav  a 2  s. c  om*/

    header.writeDelimitedTo(bos);
    if (body != null)
        body.writeDelimitedTo(bos);

    bos.flush();
    byte[] b = bos.toByteArray();
    ByteBuffer buf = ByteBuffer.allocateDirect(totalSize + 4);
    buf.put(b);

    buf.flip();
    channel.write(buf).get();

    if (LOG.isTraceEnabled()) {
        LOG.trace("Write Rpc message to socket, takes " + (System.currentTimeMillis() - t) + " ms, size "
                + totalSize);
        LOG.trace("message:" + body);
    }
    return totalSize;
}

From source file:Main.java

private static byte[] encodeUrl(String url, int position, ByteBuffer bb) {
    while (position < url.length()) {
        byte expansion = findLongestExpansion(url, position);
        if (expansion >= 0) {
            bb.put(expansion);
            position += URL_CODES.get(expansion).length();
        } else {/*from w w  w. ja va 2s . c  o  m*/
            bb.put((byte) url.charAt(position++));
        }
    }
    return byteBufferToArray(bb);
}

From source file:com.liveramp.commons.util.BytesUtils.java

public static ByteBuffer byteBufferDeepCopy(ByteBuffer src, ByteBuffer dst) {
    if (dst == null || dst.capacity() < src.remaining()) {
        dst = byteBufferDeepCopy(src);/*from   w ww.  j ava  2  s. c  om*/
    } else {
        dst.rewind();
        dst.limit(src.remaining());
        dst.put(src.slice());
        dst.flip();
    }
    return dst;
}

From source file:burstcoin.observer.service.ATService.java

public static String getATLong(String hex) {
    ByteBuffer bf = ByteBuffer.allocate(8);
    bf.order(ByteOrder.LITTLE_ENDIAN);
    bf.put(parseHexString(hex));
    return toUnsignedLong(bf.getLong(0));
}

From source file:org.osiam.tests.performance.tools.TestDataCreation.java

private static ByteBuffer getBigByteBuffer(Integer countCurrentUser) {
    String userId = countCurrentUser.toString();
    byte[] bytes = new byte[userId.length() + MIN_COUNT_BYTE_BUFFER];
    int actPosition;

    // first comes the id
    char[] userChars = userId.toCharArray();
    for (int count = 0; count < userChars.length; count++) {
        bytes[count] = (byte) userChars[count];
    }/*from   w  w  w.  j  av a  2s  . c  o m*/
    actPosition = userChars.length;

    // now we add random bytes
    Random random = new Random();
    String allowedChars = "0123456789abcdefghijklmnopqrstuvwxyz";
    int max = allowedChars.length();
    for (int i = 0; i < MIN_COUNT_BYTE_BUFFER; i++) {
        int value = random.nextInt(max);
        bytes[actPosition++] = (byte) allowedChars.charAt(value);
    }

    ByteBuffer ret = ByteBuffer.wrap(new byte[bytes.length]);
    ret.put(bytes);
    ret.flip();

    return ret;
}

From source file:com.oneguy.recognize.Util.java

public static ByteBuffer putData(ByteBuffer buffer, byte[] data) {
    if (buffer == null || data == null || data.length == 0) {
        return buffer;
    }//from w w  w .j  av a  2s  .c  o m
    while (buffer.capacity() < buffer.position() + data.length - 1) {
        buffer = doubleSize(buffer);
    }
    buffer.put(data);
    return buffer;
}