|
| 1 | +/* |
| 2 | + * #%L |
| 3 | + * SciJava Common shared library for SciJava software. |
| 4 | + * %% |
| 5 | + * Copyright (C) 2009 - 2017 Board of Regents of the University of |
| 6 | + * Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck |
| 7 | + * Institute of Molecular Cell Biology and Genetics. |
| 8 | + * %% |
| 9 | + * Redistribution and use in source and binary forms, with or without |
| 10 | + * modification, are permitted provided that the following conditions are met: |
| 11 | + * |
| 12 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 13 | + * this list of conditions and the following disclaimer. |
| 14 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 15 | + * this list of conditions and the following disclaimer in the documentation |
| 16 | + * and/or other materials provided with the distribution. |
| 17 | + * |
| 18 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE |
| 22 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 26 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 28 | + * POSSIBILITY OF SUCH DAMAGE. |
| 29 | + * #L% |
| 30 | + */ |
| 31 | + |
| 32 | +package org.scijava.io.location; |
| 33 | + |
| 34 | +import java.io.IOException; |
| 35 | +import java.io.InputStream; |
| 36 | +import java.io.OutputStream; |
| 37 | + |
| 38 | +import org.scijava.io.DataHandle; |
| 39 | +import org.scijava.io.DataHandleService; |
| 40 | +import org.scijava.io.Location; |
| 41 | +import org.scijava.plugin.Parameter; |
| 42 | + |
| 43 | +/** |
| 44 | + * Abstract superclass for {@link DataHandle}s that operate on compressed data. |
| 45 | + * |
| 46 | + * @author Gabriel Einsdorf |
| 47 | + */ |
| 48 | +public abstract class AbstractCompressedHandle<L extends AbstractHigherOrderLocation> |
| 49 | + extends AbstractStreamHandle<L> implements ResettableStreamHandle<L> |
| 50 | +{ |
| 51 | + |
| 52 | + private DataHandle<Location> rawHandle; |
| 53 | + protected InputStream inputStream; |
| 54 | + |
| 55 | + @Parameter |
| 56 | + private DataHandleService dataHandleService; |
| 57 | + |
| 58 | + public AbstractCompressedHandle() { |
| 59 | + super(); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public void resetStream() throws IOException { |
| 64 | + |
| 65 | + if (raw() instanceof ResettableStreamHandle<?>) { |
| 66 | + ((ResettableStreamHandle<Location>) rawHandle).resetStream(); |
| 67 | + } |
| 68 | + else { |
| 69 | + rawHandle.seek(0); |
| 70 | + } |
| 71 | + initInputStream(); |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public InputStream in() throws IOException { |
| 76 | + if (inputStream == null) { |
| 77 | + initInputStream(); |
| 78 | + } |
| 79 | + return inputStream; |
| 80 | + } |
| 81 | + |
| 82 | + protected abstract void initInputStream() throws IOException; |
| 83 | + |
| 84 | + @Override |
| 85 | + public OutputStream out() throws IOException { |
| 86 | + return null; |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public boolean isWritable() { |
| 91 | + return false; |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public boolean isReadable() { |
| 96 | + return true; |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public long length() throws IOException { |
| 101 | + return raw().length(); |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public void setLength(long length) throws IOException { |
| 106 | + throw new IOException("This handle " + this.getClass().getSimpleName() + |
| 107 | + " is read-only!"); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * @return the raw underlying DataHandle (not decompressed) |
| 112 | + */ |
| 113 | + protected DataHandle<Location> raw() { |
| 114 | + if (rawHandle == null) { |
| 115 | + rawHandle = dataHandleService.create(get().getBaseLocation()); |
| 116 | + } |
| 117 | + return rawHandle; |
| 118 | + } |
| 119 | + |
| 120 | +} |
0 commit comments