Skip to content

Commit 0597246

Browse files
committed
Avoid extra byte copy inside FileSystemImpl.readFileInternal
Closes #5677 The improvement consists in creating the buffer upfront and using Netty API to read the file channel. Signed-off-by: Thomas Segismont <[email protected]>
1 parent 4eca1c7 commit 0597246

File tree

1 file changed

+16
-32
lines changed

1 file changed

+16
-32
lines changed

vertx-core/src/main/java/io/vertx/core/file/impl/FileSystemImpl.java

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,45 +14,21 @@
1414
import io.vertx.codegen.annotations.Nullable;
1515
import io.vertx.core.Future;
1616
import io.vertx.core.buffer.Buffer;
17-
import io.vertx.core.file.AsyncFile;
18-
import io.vertx.core.file.CopyOptions;
19-
import io.vertx.core.file.FileProps;
17+
import io.vertx.core.file.*;
2018
import io.vertx.core.file.FileSystem;
2119
import io.vertx.core.file.FileSystemException;
22-
import io.vertx.core.file.FileSystemProps;
23-
import io.vertx.core.file.OpenOptions;
2420
import io.vertx.core.internal.ContextInternal;
2521
import io.vertx.core.internal.VertxInternal;
22+
import io.vertx.core.internal.buffer.BufferInternal;
2623

2724
import java.io.File;
2825
import java.io.FilenameFilter;
2926
import java.io.IOException;
3027
import java.io.RandomAccessFile;
31-
import java.nio.file.CopyOption;
32-
import java.nio.file.FileAlreadyExistsException;
33-
import java.nio.file.FileStore;
34-
import java.nio.file.FileVisitOption;
35-
import java.nio.file.FileVisitResult;
36-
import java.nio.file.Files;
37-
import java.nio.file.LinkOption;
38-
import java.nio.file.Path;
39-
import java.nio.file.Paths;
40-
import java.nio.file.SimpleFileVisitor;
41-
import java.nio.file.StandardCopyOption;
42-
import java.nio.file.attribute.BasicFileAttributes;
43-
import java.nio.file.attribute.FileAttribute;
44-
import java.nio.file.attribute.GroupPrincipal;
45-
import java.nio.file.attribute.PosixFileAttributeView;
46-
import java.nio.file.attribute.PosixFilePermission;
47-
import java.nio.file.attribute.PosixFilePermissions;
48-
import java.nio.file.attribute.UserPrincipal;
49-
import java.nio.file.attribute.UserPrincipalLookupService;
50-
import java.util.ArrayList;
51-
import java.util.EnumSet;
52-
import java.util.HashSet;
53-
import java.util.List;
54-
import java.util.Objects;
55-
import java.util.Set;
28+
import java.nio.channels.FileChannel;
29+
import java.nio.file.*;
30+
import java.nio.file.attribute.*;
31+
import java.util.*;
5632
import java.util.concurrent.Callable;
5733
import java.util.regex.Pattern;
5834

@@ -875,8 +851,16 @@ private BlockingAction<Buffer> readFileInternal(String path) {
875851
public Buffer perform() {
876852
try {
877853
Path target = resolveFile(path).toPath();
878-
byte[] bytes = Files.readAllBytes(target);
879-
return Buffer.buffer(bytes);
854+
try (FileChannel fc = FileChannel.open(target, StandardOpenOption.READ)) {
855+
long size = fc.size();
856+
if (size > (long) Integer.MAX_VALUE) {
857+
throw new OutOfMemoryError("File is too big");
858+
}
859+
int len = (int) size;
860+
BufferInternal res = BufferInternal.buffer(len);
861+
res.unwrap().writeBytes(fc, 0, len);
862+
return res;
863+
}
880864
} catch (IOException e) {
881865
throw new FileSystemException(getFileAccessErrorMessage("read", path), e);
882866
}

0 commit comments

Comments
 (0)