final boolean ensureCapacity(long bytes, boolean clearNewMem) { long oldCap = getCapacity(); long newBytes = bytes - oldCap; if (newBytes <= 0) return false; // avoid frequent increase of allocation area, instead increase by segment size int allSegments = (int) (bytes / segmentSizeInBytes); if (bytes % segmentSizeInBytes != 0) allSegments++; capacity = allSegments * segmentSizeInBytes; try { address = UNSAFE.reallocateMemory(address, capacity); } catch (OutOfMemoryError err) { throw new OutOfMemoryError(err.getMessage() + " - problem when allocating new memory. Old capacity: " + oldCap + ", new bytes:" + newBytes + ", segmentSizeIntsPower:" + segmentSizePower); } if (clearNewMem) UNSAFE.setMemory(address + oldCap, capacity - oldCap, (byte) 0); return true; }
@Override public void flush() { if (isClosed()) throw new IllegalStateException("already closed"); try { RandomAccessFile raFile = new RandomAccessFile(getFullName(), "rw"); try { long len = getCapacity(); writeHeader(raFile, len, segmentSizeInBytes); raFile.seek(HEADER_OFFSET); byte bytes[] = new byte[segmentSizeInBytes]; int segs = getSegments(); for (int s = 0; s < segs; s++) { getBytes(s * segmentSizeInBytes, bytes, segmentSizeInBytes); raFile.write(bytes); } } finally { raFile.close(); } } catch (Exception ex) { throw new RuntimeException("Couldn't store bytes to " + toString(), ex); } }
@Override public void flush() { if (isClosed()) throw new IllegalStateException("already closed"); try { RandomAccessFile raFile = new RandomAccessFile(getFullName(), "rw"); try { long len = getCapacity(); writeHeader(raFile, len, segmentSizeInBytes); raFile.seek(HEADER_OFFSET); byte bytes[] = new byte[segmentSizeInBytes]; int segs = getSegments(); for (int s = 0; s < segs; s++) { getBytes(s * segmentSizeInBytes, bytes, segmentSizeInBytes); raFile.write(bytes); } } finally { raFile.close(); } } catch (Exception ex) { throw new RuntimeException("Couldn't store bytes to " + toString(), ex); } }
final boolean ensureCapacity(long bytes, boolean clearNewMem) { long oldCap = getCapacity(); long newBytes = bytes - oldCap; if (newBytes <= 0) return false; // avoid frequent increase of allocation area, instead increase by segment size int allSegments = (int) (bytes / segmentSizeInBytes); if (bytes % segmentSizeInBytes != 0) allSegments++; capacity = allSegments * segmentSizeInBytes; try { address = UNSAFE.reallocateMemory(address, capacity); } catch (OutOfMemoryError err) { throw new OutOfMemoryError(err.getMessage() + " - problem when allocating new memory. Old capacity: " + oldCap + ", new bytes:" + newBytes + ", segmentSizeIntsPower:" + segmentSizePower); } if (clearNewMem) UNSAFE.setMemory(address + oldCap, capacity - oldCap, (byte) 0); return true; }
final boolean ensureCapacity(long bytes, boolean clearNewMem) { long oldCap = getCapacity(); long newBytes = bytes - oldCap; if (newBytes <= 0) return false; // avoid frequent increase of allocation area, instead increase by segment size int allSegments = (int) (bytes / segmentSizeInBytes); if (bytes % segmentSizeInBytes != 0) allSegments++; capacity = allSegments * segmentSizeInBytes; try { address = UNSAFE.reallocateMemory(address, capacity); } catch (OutOfMemoryError err) { throw new OutOfMemoryError(err.getMessage() + " - problem when allocating new memory. Old capacity: " + oldCap + ", new bytes:" + newBytes + ", segmentSizeIntsPower:" + segmentSizePower); } if (clearNewMem) UNSAFE.setMemory(address + oldCap, capacity - oldCap, (byte) 0); return true; }
@Override public void flush() { if (isClosed()) throw new IllegalStateException("already closed"); try { RandomAccessFile raFile = new RandomAccessFile(getFullName(), "rw"); try { long len = getCapacity(); writeHeader(raFile, len, segmentSizeInBytes); raFile.seek(HEADER_OFFSET); byte bytes[] = new byte[segmentSizeInBytes]; int segs = getSegments(); for (int s = 0; s < segs; s++) { getBytes(s * segmentSizeInBytes, bytes, segmentSizeInBytes); raFile.write(bytes); } } finally { raFile.close(); } } catch (Exception ex) { throw new RuntimeException("Couldn't store bytes to " + toString(), ex); } }
final boolean ensureCapacity( long bytes, boolean clearNewMem ) { long oldCap = getCapacity(); long newBytes = bytes - oldCap; if (newBytes <= 0) return false; // avoid frequent increase of allocation area, instead increase by segment size int allSegments = (int) (bytes / segmentSizeInBytes); if (bytes % segmentSizeInBytes != 0) allSegments++; capacity = allSegments * segmentSizeInBytes; try { address = UNSAFE.reallocateMemory(address, capacity); } catch (OutOfMemoryError err) { throw new OutOfMemoryError(err.getMessage() + " - problem when allocating new memory. Old capacity: " + oldCap + ", new bytes:" + newBytes + ", segmentSizeIntsPower:" + segmentSizePower); } if (clearNewMem) UNSAFE.setMemory(address + oldCap, capacity - oldCap, (byte) 0); return true; }
@Override public void flush() { if (isClosed()) throw new IllegalStateException("already closed"); try { RandomAccessFile raFile = new RandomAccessFile(getFullName(), "rw"); try { long len = getCapacity(); writeHeader(raFile, len, segmentSizeInBytes); raFile.seek(HEADER_OFFSET); byte bytes[] = new byte[segmentSizeInBytes]; int segs = getSegments(); for (int s = 0; s < segs; s++) { getBytes(s * segmentSizeInBytes, bytes, segmentSizeInBytes); raFile.write(bytes); } } finally { raFile.close(); } } catch (Exception ex) { throw new RuntimeException("Couldn't store bytes to " + toString(), ex); } }