diff --git a/src/main/java/org/scijava/io/handle/DefaultBufferedStreamHandle.java b/src/main/java/org/scijava/io/handle/DefaultBufferedStreamHandle.java index 727e8e765..89080ac7f 100644 --- a/src/main/java/org/scijava/io/handle/DefaultBufferedStreamHandle.java +++ b/src/main/java/org/scijava/io/handle/DefaultBufferedStreamHandle.java @@ -105,7 +105,7 @@ public void seek(final long pos) throws IOException { } private long maxBuf() throws IOException { - return getBufferIfOpen().getMaxPos(); + return getBufferIfOpen().size(); } @Override @@ -231,4 +231,9 @@ private ByteBank getBufferIfOpen() throws IOException { } return buffer; } + + @Override + public boolean exists() throws IOException { + return false; + } } diff --git a/src/main/java/org/scijava/io/location/AbstractCompressedHandle.java b/src/main/java/org/scijava/io/location/AbstractCompressedHandle.java index 94ae6c345..d7bc194d7 100644 --- a/src/main/java/org/scijava/io/location/AbstractCompressedHandle.java +++ b/src/main/java/org/scijava/io/location/AbstractCompressedHandle.java @@ -35,9 +35,10 @@ import java.io.InputStream; import java.io.OutputStream; -import org.scijava.io.DataHandle; -import org.scijava.io.DataHandleService; -import org.scijava.io.Location; +import org.scijava.io.handle.AbstractStreamHandle; +import org.scijava.io.handle.DataHandle; +import org.scijava.io.handle.DataHandleService; +import org.scijava.io.handle.ResettableStreamHandle; import org.scijava.plugin.Parameter; /** @@ -79,6 +80,13 @@ public InputStream in() throws IOException { return inputStream; } + @Override + public long skip(long n) throws IOException { + long skipped = in().skip(n); + setOffset(offset() + skipped); + return skipped; + } + protected abstract void initInputStream() throws IOException; @Override @@ -101,6 +109,11 @@ public long length() throws IOException { return raw().length(); } + @Override + public boolean exists() throws IOException { + return raw().exists(); + } + @Override public void setLength(long length) throws IOException { throw new IOException("This handle " + this.getClass().getSimpleName() + diff --git a/src/main/java/org/scijava/io/location/AbstractHigherOrderLocation.java b/src/main/java/org/scijava/io/location/AbstractHigherOrderLocation.java new file mode 100644 index 000000000..d89a22c26 --- /dev/null +++ b/src/main/java/org/scijava/io/location/AbstractHigherOrderLocation.java @@ -0,0 +1,15 @@ + +package org.scijava.io.location; + +public abstract class AbstractHigherOrderLocation implements Location { + + private final Location baseLocation; + + public AbstractHigherOrderLocation(final Location location) { + this.baseLocation = location; + } + + public Location getBaseLocation() { + return baseLocation; + } +} diff --git a/src/main/java/org/scijava/io/location/bzip2/BZip2Handle.java b/src/main/java/org/scijava/io/location/bzip2/BZip2Handle.java index 3575fc46e..205dd108a 100644 --- a/src/main/java/org/scijava/io/location/bzip2/BZip2Handle.java +++ b/src/main/java/org/scijava/io/location/bzip2/BZip2Handle.java @@ -33,7 +33,7 @@ import java.io.IOException; -import org.scijava.io.DataHandle; +import org.scijava.io.handle.DataHandle; import org.scijava.io.location.AbstractCompressedHandle; import org.scijava.plugin.Plugin; @@ -60,5 +60,4 @@ protected void initInputStream() throws IOException { public Class getType() { return BZip2Location.class; } - } diff --git a/src/main/java/org/scijava/io/location/bzip2/BZip2Location.java b/src/main/java/org/scijava/io/location/bzip2/BZip2Location.java index 4de9c768a..1e691ebba 100644 --- a/src/main/java/org/scijava/io/location/bzip2/BZip2Location.java +++ b/src/main/java/org/scijava/io/location/bzip2/BZip2Location.java @@ -1,9 +1,9 @@ package org.scijava.io.location.bzip2; -import org.scijava.io.DataHandle; -import org.scijava.io.Location; +import org.scijava.io.handle.DataHandle; import org.scijava.io.location.AbstractHigherOrderLocation; +import org.scijava.io.location.Location; /** * {@link Location} backed by a {@link DataHandle} that is BZip2 compressed. diff --git a/src/main/java/org/scijava/io/location/bzip2/CBZip2InputStream.java b/src/main/java/org/scijava/io/location/bzip2/CBZip2InputStream.java index ec20cd80c..5f1be45f0 100644 --- a/src/main/java/org/scijava/io/location/bzip2/CBZip2InputStream.java +++ b/src/main/java/org/scijava/io/location/bzip2/CBZip2InputStream.java @@ -56,8 +56,8 @@ import java.io.IOException; import java.io.InputStream; -import org.scijava.io.DataHandle; -import org.scijava.io.Location; +import org.scijava.io.handle.DataHandle; +import org.scijava.io.location.Location; import org.scijava.log.LogService; /** diff --git a/src/main/java/org/scijava/io/location/gzip/GZipHandle.java b/src/main/java/org/scijava/io/location/gzip/GZipHandle.java index 062f2c52a..398505e1d 100644 --- a/src/main/java/org/scijava/io/location/gzip/GZipHandle.java +++ b/src/main/java/org/scijava/io/location/gzip/GZipHandle.java @@ -33,8 +33,8 @@ import java.io.IOException; import java.util.zip.GZIPInputStream; -import org.scijava.io.DataHandle; -import org.scijava.io.DataHandleInputStream; +import org.scijava.io.handle.DataHandle; +import org.scijava.io.handle.DataHandleInputStream; import org.scijava.io.location.AbstractCompressedHandle; import org.scijava.plugin.Plugin; diff --git a/src/main/java/org/scijava/io/location/gzip/GZipLocation.java b/src/main/java/org/scijava/io/location/gzip/GZipLocation.java index b89640846..8392240bc 100644 --- a/src/main/java/org/scijava/io/location/gzip/GZipLocation.java +++ b/src/main/java/org/scijava/io/location/gzip/GZipLocation.java @@ -1,9 +1,9 @@ package org.scijava.io.location.gzip; -import org.scijava.io.DataHandle; -import org.scijava.io.Location; +import org.scijava.io.handle.DataHandle; import org.scijava.io.location.AbstractHigherOrderLocation; +import org.scijava.io.location.Location; /** * {@link Location} backed by a {@link DataHandle} that is gzip diff --git a/src/main/java/org/scijava/io/location/zip/ZipHandle.java b/src/main/java/org/scijava/io/location/zip/ZipHandle.java index 564378551..6818fbe83 100644 --- a/src/main/java/org/scijava/io/location/zip/ZipHandle.java +++ b/src/main/java/org/scijava/io/location/zip/ZipHandle.java @@ -35,12 +35,12 @@ import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; -import org.scijava.io.DataHandle; -import org.scijava.io.DataHandleInputStream; -import org.scijava.io.Location; +import org.scijava.io.handle.DataHandle; +import org.scijava.io.handle.DataHandleInputStream; +import org.scijava.io.handle.ResettableStreamHandle; +import org.scijava.io.handle.StreamHandle; import org.scijava.io.location.AbstractCompressedHandle; -import org.scijava.io.location.ResettableStreamHandle; -import org.scijava.io.location.StreamHandle; +import org.scijava.io.location.Location; import org.scijava.plugin.Plugin; /** @@ -202,4 +202,16 @@ protected void initInputStream() throws IOException { resetStream(); } } + + @Override + public long length() throws IOException { + if (entry == null) { + return -1; + } + return entry.getSize(); + } + + public long getEntryLength() { + return entryLength; + } } diff --git a/src/main/java/org/scijava/io/location/zip/ZipLocation.java b/src/main/java/org/scijava/io/location/zip/ZipLocation.java index 69dcaa482..4ef5d88f1 100644 --- a/src/main/java/org/scijava/io/location/zip/ZipLocation.java +++ b/src/main/java/org/scijava/io/location/zip/ZipLocation.java @@ -33,9 +33,9 @@ import java.util.zip.ZipEntry; -import org.scijava.io.DataHandle; -import org.scijava.io.Location; +import org.scijava.io.handle.DataHandle; import org.scijava.io.location.AbstractHigherOrderLocation; +import org.scijava.io.location.Location; import org.scijava.plugin.Plugin; /** diff --git a/src/test/java/org/scijava/io/BZip2HandleTest.java b/src/test/java/org/scijava/io/handle/BZip2HandleTest.java similarity index 85% rename from src/test/java/org/scijava/io/BZip2HandleTest.java rename to src/test/java/org/scijava/io/handle/BZip2HandleTest.java index 658d0430e..4066c54a2 100644 --- a/src/test/java/org/scijava/io/BZip2HandleTest.java +++ b/src/test/java/org/scijava/io/handle/BZip2HandleTest.java @@ -29,15 +29,19 @@ * #L% */ -package org.scijava.io; +package org.scijava.io.handle; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; +import org.junit.Ignore; +import org.junit.Test; +import org.scijava.io.handle.DataHandle; +import org.scijava.io.location.FileLocation; +import org.scijava.io.location.Location; import org.scijava.io.location.bzip2.BZip2Handle; import org.scijava.io.location.bzip2.BZip2Location; -import org.scijava.io.location.file.FileLocation; /** * Tests {@link BZip2Handle}. @@ -76,16 +80,19 @@ public Location createLocation() throws IOException { return new BZip2Location(new FileLocation(bzip2File)); } + @Test @Override - protected void checkWrites(DataHandle handle) - throws IOException - { - // NB Handle is read only + public void testWriting() throws IOException { + // no Op } @Override - protected void setup() { - checkLength = false; + @Test + public void testReading() throws IOException { + try (final DataHandle handle = createHandle()) { + checkBasicReadMethods(handle, false); + checkEndiannessReading(handle); + } } } diff --git a/src/test/java/org/scijava/io/handle/DataHandleTest.java b/src/test/java/org/scijava/io/handle/DataHandleTest.java index 7c38e3e1a..f2b7d5a16 100644 --- a/src/test/java/org/scijava/io/handle/DataHandleTest.java +++ b/src/test/java/org/scijava/io/handle/DataHandleTest.java @@ -113,7 +113,7 @@ public void testEndianesSettings() throws IOException { @Test public void testReading() throws IOException { try (final DataHandle handle = createHandle()) { - checkBasicReadMethods(handle); + checkBasicReadMethods(handle, true); checkEndiannessReading(handle); } } @@ -168,13 +168,16 @@ public void populateData(final OutputStream out) throws IOException { * Checks basic byte reading methods. * * @param handle the handle to test + * @param checkLength whether to check the total length of the handle * @throws IOException */ public void checkBasicReadMethods( - final DataHandle handle) throws IOException + final DataHandle handle, boolean checkLength) throws IOException { assertEquals(0, handle.offset()); - assertEquals(BYTES.length, handle.length()); + if (checkLength) { + assertEquals(BYTES.length, handle.length()); + } assertEquals("UTF-8", handle.getEncoding()); // test read() diff --git a/src/test/java/org/scijava/io/GZipHandleTest.java b/src/test/java/org/scijava/io/handle/GZipHandleTest.java similarity index 84% rename from src/test/java/org/scijava/io/GZipHandleTest.java rename to src/test/java/org/scijava/io/handle/GZipHandleTest.java index 6c18d56cb..431f79e1b 100644 --- a/src/test/java/org/scijava/io/GZipHandleTest.java +++ b/src/test/java/org/scijava/io/handle/GZipHandleTest.java @@ -29,14 +29,17 @@ * #L% */ -package org.scijava.io; +package org.scijava.io.handle; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.GZIPOutputStream; -import org.scijava.io.location.file.FileLocation; +import org.junit.Ignore; +import org.junit.Test; +import org.scijava.io.location.FileLocation; +import org.scijava.io.location.Location; import org.scijava.io.location.gzip.GZipHandle; import org.scijava.io.location.gzip.GZipLocation; @@ -68,14 +71,18 @@ public Location createLocation() throws IOException { return new GZipLocation(new FileLocation(tmpFile)); } + @Test @Override - protected void checkWrites(DataHandle handle) - throws IOException - { - // NB Handle is read only + public void testWriting() throws IOException { + // no Op } + @Override - protected void setup() { - checkLength = false; + @Test + public void testReading() throws IOException { + try (final DataHandle handle = createHandle()) { + checkBasicReadMethods(handle, false); + checkEndiannessReading(handle); + } } } diff --git a/src/test/java/org/scijava/io/handle/ReadBufferDataHandleTest.java b/src/test/java/org/scijava/io/handle/ReadBufferDataHandleTest.java index e7cb332f2..9f05bd38b 100644 --- a/src/test/java/org/scijava/io/handle/ReadBufferDataHandleTest.java +++ b/src/test/java/org/scijava/io/handle/ReadBufferDataHandleTest.java @@ -64,7 +64,7 @@ public void testSmallBuffer() throws IOException { new ReadBufferDataHandle(handle, 5)) { // check with small buffersize - checkBasicReadMethods(bufferedHandle); + checkBasicReadMethods(bufferedHandle, true); checkEndiannessReading(bufferedHandle); } } @@ -127,7 +127,6 @@ public void testLargeRead() throws Exception { } @Test - @Ignore @Override public void testWriting() throws IOException { // nothing to do here diff --git a/src/test/java/org/scijava/io/handle/WriteBufferDataHandleTest.java b/src/test/java/org/scijava/io/handle/WriteBufferDataHandleTest.java index 40f035e3a..6ea468872 100644 --- a/src/test/java/org/scijava/io/handle/WriteBufferDataHandleTest.java +++ b/src/test/java/org/scijava/io/handle/WriteBufferDataHandleTest.java @@ -64,14 +64,12 @@ public DataHandle createHandle() { } @Test - @Ignore @Override public void testReading() throws IOException { // nothing to do } @Test - @Ignore @Override public void checkSkip() throws IOException { // nothing to do diff --git a/src/test/java/org/scijava/io/ZipHandleTest.java b/src/test/java/org/scijava/io/handle/ZipHandleTest.java similarity index 84% rename from src/test/java/org/scijava/io/ZipHandleTest.java rename to src/test/java/org/scijava/io/handle/ZipHandleTest.java index 4c953f8a4..c6595eb7f 100644 --- a/src/test/java/org/scijava/io/ZipHandleTest.java +++ b/src/test/java/org/scijava/io/handle/ZipHandleTest.java @@ -29,7 +29,7 @@ * #L% */ -package org.scijava.io; +package org.scijava.io.handle; import java.io.File; import java.io.FileOutputStream; @@ -37,7 +37,11 @@ import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; -import org.scijava.io.location.file.FileLocation; +import org.junit.Ignore; +import org.junit.Test; +import org.scijava.io.handle.DataHandle; +import org.scijava.io.location.FileLocation; +import org.scijava.io.location.Location; import org.scijava.io.location.zip.ZipHandle; import org.scijava.io.location.zip.ZipLocation; @@ -69,15 +73,18 @@ public Location createLocation() throws IOException { return new ZipLocation(new FileLocation(tmpFile)); } + @Test @Override - protected void checkWrites(DataHandle handle) - throws IOException - { - // NB Handle is read only + public void testWriting() throws IOException { + // no Op } @Override - protected void setup() { - checkLength = false; + @Test + public void testReading() throws IOException { + try (final DataHandle handle = createHandle()) { + checkBasicReadMethods(handle, false); + checkEndiannessReading(handle); + } } } diff --git a/src/test/java/org/scijava/io/BZip2LocationTest.java b/src/test/java/org/scijava/io/location/BZip2LocationTest.java similarity index 94% rename from src/test/java/org/scijava/io/BZip2LocationTest.java rename to src/test/java/org/scijava/io/location/BZip2LocationTest.java index b0324f3e8..8fb7e6225 100644 --- a/src/test/java/org/scijava/io/BZip2LocationTest.java +++ b/src/test/java/org/scijava/io/location/BZip2LocationTest.java @@ -29,15 +29,16 @@ * #L% */ -package org.scijava.io; +package org.scijava.io.location; import static org.junit.Assert.assertEquals; import java.io.IOException; import org.junit.Test; +import org.scijava.io.location.FileLocation; +import org.scijava.io.location.Location; import org.scijava.io.location.bzip2.BZip2Location; -import org.scijava.io.location.file.FileLocation; /** * Tests {@link BZip2Location}. diff --git a/src/test/java/org/scijava/io/GZipLocationTest.java b/src/test/java/org/scijava/io/location/GZipLocationTest.java similarity index 94% rename from src/test/java/org/scijava/io/GZipLocationTest.java rename to src/test/java/org/scijava/io/location/GZipLocationTest.java index a10a2cad5..cdac69612 100644 --- a/src/test/java/org/scijava/io/GZipLocationTest.java +++ b/src/test/java/org/scijava/io/location/GZipLocationTest.java @@ -29,14 +29,15 @@ * #L% */ -package org.scijava.io; +package org.scijava.io.location; import static org.junit.Assert.assertEquals; import java.io.IOException; import org.junit.Test; -import org.scijava.io.location.file.FileLocation; +import org.scijava.io.location.FileLocation; +import org.scijava.io.location.Location; import org.scijava.io.location.gzip.GZipLocation; /** diff --git a/src/test/java/org/scijava/io/ZipLocationTest.java b/src/test/java/org/scijava/io/location/ZipLocationTest.java similarity index 94% rename from src/test/java/org/scijava/io/ZipLocationTest.java rename to src/test/java/org/scijava/io/location/ZipLocationTest.java index 4acc19749..0fa86ae72 100644 --- a/src/test/java/org/scijava/io/ZipLocationTest.java +++ b/src/test/java/org/scijava/io/location/ZipLocationTest.java @@ -29,14 +29,15 @@ * #L% */ -package org.scijava.io; +package org.scijava.io.location; import static org.junit.Assert.assertEquals; import java.io.IOException; import org.junit.Test; -import org.scijava.io.location.file.FileLocation; +import org.scijava.io.location.FileLocation; +import org.scijava.io.location.Location; import org.scijava.io.location.zip.ZipLocation; /**