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 abstract ByteBuffer put(int index, byte b);

Source Link

Document

Write a byte to the specified index of this buffer without changing the position.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    ByteBuffer bbuf = ByteBuffer.allocate(10);
    int capacity = bbuf.capacity(); // 10
    System.out.println(capacity);
    bbuf.put(0, (byte) 2);

    System.out.println(Arrays.toString(bbuf.array()));
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    ByteBuffer byteBuffer = ByteBuffer.allocate(7).order(ByteOrder.BIG_ENDIAN);
    CharBuffer charBuffer = byteBuffer.asCharBuffer();

    byteBuffer.put(0, (byte) 0);
    byteBuffer.put(1, (byte) 'H');
    byteBuffer.put(2, (byte) 0);
    byteBuffer.put(3, (byte) 'i');
    byteBuffer.put(4, (byte) 0);
    byteBuffer.put(5, (byte) '!');
    byteBuffer.put(6, (byte) 0);

    println(byteBuffer);//from www.  ja va  2s.c  om
    println(charBuffer);

    // now slice it differently
    byteBuffer.position(4);
    charBuffer = byteBuffer.asCharBuffer();

    println(byteBuffer);
    println(charBuffer);
}

From source file:Main.java

public static void putUnsignedByte(ByteBuffer bb, int position, int value) {
    bb.put(position, (byte) (value & 0xff));
}

From source file:Main.java

static void memset(ByteBuffer dstBuffer, int dstByteOffset, byte value, int length) {
    // TODO we can probably do this faster
    for (int ii = dstByteOffset; ii < dstByteOffset + length; ++ii) {
        dstBuffer.put(ii, value);
    }/*w ww  . ja v a2s  .com*/
}

From source file:com.linkedin.databus.core.DbusEventPart.java

/**
 * Replace the schema-digest in a serialized DbusEventPart.
 *
 * @param buf The buffer that contains the serialized DbusEventPart.
 * @param position the position in the buffer where the DbusEventPart starts.
 * @param schemaDigest The digest value to substitute. The value must match in length to the existing value.
 *///from   w w  w . j  a  v a  2 s  .c om
public static void replaceSchemaDigest(ByteBuffer buf, int position, byte[] schemaDigest) {
    DbusEvent.SchemaDigestType digestType = digestType(buf.getShort(position + AttributesOffset));
    int digestLen = digestLen(digestType);
    if (schemaDigest.length != digestLen) {
        throw new RuntimeException(
                "Expecting length " + digestLen + " for type " + digestType + ", found " + schemaDigest.length);
    }
    for (int i = 0; i < digestLen; i++) {
        buf.put(position + AttributesOffset + AttributesLen + i, schemaDigest[i]);
    }
}

From source file:com.glaf.core.util.ByteBufferUtils.java

/**
 * Transfer bytes from one ByteBuffer to another. This function acts as
 * System.arrayCopy() but for ByteBuffers.
 * //  w  w w. ja  v a 2 s.c  o m
 * @param src
 *            the source ByteBuffer
 * @param srcPos
 *            starting position in the source ByteBuffer
 * @param dst
 *            the destination ByteBuffer
 * @param dstPos
 *            starting position in the destination ByteBuffer
 * @param length
 *            the number of bytes to copy
 */
public static void arrayCopy(ByteBuffer src, int srcPos, ByteBuffer dst, int dstPos, int length) {
    if (src.hasArray() && dst.hasArray()) {
        System.arraycopy(src.array(), src.arrayOffset() + srcPos, dst.array(), dst.arrayOffset() + dstPos,
                length);
    } else {
        if (src.limit() - srcPos < length || dst.limit() - dstPos < length)
            throw new IndexOutOfBoundsException();

        for (int i = 0; i < length; i++)

            dst.put(dstPos++, src.get(srcPos++));
    }
}

From source file:com.linkedin.pinot.core.indexsegment.utils.MmapMemoryManagerTest.java

