/** * @param ptr Pointer. * @param keyLen Key length. * @param valBytes Value bytes. * @param mem Memory. */ static void writeValueBytes(long ptr, int keyLen, byte[] valBytes, GridUnsafeMemory mem) { mem.writeBytes(ptr + HEADER_SIZE + keyLen, valBytes); }
/** * @param ptr Pointer. * @param keyBytes Key bytes. * @param mem Memory. */ static void writeKeyBytes(long ptr, byte[] keyBytes, GridUnsafeMemory mem) { mem.writeBytes(ptr + HEADER_SIZE, keyBytes); }
/** {@inheritDoc} */ @Override public void writeBytes(byte[] b, int offset, int len) throws IOException { assert b != null; crc.update(b, offset, len); while (len > 0) { if (bufPosition == bufLength) { currBufIdx++; switchCurrentBuffer(); } int remainInBuf = BUFFER_SIZE - bufPosition; int bytesToCp = len < remainInBuf ? len : remainInBuf; mem.writeBytes(currBuf + bufPosition, b, offset, bytesToCp); offset += bytesToCp; len -= bytesToCp; bufPosition += bytesToCp; } }
/** {@inheritDoc} */ @Override public void copyBytes(DataInput input, long numBytes) throws IOException { assert numBytes >= 0 : "numBytes=" + numBytes; GridLuceneInputStream gridInput = input instanceof GridLuceneInputStream ? (GridLuceneInputStream)input : null; while (numBytes > 0) { if (bufPosition == bufLength) { currBufIdx++; switchCurrentBuffer(); } int toCp = BUFFER_SIZE - bufPosition; if (numBytes < toCp) toCp = (int)numBytes; if (gridInput != null) gridInput.readBytes(currBuf + bufPosition, toCp); else { byte[] buff = new byte[toCp]; input.readBytes(buff, 0, toCp, false); mem.writeBytes(currBuf + bufPosition, buff); } numBytes -= toCp; bufPosition += toCp; } }
/** * Stores value to the specified memory location. If specified pointer is {@code 0}, then will * allocate required space. If size of allocated space is not enough to hold given values, will * reallocate memory. * * @param ptr Optional pointer to allocated memory. First 4 bytes in allocated region must contain * size of allocated chunk. * @param val Value to store. * @param type Value type. * @return Pointer. */ public long putOffHeap(long ptr, byte[] val, byte type) { int size = val.length; assert size != 0; int allocated = ptr == 0 ? 0 : readInt(ptr); if (allocated != size) { if (ptr != 0) release(ptr, allocated + 5); ptr = allocate(size + 5); writeInt(ptr, size); } writeByte(ptr + 4, type); writeBytes(ptr + 5, val); return ptr; }
/** * @throws Exception If failed. */ @Test public void testBytes() throws Exception { GridUnsafeMemory mem = new GridUnsafeMemory(64); String s = "123"; byte[] bytes = s.getBytes(); int size = bytes.length * 2; long addr = mem.allocate(size); try { mem.writeBytes(addr, bytes); byte[] read = mem.readBytes(addr, bytes.length); assert Arrays.equals(bytes, read); } finally { mem.release(addr, size); } }
/** * @param s String. * @throws Exception If failed. */ public void checkCompare(String s) throws Exception { byte[] bytes = s.getBytes(); int size = bytes.length + 8; GridUnsafeMemory mem = new GridUnsafeMemory(size); for (int i = 0; i < 8; i++) { long addr = mem.allocate(size); long ptr = addr + i; try { mem.writeBytes(ptr, bytes); assert mem.compare(ptr, bytes); byte[] read = mem.readBytes(ptr, bytes.length); assert Arrays.equals(bytes, read); } finally { mem.release(addr, size); } } }
/** * @param ptr Pointer. * @param keyBytes Key bytes. * @param mem Memory. */ static void writeKeyBytes(long ptr, byte[] keyBytes, GridUnsafeMemory mem) { mem.writeBytes(ptr + HEADER_SIZE, keyBytes); }
/** * @param ptr Pointer. * @param keyLen Key length. * @param valBytes Value bytes. * @param mem Memory. */ static void writeValueBytes(long ptr, int keyLen, byte[] valBytes, GridUnsafeMemory mem) { mem.writeBytes(ptr + HEADER_SIZE + keyLen, valBytes); }
/** {@inheritDoc} */ @Override public void writeBytes(byte[] b, int offset, int len) throws IOException { assert b != null; crc.update(b, offset, len); while (len > 0) { if (bufPosition == bufLength) { currBufIdx++; switchCurrentBuffer(); } int remainInBuf = BUFFER_SIZE - bufPosition; int bytesToCp = len < remainInBuf ? len : remainInBuf; mem.writeBytes(currBuf + bufPosition, b, offset, bytesToCp); offset += bytesToCp; len -= bytesToCp; bufPosition += bytesToCp; } }
/** {@inheritDoc} */ @Override public void copyBytes(DataInput input, long numBytes) throws IOException { assert numBytes >= 0 : "numBytes=" + numBytes; GridLuceneInputStream gridInput = input instanceof GridLuceneInputStream ? (GridLuceneInputStream)input : null; while (numBytes > 0) { if (bufPosition == bufLength) { currBufIdx++; switchCurrentBuffer(); } int toCp = BUFFER_SIZE - bufPosition; if (numBytes < toCp) toCp = (int)numBytes; if (gridInput != null) gridInput.readBytes(currBuf + bufPosition, toCp); else { byte[] buff = new byte[toCp]; input.readBytes(buff, 0, toCp, false); mem.writeBytes(currBuf + bufPosition, buff); } numBytes -= toCp; bufPosition += toCp; } }
/** * Stores value to the specified memory location. If specified pointer is {@code 0}, then will * allocate required space. If size of allocated space is not enough to hold given values, will * reallocate memory. * * @param ptr Optional pointer to allocated memory. First 4 bytes in allocated region must contain * size of allocated chunk. * @param val Value to store. * @param type Value type. * @return Pointer. */ public long putOffHeap(long ptr, byte[] val, byte type) { int size = val.length; assert size != 0; int allocated = ptr == 0 ? 0 : readInt(ptr); if (allocated != size) { if (ptr != 0) release(ptr, allocated + 5); ptr = allocate(size + 5); writeInt(ptr, size); } writeByte(ptr + 4, type); writeBytes(ptr + 5, val); return ptr; }