From e0cffd562df5165d5cc14b52b8d22e74076bdee7 Mon Sep 17 00:00:00 2001 From: "Maxim.Mossienko" Date: Fri, 4 May 2012 14:37:02 +0400 Subject: [PATCH] do not create exact byte[] for content if we really need stream --- .../util/io/UnsyncByteArrayInputStream.java | 8 ++++++-- .../util/io/storage/RefCountingStorage.java | 16 ++++++++++++++-- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/platform/util/src/com/intellij/util/io/UnsyncByteArrayInputStream.java b/platform/util/src/com/intellij/util/io/UnsyncByteArrayInputStream.java index aa0f1b845642..41af61ef256e 100644 --- a/platform/util/src/com/intellij/util/io/UnsyncByteArrayInputStream.java +++ b/platform/util/src/com/intellij/util/io/UnsyncByteArrayInputStream.java @@ -24,9 +24,13 @@ public class UnsyncByteArrayInputStream extends InputStream { private int myMarkedPosition; public UnsyncByteArrayInputStream(byte buf[]) { + this(buf, 0, buf.length); + } + + public UnsyncByteArrayInputStream(byte buf[], int offset, int length) { this.myBuffer = buf; - this.myPosition = 0; - this.myCount = buf.length; + this.myPosition = offset; + this.myCount = length; } public int read() { diff --git a/platform/util/src/com/intellij/util/io/storage/RefCountingStorage.java b/platform/util/src/com/intellij/util/io/storage/RefCountingStorage.java index a1e015453c9b..c03823a4d96a 100644 --- a/platform/util/src/com/intellij/util/io/storage/RefCountingStorage.java +++ b/platform/util/src/com/intellij/util/io/storage/RefCountingStorage.java @@ -27,6 +27,7 @@ import com.intellij.util.containers.ConcurrentHashMap; import com.intellij.util.io.PagePool; import com.intellij.util.io.UnsyncByteArrayInputStream; +import java.io.DataInputStream; import java.io.File; import java.io.IOException; import java.util.Map; @@ -51,10 +52,19 @@ public class RefCountingStorage extends AbstractStorage { super(path); } + public DataInputStream readStream(int record) throws IOException { + if (myDoNotZipCaches) return super.readStream(record); + BufferExposingByteArrayOutputStream stream = internalReadStream(record); + return new DataInputStream(new UnsyncByteArrayInputStream(stream.getInternalBuffer(), 0, stream.size())); + } + @Override protected byte[] readBytes(int record) throws IOException { - if (myDoNotZipCaches) return super.readBytes(record); + return internalReadStream(record).toByteArray(); + } + + private BufferExposingByteArrayOutputStream internalReadStream(int record) throws IOException { waitForPendingWriteForRecord(record); synchronized (myLock) { @@ -62,7 +72,9 @@ public class RefCountingStorage extends AbstractStorage { byte[] result = super.readBytes(record); InflaterInputStream in = new InflaterInputStream(new UnsyncByteArrayInputStream(result)); try { - return StreamUtil.loadFromStream(in); + final BufferExposingByteArrayOutputStream outputStream = new BufferExposingByteArrayOutputStream(); + StreamUtil.copyStreamContent(in, outputStream); + return outputStream; } finally { in.close();