|
| 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; |
| 33 | + |
| 34 | +import java.io.IOException; |
| 35 | +import java.io.InputStream; |
| 36 | +import java.io.OutputStream; |
| 37 | + |
| 38 | +/** |
| 39 | + * A {@link BufferedStreamHandle} backed by an {@link IOBuffer}. |
| 40 | + * |
| 41 | + * @author Gabriel Einsdorf |
| 42 | + */ |
| 43 | +public class DefaultBufferedStreamHandle<L extends Location> extends |
| 44 | + AbstractStreamHandle<L> implements BufferedStreamHandle<L> |
| 45 | +{ |
| 46 | + |
| 47 | + private static final int CHUNK_SIZE = 8192; |
| 48 | + |
| 49 | + private final StreamHandle<L> handle; |
| 50 | + private IOBuffer buffer; |
| 51 | + |
| 52 | + private InputStream inStreamProxy; |
| 53 | + private OutputStream outStreamProxy; |
| 54 | + |
| 55 | + private boolean closed; |
| 56 | + |
| 57 | + /** |
| 58 | + * Wraps around StreamHandle in a buffer |
| 59 | + * |
| 60 | + * @param handle |
| 61 | + */ |
| 62 | + public DefaultBufferedStreamHandle(final StreamHandle<L> handle) { |
| 63 | + this.handle = handle; |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public byte readByte() throws IOException { |
| 68 | + |
| 69 | + // reached end of the buffer |
| 70 | + if (offset() == handle.length()) { |
| 71 | + return -1; |
| 72 | + } |
| 73 | + |
| 74 | + // check if we need to refill the buffer |
| 75 | + if (offset() > maxBuf() || maxBuf() == -1) { |
| 76 | + // buffer more bytes |
| 77 | + final int filled = fill(); |
| 78 | + if (filled <= 0) { |
| 79 | + // no more bytes in input handle |
| 80 | + return -1; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + byte b = getBufferIfOpen().getByte(offset()); |
| 85 | + advance(1); |
| 86 | + return b; |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public void seek(final long pos) throws IOException { |
| 91 | + final long off = offset(); |
| 92 | + if (off == pos) return; |
| 93 | + if (pos > off) { |
| 94 | + // ensure target is buffered |
| 95 | + while (pos > maxBuf()) { |
| 96 | + fill(); |
| 97 | + } |
| 98 | + } |
| 99 | + // values in the range (pos < off) are already buffered |
| 100 | + setOffset(pos); |
| 101 | + } |
| 102 | + |
| 103 | + private long maxBuf() throws IOException { |
| 104 | + return getBufferIfOpen().getMaxPos(); |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public long skip(final long n) throws IOException { |
| 109 | + seek(offset() + n); |
| 110 | + return handle().skip(n); |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public int read(final byte[] b, final int off, final int len) |
| 115 | + throws IOException |
| 116 | + { |
| 117 | + |
| 118 | + while (maxBuf() < offset() + len) { |
| 119 | + final int filled = fill(); |
| 120 | + if (filled <= 0) { |
| 121 | + // no more bytes available |
| 122 | + break; |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + // read all available bytes |
| 127 | + int available = (int) available(len); |
| 128 | + getBufferIfOpen().getBytes(offset(), b, off, available); |
| 129 | + setOffset(offset() + available); |
| 130 | + return available; |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Fills the buffer with XX more bytes |
| 135 | + * |
| 136 | + * @throws IOException |
| 137 | + */ |
| 138 | + private int fill() throws IOException { |
| 139 | + final byte[] buf = new byte[CHUNK_SIZE]; |
| 140 | + final int read = handle().read(buf); |
| 141 | + if (read <= 0) { |
| 142 | + return -1; |
| 143 | + } |
| 144 | + getBufferIfOpen().appendBytes(buf, read); |
| 145 | + return read; |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + public InputStream in() { |
| 150 | + if (inStreamProxy == null) { |
| 151 | + inStreamProxy = new DataHandleInputStream<>(this); |
| 152 | + } |
| 153 | + return inStreamProxy; |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + public OutputStream out() { |
| 158 | + if (outStreamProxy == null) { |
| 159 | + outStreamProxy = new DataHandleOutputStream<>(this); |
| 160 | + } |
| 161 | + return outStreamProxy; |
| 162 | + } |
| 163 | + |
| 164 | + @Override |
| 165 | + public long length() throws IOException { |
| 166 | + return handle().length(); |
| 167 | + } |
| 168 | + |
| 169 | + private StreamHandle<L> handle() throws IOException { |
| 170 | + if (closed) { |
| 171 | + throw new IOException("Handle is closed!"); |
| 172 | + } |
| 173 | + return handle; |
| 174 | + } |
| 175 | + |
| 176 | + @Override |
| 177 | + public void setLength(final long length) throws IOException { |
| 178 | + handle().setLength(length); |
| 179 | + } |
| 180 | + |
| 181 | + @Override |
| 182 | + public boolean isReadable() { |
| 183 | + return !closed && handle.isReadable(); |
| 184 | + } |
| 185 | + |
| 186 | + @Override |
| 187 | + public boolean isWritable() { |
| 188 | + return !closed && handle.isWritable(); |
| 189 | + } |
| 190 | + |
| 191 | + @Override |
| 192 | + public Class<L> getType() { |
| 193 | + return handle.getType(); |
| 194 | + } |
| 195 | + |
| 196 | + @Override |
| 197 | + public void resetStream() throws IOException { |
| 198 | + getBufferIfOpen(); |
| 199 | + if (handle() instanceof ResettableStreamHandle) { |
| 200 | + ((ResettableStreamHandle<L>) handle()).resetStream(); |
| 201 | + } |
| 202 | + else { |
| 203 | + throw new IOException("Handle can not be reset!"); |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + @Override |
| 208 | + public void close() throws IOException { |
| 209 | + if (!closed) { |
| 210 | + closed = true; |
| 211 | + handle().close(); |
| 212 | + getBufferIfOpen().clear(); |
| 213 | + buffer = null; |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + /** |
| 218 | + * @return the buffer used in this handle |
| 219 | + * @throws IOException if this handle has been closed |
| 220 | + */ |
| 221 | + private IOBuffer getBufferIfOpen() throws IOException { |
| 222 | + if (closed) { |
| 223 | + throw new IOException("Handle is closed"); |
| 224 | + } |
| 225 | + if (buffer == null) { |
| 226 | + buffer = new ByteArrayIOBuffer(); |
| 227 | + } |
| 228 | + return buffer; |
| 229 | + } |
| 230 | +} |
0 commit comments