diff options
author | DJ Houghton | 2006-08-14 15:09:34 +0000 |
---|---|---|
committer | DJ Houghton | 2006-08-14 15:09:34 +0000 |
commit | c41174de45dbe16aefc694bf720440e57a0c1d1c (patch) | |
tree | 703a6971f2996ec48d4ec31512964c7d8cd12313 | |
parent | 622ad4789ca88a64de13a27966553609a19705b0 (diff) | |
download | rt.equinox.bundles-c41174de45dbe16aefc694bf720440e57a0c1d1c.tar.gz rt.equinox.bundles-c41174de45dbe16aefc694bf720440e57a0c1d1c.tar.xz rt.equinox.bundles-c41174de45dbe16aefc694bf720440e57a0c1d1c.zip |
Bug 150852 - Improve BufferedRandomInputStream # seek() functionalityR3_2_1R32x_v20060814
Bug 150516 - BufferedRandomInputStream doesn't buffer correctly
-rw-r--r-- | bundles/org.eclipse.equinox.registry/META-INF/MANIFEST.MF | 2 | ||||
-rw-r--r-- | bundles/org.eclipse.equinox.registry/src/org/eclipse/core/internal/registry/BufferedRandomInputStream.java | 47 |
2 files changed, 37 insertions, 12 deletions
diff --git a/bundles/org.eclipse.equinox.registry/META-INF/MANIFEST.MF b/bundles/org.eclipse.equinox.registry/META-INF/MANIFEST.MF index 0b774836e..24ccadd6f 100644 --- a/bundles/org.eclipse.equinox.registry/META-INF/MANIFEST.MF +++ b/bundles/org.eclipse.equinox.registry/META-INF/MANIFEST.MF @@ -2,7 +2,7 @@ Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: %pluginName Bundle-SymbolicName: org.eclipse.equinox.registry; singleton:=true -Bundle-Version: 3.2.0.qualifier +Bundle-Version: 3.2.1.qualifier Bundle-Localization: plugin Export-Package: org.eclipse.core.internal.registry;x-friends:="org.eclipse.core.runtime", org.eclipse.core.internal.registry.osgi;x-friends:="org.eclipse.core.runtime", diff --git a/bundles/org.eclipse.equinox.registry/src/org/eclipse/core/internal/registry/BufferedRandomInputStream.java b/bundles/org.eclipse.equinox.registry/src/org/eclipse/core/internal/registry/BufferedRandomInputStream.java index a19c8619f..db0abec13 100644 --- a/bundles/org.eclipse.equinox.registry/src/org/eclipse/core/internal/registry/BufferedRandomInputStream.java +++ b/bundles/org.eclipse.equinox.registry/src/org/eclipse/core/internal/registry/BufferedRandomInputStream.java @@ -21,6 +21,16 @@ public class BufferedRandomInputStream extends InputStream { private String filePath; // Canonical path to the underlying file used for logging private int buffer_size; // Current size of the buffer private int buffer_pos; // Current read position in the buffer + /** + * The absolute position in the file where the buffered region starts. + */ + private long buffer_start = 0; + + /** + * The current value of the RAF's file pointer. + */ + private long file_pointer; + private byte buffer[]; public BufferedRandomInputStream(File file) throws IOException { @@ -31,17 +41,21 @@ public class BufferedRandomInputStream extends InputStream { filePath = file.getCanonicalPath(); inputFile = new RandomAccessFile(file, "r"); //$NON-NLS-1$ buffer = new byte[bufferSize]; + file_pointer = 0; resetBuffer(); } - private void resetBuffer() throws IOException { + private void resetBuffer() { buffer_pos = 0; buffer_size = 0; + buffer_start = 0; } private int fillBuffer() throws IOException { buffer_pos = 0; + buffer_start = file_pointer; buffer_size = inputFile.read(buffer, 0, buffer.length); + file_pointer += buffer_size; return buffer_size; } @@ -55,17 +69,20 @@ public class BufferedRandomInputStream extends InputStream { public int read(byte b[], int off, int len) throws IOException { int available = buffer_size - buffer_pos; + if (available < 0) + return -1; + //the buffer contains all the bytes we need, so copy over and return if (len <= available) { System.arraycopy(buffer, buffer_pos, b, off, len); buffer_pos += len; return len; } - - // Use portion remaining in the buffer and read the rest from file + // Use portion remaining in the buffer System.arraycopy(buffer, buffer_pos, b, off, available); - int read = inputFile.read(b, off + available, len - available); - fillBuffer(); // nothing left in the buffer, fill it with the next chunk - return available + read; + if (fillBuffer() <= 0) + return available; + //recursive call to read again until we have the bytes we need + return available + read(b, off + available, len - available); } public long skip(long n) throws IOException { @@ -76,10 +93,11 @@ public class BufferedRandomInputStream extends InputStream { if (n <= available) { buffer_pos += n; return n; - } else { - resetBuffer(); - return available + inputFile.skipBytes((int) (n - available)); } + resetBuffer(); + final int skipped = inputFile.skipBytes((int) (n - available)); + file_pointer += skipped; + return available + skipped; } public int available() throws IOException { @@ -104,8 +122,15 @@ public class BufferedRandomInputStream extends InputStream { * @throws IOException */ public void seek(long pos) throws IOException { - inputFile.seek(pos); - resetBuffer(); + if (pos >= buffer_start && pos < buffer_start + buffer_size) { + //seeking within the current buffer + buffer_pos = (int) (pos - buffer_start); + } else { + //seeking outside the buffer - just discard the buffer + inputFile.seek(pos); + file_pointer = pos; + resetBuffer(); + } } /** |