Flaugh24 commented on code in PR #2326:
URL: https://github.com/apache/ignite-3/pull/2326#discussion_r1270626847
##########
modules/network/src/main/java/org/apache/ignite/internal/network/direct/stream/DirectByteBufferStreamImplV1.java:
##########
@@ -1662,12 +1682,167 @@ public <C extends Set<?>> C
readSet(MessageCollectionItemType itemType, MessageR
return map0;
}
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void writeFile(File file) {
+ try {
+ if (file == null) {
+ writeInt(-1);
+ return;
+ }
+
+ switch (fileSate) {
+ case 0:
+ writeLong(file.length());
+
+ if (!lastFinished) {
+ return;
+ }
+
+ fileSate++;
+
+ //noinspection fallthrough
+ case 1:
+ writeString(file.getName());
+
+ if (!lastFinished) {
+ return;
+ }
+
+ fileSate++;
+
+ //noinspection fallthrough
+ case 2:
+ lastFinished = false;
+
+ if (fileReader == null) {
+ fileReader = new BufferedInputStream(new
FileInputStream(file));
+ }
+
+ int pos = buf.position();
+ int toWrite = Math.min(buf.remaining(),
fileReader.available());
+
+ if (toWrite > 0) {
+ byte[] arr = new byte[toWrite];
+ fileReader.read(arr, 0, toWrite);
+ GridUnsafe.copyMemory(arr, BYTE_ARR_OFF, heapArr,
baseOff + pos, toWrite);
+ buf.position(pos + toWrite);
+ }
+
+ if (fileReader.available() == 0) {
+ fileReader.close();
+ fileReader = null;
+
+ lastFinished = true;
+
+ fileSate = 0;
+ }
+ break;
+
+ default:
+ throw new IllegalStateException("Invalid file state: " +
fileSate);
+ }
+ } catch (IOException e) {
+ if (fileReader != null) {
+ try {
+ fileReader.close();
+ fileReader = null;
+ } catch (IOException ex) {
+ RuntimeException exception = new RuntimeException(ex);
+ exception.addSuppressed(e);
+ throw exception;
+ }
+ }
+ throw new RuntimeException(e);
+ }
+
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public File readFile() {
+ try {
+ switch (fileSate) {
+ case 0:
+ fileSize = readLong();
+ if (!lastFinished) {
+ return null;
+ } else if (fileSize < 0) {
+ return null;
+ }
+
+ fileSate++;
+
+ //noinspection fallthrough
+ case 1:
+ String fileName = readString();
+ if (!lastFinished) {
+ return null;
+ }
+
+ filePath = Files.createTempDirectory("").resolve(fileName);
+ fileWriter = new BufferedOutputStream(new
FileOutputStream(filePath.toFile(), true));
Review Comment:
Purpose of this patch it avoids collecting the whole content of the file in
a heap (byte array). We can support attributes later if we need them. But right
now, we don't need them.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]