@Override public void trimTo(long capacity) { if (capacity > getCapacity()) { throw new IllegalStateException("Cannot increase capacity (" + getCapacity() + ") to " + capacity + " via trimTo. Use ensureCapacity instead. "); } if (capacity < segmentSizeInBytes) capacity = segmentSizeInBytes; int remainingSegments = (int) (capacity / segmentSizeInBytes); if (capacity % segmentSizeInBytes != 0) { remainingSegments++; } segments = Arrays.copyOf(segments, remainingSegments); }
@Override public boolean ensureCapacity(long bytes) { if (bytes < 0) throw new IllegalArgumentException("new capacity has to be strictly positive"); long cap = getCapacity(); long newBytes = bytes - cap; if (newBytes <= 0) return false; int segmentsToCreate = (int) (newBytes / segmentSizeInBytes); if (newBytes % segmentSizeInBytes != 0) segmentsToCreate++; try { byte[][] newSegs = Arrays.copyOf(segments, segments.length + segmentsToCreate); for (int i = segments.length; i < newSegs.length; i++) { newSegs[i] = new byte[1 << segmentSizePower]; } segments = newSegs; } catch (OutOfMemoryError err) { throw new OutOfMemoryError(err.getMessage() + " - problem when allocating new memory. Old capacity: " + cap + ", new bytes:" + newBytes + ", segmentSizeIntsPower:" + segmentSizePower + ", new segments:" + segmentsToCreate + ", existing:" + segments.length); } return true; }
@Override public void flush() { if (closed) throw new IllegalStateException("already closed"); if (!store) return; try { RandomAccessFile raFile = new RandomAccessFile(getFullName(), "rw"); try { long len = getCapacity(); writeHeader(raFile, len, segmentSizeInBytes); raFile.seek(HEADER_OFFSET); // raFile.writeInt() <- too slow, so copy into byte array for (int s = 0; s < segments.length; s++) { byte area[] = segments[s]; raFile.write(area); } } finally { raFile.close(); } } catch (Exception ex) { throw new RuntimeException("Couldn't store bytes to " + toString(), ex); } }
@Override public void trimTo(long capacity) { if (capacity > getCapacity()) { throw new IllegalStateException("Cannot increase capacity (" + getCapacity() + ") to " + capacity + " via trimTo. Use ensureCapacity instead. "); } if (capacity < segmentSizeInBytes) capacity = segmentSizeInBytes; int remainingSegments = (int) (capacity / segmentSizeInBytes); if (capacity % segmentSizeInBytes != 0) { remainingSegments++; } segments = Arrays.copyOf(segments, remainingSegments); }
@Override public void trimTo(long capacity) { if (capacity > getCapacity()) { throw new IllegalStateException("Cannot increase capacity (" + getCapacity() + ") to " + capacity + " via trimTo. Use ensureCapacity instead. "); } if (capacity < segmentSizeInBytes) capacity = segmentSizeInBytes; int remainingSegments = (int) (capacity / segmentSizeInBytes); if (capacity % segmentSizeInBytes != 0) { remainingSegments++; } segments = Arrays.copyOf(segments, remainingSegments); }
@Override public void trimTo( long capacity ) { if (capacity > getCapacity()) { throw new IllegalStateException("Cannot increase capacity (" + getCapacity() + ") to " + capacity + " via trimTo. Use ensureCapacity instead. "); } if (capacity < segmentSizeInBytes) capacity = segmentSizeInBytes; int remainingSegments = (int) (capacity / segmentSizeInBytes); if (capacity % segmentSizeInBytes != 0) { remainingSegments++; } segments = Arrays.copyOf(segments, remainingSegments); }
@Override public boolean ensureCapacity(long bytes) { if (bytes < 0) throw new IllegalArgumentException("new capacity has to be strictly positive"); long cap = getCapacity(); long newBytes = bytes - cap; if (newBytes <= 0) return false; int segmentsToCreate = (int) (newBytes / segmentSizeInBytes); if (newBytes % segmentSizeInBytes != 0) segmentsToCreate++; try { byte[][] newSegs = Arrays.copyOf(segments, segments.length + segmentsToCreate); for (int i = segments.length; i < newSegs.length; i++) { newSegs[i] = new byte[1 << segmentSizePower]; } segments = newSegs; } catch (OutOfMemoryError err) { throw new OutOfMemoryError(err.getMessage() + " - problem when allocating new memory. Old capacity: " + cap + ", new bytes:" + newBytes + ", segmentSizeIntsPower:" + segmentSizePower + ", new segments:" + segmentsToCreate + ", existing:" + segments.length); } return true; }
@Override public boolean ensureCapacity( long bytes ) { if (bytes < 0) throw new IllegalArgumentException("new capacity has to be strictly positive"); long cap = getCapacity(); long newBytes = bytes - cap; if (newBytes <= 0) return false; int segmentsToCreate = (int) (newBytes / segmentSizeInBytes); if (newBytes % segmentSizeInBytes != 0) segmentsToCreate++; try { byte[][] newSegs = Arrays.copyOf(segments, segments.length + segmentsToCreate); for (int i = segments.length; i < newSegs.length; i++) { newSegs[i] = new byte[1 << segmentSizePower]; } segments = newSegs; } catch (OutOfMemoryError err) { throw new OutOfMemoryError(err.getMessage() + " - problem when allocating new memory. Old capacity: " + cap + ", new bytes:" + newBytes + ", segmentSizeIntsPower:" + segmentSizePower + ", new segments:" + segmentsToCreate + ", existing:" + segments.length); } return true; }
@Override public boolean ensureCapacity(long bytes) { if (bytes < 0) throw new IllegalArgumentException("new capacity has to be strictly positive"); long cap = getCapacity(); long newBytes = bytes - cap; if (newBytes <= 0) return false; int segmentsToCreate = (int) (newBytes / segmentSizeInBytes); if (newBytes % segmentSizeInBytes != 0) segmentsToCreate++; try { byte[][] newSegs = Arrays.copyOf(segments, segments.length + segmentsToCreate); for (int i = segments.length; i < newSegs.length; i++) { newSegs[i] = new byte[1 << segmentSizePower]; } segments = newSegs; } catch (OutOfMemoryError err) { throw new OutOfMemoryError(err.getMessage() + " - problem when allocating new memory. Old capacity: " + cap + ", new bytes:" + newBytes + ", segmentSizeIntsPower:" + segmentSizePower + ", new segments:" + segmentsToCreate + ", existing:" + segments.length); } return true; }
@Override public void flush() { if (closed) throw new IllegalStateException("already closed"); if (!store) return; try { RandomAccessFile raFile = new RandomAccessFile(getFullName(), "rw"); try { long len = getCapacity(); writeHeader(raFile, len, segmentSizeInBytes); raFile.seek(HEADER_OFFSET); // raFile.writeInt() <- too slow, so copy into byte array for (int s = 0; s < segments.length; s++) { byte area[] = segments[s]; raFile.write(area); } } finally { raFile.close(); } } catch (Exception ex) { throw new RuntimeException("Couldn't store bytes to " + toString(), ex); } }
@Override public void flush() { if (closed) throw new IllegalStateException("already closed"); if (!store) return; try { RandomAccessFile raFile = new RandomAccessFile(getFullName(), "rw"); try { long len = getCapacity(); writeHeader(raFile, len, segmentSizeInBytes); raFile.seek(HEADER_OFFSET); // raFile.writeInt() <- too slow, so copy into byte array for (int s = 0; s < segments.length; s++) { byte area[] = segments[s]; raFile.write(area); } } finally { raFile.close(); } } catch (Exception ex) { throw new RuntimeException("Couldn't store bytes to " + toString(), ex); } }
@Override public void flush() { if (closed) throw new IllegalStateException("already closed"); if (!store) return; try { RandomAccessFile raFile = new RandomAccessFile(getFullName(), "rw"); try { long len = getCapacity(); writeHeader(raFile, len, segmentSizeInBytes); raFile.seek(HEADER_OFFSET); // raFile.writeInt() <- too slow, so copy into byte array for (int s = 0; s < segments.length; s++) { byte area[] = segments[s]; raFile.write(area); } } finally { raFile.close(); } } catch (Exception ex) { throw new RuntimeException("Couldn't store bytes to " + toString(), ex); } }