From 0c503e9476c15f0e4be108d5711e2ac0d9fdb286 Mon Sep 17 00:00:00 2001 From: Dmitry Avdeev Date: Wed, 16 Sep 2015 12:21:24 +0300 Subject: [PATCH] IDEA-143773 Problems with opening workspace/project/module files from an encrypted directory --- .../vfs/impl/local/LocalFileSystemBase.java | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/platform/platform-impl/src/com/intellij/openapi/vfs/impl/local/LocalFileSystemBase.java b/platform/platform-impl/src/com/intellij/openapi/vfs/impl/local/LocalFileSystemBase.java index a4136a37a384..962c15f317eb 100644 --- a/platform/platform-impl/src/com/intellij/openapi/vfs/impl/local/LocalFileSystemBase.java +++ b/platform/platform-impl/src/com/intellij/openapi/vfs/impl/local/LocalFileSystemBase.java @@ -41,6 +41,7 @@ import org.jetbrains.annotations.Nullable; import java.io.*; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.Locale; @@ -465,13 +466,29 @@ public abstract class LocalFileSystemBase extends LocalFileSystem { if (length < 0) throw new IOException("Invalid file length: " + length + ", " + file); // io_util.c#readBytes allocates custom native stack buffer for io operation with malloc if io request > 8K // so let's do buffered requests with buffer size 8192 that will use stack allocated buffer - return FileUtil.loadBytes(length <= 8192 ? stream : new BufferedInputStream(stream), length); + return loadBytes(length <= 8192 ? stream : new BufferedInputStream(stream), length); } finally { stream.close(); } } + @NotNull + private static byte[] loadBytes(@NotNull InputStream stream, int length) throws IOException { + byte[] bytes = new byte[length]; + int count = 0; + while (count < length) { + int n = stream.read(bytes, count, length - count); + if (n <= 0) break; + count += n; + } + if (count < length) { + // this may happen with encrypted files, see IDEA-143773 + return Arrays.copyOf(bytes, count); + } + return bytes; + } + @Override @NotNull public OutputStream getOutputStream(@NotNull VirtualFile file, Object requestor, long modStamp, final long timeStamp) throws IOException {