Skip to content
This repository has been archived by the owner on Mar 3, 2024. It is now read-only.

Commit

Permalink
Do not give array away
Browse files Browse the repository at this point in the history
  • Loading branch information
incubos committed Dec 6, 2023
1 parent 3be98c5 commit 6051c0c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
10 changes: 8 additions & 2 deletions src/main/java/ru/vk/itmo/reference/ByteArraySegment.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package ru.vk.itmo.reference;

import java.io.IOException;
import java.lang.foreign.MemorySegment;
import java.nio.ByteBuffer;
import java.util.function.Consumer;

/**
* Growable buffer with {@link ByteBuffer} and {@link MemorySegment} interface.
Expand All @@ -17,8 +19,8 @@ final class ByteArraySegment {
this.segment = MemorySegment.ofArray(array);
}

byte[] array() {
return array;
void withArray(final ArrayConsumer consumer) throws IOException {
consumer.process(array);
}

MemorySegment segment() {
Expand All @@ -40,4 +42,8 @@ void ensureCapacity(final long size) {
array = new byte[newSize];
segment = MemorySegment.ofArray(array);
}

interface ArrayConsumer {
void process(byte[] array) throws IOException;
}
}
11 changes: 6 additions & 5 deletions src/main/java/ru/vk/itmo/reference/SSTableWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ private void writeLong(
ValueLayout.OfLong.JAVA_LONG_UNALIGNED,
0,
value);
os.write(longBuffer.array());
longBuffer.withArray(os::write);
}

private void writeSegment(
Expand All @@ -119,10 +119,11 @@ private void writeSegment(
blobBuffer.segment(),
0L,
size);
os.write(
blobBuffer.array(),
0,
(int) size);
blobBuffer.withArray(array ->
os.write(
array,
0,
(int) size));
}

/**
Expand Down

0 comments on commit 6051c0c

Please sign in to comment.