@Test
public void testCornerConditions() throws Exception {
    final String segmentName = "someSegment";
    PinotDataBufferMemoryManager memoryManager = new MmapMemoryManager(_tmpDir, segmentName);
    final long s1 = MmapMemoryManager.getDefaultFileLength() - 1;
    final long s2 = 1;
    final long s3 = 100 * 1024 * 1024;
    final String colName = "col";
    final byte v1 = 56;
    final byte v2 = 11;
    final byte v3 = 32;

    // Allocate a buffer 1 less than the default file length, and write the last byte of the buffer.
    PinotDataBuffer b1 = memoryManager.allocate(s1, colName);
    ByteBuffer bb1 = b1.toDirectByteBuffer(0, (int) s1);
    bb1.put((int) s1 - 1, v1);

    // Allocate another buffer that is 1 byte in size, should be in the same file.
    // Write a value in the byte.
    PinotDataBuffer b2 = memoryManager.allocate(s2, colName);
    ByteBuffer bb2 = b2.toDirectByteBuffer(0, (int) s2);
    bb2.put((int) s2 - 1, v2);

    // Verify that there is only one file.
    File dir = new File(_tmpDir);
    Assert.assertEquals(dir.listFiles().length, 1);

    // Now allocate another buffer that will open a second file, write a value in the first byte of the buffer.
    PinotDataBuffer b3 = memoryManager.allocate(s3, colName);
    ByteBuffer bb3 = b3.toDirectByteBuffer(0, (int) s3);
    bb3.put(0, v3);//from ww w.  j  a  va2s. com

    // Ensure that there are 2 files.
    Assert.assertEquals(dir.listFiles().length, 2);

    // Make sure that the values written are preserved.
    Assert.assertEquals(bb1.get((int) s1 - 1), v1);
    Assert.assertEquals(bb2.get((int) s2 - 1), v2);
    Assert.assertEquals(bb3.get(0), v3);

    memoryManager.close();
    List<Pair<MmapUtils.AllocationContext, Integer>> allocationContexts = MmapUtils.getAllocationsAndSizes();
    Assert.assertEquals(allocationContexts.size(), 0);
}

From source file:com.linkedin.pinot.core.indexsegment.utils.MmapMemoryManagerTest.java

@Test
public void testLargeBlocks() throws Exception {
    final String segmentName = "someSegment";
    PinotDataBufferMemoryManager memoryManager = new MmapMemoryManager(_tmpDir, segmentName);
    final long s1 = 2 * MmapMemoryManager.getDefaultFileLength();
    final long s2 = 1000;
    final String col1 = "col1";
    final String col2 = "col2";
    final byte value = 34;
    PinotDataBuffer buf1 = memoryManager.allocate(s1, col1);

    // Verify that we can write to and read from the buffer
    ByteBuffer b1 = buf1.toDirectByteBuffer(0, (int) s1);
    b1.putLong(0, s1);// w  w  w  .j  ava 2  s . co m
    Assert.assertEquals(b1.getLong(0), s1);
    b1.put((int) s1 - 1, value);
    Assert.assertEquals(b1.get((int) s1 - 1), value);

    PinotDataBuffer buf2 = memoryManager.allocate(s2, col2);
    ByteBuffer b2 = buf2.toDirectByteBuffer(0, (int) s2);
    b2.putLong(0, s2);
    Assert.assertEquals(b2.getLong(0), s2);

    File dir = new File(_tmpDir);
    File[] files = dir.listFiles();
    Assert.assertEquals(files.length, 2);

    Arrays.sort(files, new Comparator<File>() {
        @Override
        public int compare(File o1, File o2) {
            return o1.getName().compareTo(o2.getName());
        }
    });

    String fileName = files[0].getName();
    Assert.assertTrue(fileName.contains(segmentName));

    fileName = files[1].getName();
    Assert.assertTrue(fileName.contains(segmentName));

    buf1.close();
    buf2.close();

    memoryManager.close();

    List<Pair<MmapUtils.AllocationContext, Integer>> allocationContexts = MmapUtils.getAllocationsAndSizes();
    Assert.assertEquals(allocationContexts.size(), 0);
    Assert.assertEquals(new File(_tmpDir).listFiles().length, 0);
}

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

/**
 * Applies the XOR mask to the database header in the given buffer.
 *//*  w w  w  . j a v  a  2 s  .c om*/
private void applyHeaderMask(ByteBuffer buffer) {
    // de/re-obfuscate the header
    byte[] headerMask = _format.HEADER_MASK;
    for (int idx = 0; idx < headerMask.length; ++idx) {
        int pos = idx + _format.OFFSET_MASKED_HEADER;
        byte b = (byte) (buffer.get(pos) ^ headerMask[idx]);
        buffer.put(pos, b);
    }
}

From source file:com.longle1.facedetection.MainActivity.java

protected void processImage(byte[] data, int width, int height) {
    // First, downsample our image and convert it into a grayscale IplImage
    int f = SUBSAMPLING_FACTOR;
    if (grayImage == null || grayImage.width() != width / f || grayImage.height() != height / f) {
        grayImage = IplImage.create(width / f, height / f, IPL_DEPTH_8U, 1);
    }//from   w w w .  j av a  2 s .c o  m
    int imageWidth = grayImage.width();
    int imageHeight = grayImage.height();
    int dataStride = f * width;
    int imageStride = grayImage.widthStep();
    ByteBuffer imageBuffer = grayImage.getByteBuffer();
    for (int y = 0; y < imageHeight; y++) {
        int dataLine = y * dataStride;
        int imageLine = y * imageStride;
        for (int x = 0; x < imageWidth; x++) {
            imageBuffer.put(imageLine + x, data[dataLine + f * x]);
        }
    }

    cvClearMemStorage(storage);
    faces = cvHaarDetectObjects(grayImage, classifier, storage, 1.1, 3, CV_HAAR_DO_CANNY_PRUNING);
    postInvalidate();

    postProcessImage(data, width, height);
}