/** * Tries to write into a {@link ByteBuffer} instance which is read-only. This should fail with a * {@link ReadOnlyBufferException}. * * @param directBuffer * whether the {@link ByteBuffer} instance should be a direct byte buffer or not * * @throws ReadOnlyBufferException * expected exception due to writing to a read-only buffer */ private void testByteBufferGetReadOnly(boolean directBuffer) throws ReadOnlyBufferException { MemorySegment seg = createSegment(pageSize); ByteBuffer target = (directBuffer ? ByteBuffer.allocateDirect(pageSize) : ByteBuffer.allocate(pageSize)).asReadOnlyBuffer(); seg.get(0, target, pageSize); }
@Test public void testByteBufferGet() { testByteBufferGet(false); testByteBufferGet(true); }
@Test(expected = ReadOnlyBufferException.class) public void testOffHeapByteBufferGetReadOnly() { testByteBufferGetReadOnly(true); }
@Test public void testByteBufferPut() { testByteBufferPut(false); testByteBufferPut(true); }
@Test public void testSlicedByteBufferGet() { testSlicedByteBufferGet(false); testSlicedByteBufferGet(true); }
@Test public void testSlicedByteBufferPut() { testSlicedByteBufferPut(false); testSlicedByteBufferPut(true); }
private void testByteBufferPut(boolean directBuffer) { byte[] bytes = new byte[pageSize]; random.nextBytes(bytes); ByteBuffer source = directBuffer ? ByteBuffer.allocateDirect(pageSize) : ByteBuffer.allocate(pageSize); source.put(bytes); source.clear(); MemorySegment seg = createSegment(3 * pageSize); int offset = 2 * pageSize; // transfer the segment in chunks into the byte buffer int pos = 0; while (pos < pageSize) { int len = random.nextInt(pageSize / 10); len = Math.min(len, pageSize - pos); seg.put(offset + pos, source, len); pos += len; } // verify that we read the same bytes byte[] result = new byte[pageSize]; seg.get(offset, result); assertArrayEquals(bytes, result); }
@Test(expected = ReadOnlyBufferException.class) public void testHeapByteBufferGetReadOnly() { testByteBufferGetReadOnly(false); }
private void testByteBufferGet(boolean directBuffer) { MemorySegment seg = createSegment(pageSize); byte[] bytes = new byte[pageSize]; random.nextBytes(bytes); seg.put(0, bytes); ByteBuffer target = directBuffer ? ByteBuffer.allocateDirect(3 * pageSize) : ByteBuffer.allocate(3 * pageSize); target.position(2 * pageSize); // transfer the segment in chunks into the byte buffer int pos = 0; while (pos < pageSize) { int len = random.nextInt(pageSize / 10); len = Math.min(len, pageSize - pos); seg.get(pos, target, len); pos += len; } // verify that we wrote the same bytes byte[] result = new byte[pageSize]; target.position(2 * pageSize); target.get(result); assertArrayEquals(bytes, result); }
private void testSlicedByteBufferGet(boolean directBuffer) { MemorySegment seg = createSegment(pageSize); byte[] bytes = new byte[pageSize]; random.nextBytes(bytes); seg.put(0, bytes); ByteBuffer target = directBuffer ? ByteBuffer.allocateDirect(pageSize + 49) : ByteBuffer.allocate(pageSize + 49); target.position(19).limit(19 + pageSize); ByteBuffer slicedTarget = target.slice(); // transfer the segment in chunks into the byte buffer int pos = 0; while (pos < pageSize) { int len = random.nextInt(pageSize / 10); len = Math.min(len, pageSize - pos); seg.get(pos, slicedTarget, len); pos += len; } // verify that we wrote the same bytes byte[] result = new byte[pageSize]; target.position(19); target.get(result); assertArrayEquals(bytes, result); }
private void testSlicedByteBufferPut(boolean directBuffer) { byte[] bytes = new byte[pageSize + 49]; random.nextBytes(bytes); ByteBuffer source = directBuffer ? ByteBuffer.allocateDirect(pageSize + 49) : ByteBuffer.allocate(pageSize + 49); source.put(bytes); source.position(19).limit(19 + pageSize); ByteBuffer slicedSource = source.slice(); MemorySegment seg = createSegment(3 * pageSize); final int offset = 2 * pageSize; // transfer the segment in chunks into the byte buffer int pos = 0; while (pos < pageSize) { int len = random.nextInt(pageSize / 10); len = Math.min(len, pageSize - pos); seg.put(offset + pos, slicedSource, len); pos += len; } // verify that we read the same bytes byte[] result = new byte[pageSize]; seg.get(offset, result); byte[] expected = Arrays.copyOfRange(bytes, 19, 19 + pageSize); assertArrayEquals(expected, result); }
@Test public void testDataInputOutput() throws IOException { MemorySegment seg = createSegment(pageSize); byte[] contents = new byte[pageSize]; random.nextBytes(contents); MemorySegment reader = createSegment(pageSize); DataInputStream in = new DataInputStream(new ByteArrayInputStream(result));
MemorySegment seg1 = createSegment(pageSize); MemorySegment seg2 = createSegment(pageSize); seg1.put(0, bytes1); seg2.put(0, bytes2);
@Test public void testOwner() { // a segment without an owner has a null owner assertNull(createSegment(64).getOwner()); Object theOwner = new Object(); MemorySegment seg = createSegment(64, theOwner); assertEquals(theOwner, seg.getOwner()); // freeing must release the owner, to prevent leaks that prevent class unloading! seg.free(); assertNotNull(seg.getOwner()); }
final MemorySegment segment = createSegment(pageSize); long seed = random.nextLong(); final MemorySegment segment = createSegment(pageSize); byte[] expected = new byte[pageSize]; random.nextBytes(expected); final MemorySegment segment = createSegment(pageSize); byte[] expected = new byte[pageSize]; final MemorySegment segment = createSegment(pageSize); byte[] contents = new byte[pageSize]; random.nextBytes(contents);
MemorySegment seg = createSegment(segmentSize); byte[] bytes = new byte[segmentSize]; random.nextBytes(bytes);
ByteBuffer bb = ByteBuffer.allocate(bbCapacity); MemorySegment seg = createSegment(pageSize);
@Test public void testSwapBytes() { final int halfPageSize = pageSize / 2; final byte[] bytes1 = new byte[pageSize]; final byte[] bytes2 = new byte[halfPageSize]; Arrays.fill(bytes2, (byte) 1); MemorySegment seg1 = createSegment(pageSize); MemorySegment seg2 = createSegment(halfPageSize); seg1.put(0, bytes1); seg2.put(0, bytes2); // wap the second half of the first segment with the second segment int pos = 0; while (pos < halfPageSize) { int len = random.nextInt(pageSize / 40); len = Math.min(len, halfPageSize - pos); seg1.swapBytes(new byte[len], seg2, pos + halfPageSize, pos, len); pos += len; } // the second segment should now be all zeros, the first segment should have one in its second half for (int i = 0; i < halfPageSize; i++) { assertEquals((byte) 0, seg1.get(i)); assertEquals((byte) 0, seg2.get(i)); assertEquals((byte) 1, seg1.get(i + halfPageSize)); } }
@Test public void testByteBufferWrapping() { MemorySegment seg = createSegment(1024);
final int[] invalidLengths = { -1, -pageSize, Integer.MAX_VALUE, Integer.MIN_VALUE }; final MemorySegment seg = createSegment(pageSize);