michael@0: /* michael@0: * Copyright (C) 2013 Square, Inc. michael@0: * michael@0: * Licensed under the Apache License, Version 2.0 (the "License"); michael@0: * you may not use this file except in compliance with the License. michael@0: * You may obtain a copy of the License at michael@0: * michael@0: * http://www.apache.org/licenses/LICENSE-2.0 michael@0: * michael@0: * Unless required by applicable law or agreed to in writing, software michael@0: * distributed under the License is distributed on an "AS IS" BASIS, michael@0: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. michael@0: * See the License for the specific language governing permissions and michael@0: * limitations under the License. michael@0: */ michael@0: package com.squareup.picasso; michael@0: michael@0: import java.io.BufferedInputStream; michael@0: import java.io.IOException; michael@0: import java.io.InputStream; michael@0: michael@0: /** michael@0: * An input stream wrapper that supports unlimited independent cursors for michael@0: * marking and resetting. Each cursor is a token, and it's the caller's michael@0: * responsibility to keep track of these. michael@0: */ michael@0: final class MarkableInputStream extends InputStream { michael@0: private final InputStream in; michael@0: michael@0: private long offset; michael@0: private long reset; michael@0: private long limit; michael@0: michael@0: private long defaultMark = -1; michael@0: michael@0: public MarkableInputStream(InputStream in) { michael@0: if (!in.markSupported()) { michael@0: in = new BufferedInputStream(in); michael@0: } michael@0: this.in = in; michael@0: } michael@0: michael@0: /** Marks this place in the stream so we can reset back to it later. */ michael@0: @Override public void mark(int readLimit) { michael@0: defaultMark = savePosition(readLimit); michael@0: } michael@0: michael@0: /** michael@0: * Returns an opaque token representing the current position in the stream. michael@0: * Call {@link #reset(long)} to return to this position in the stream later. michael@0: * It is an error to call {@link #reset(long)} after consuming more than michael@0: * {@code readLimit} bytes from this stream. michael@0: */ michael@0: public long savePosition(int readLimit) { michael@0: long offsetLimit = offset + readLimit; michael@0: if (limit < offsetLimit) { michael@0: setLimit(offsetLimit); michael@0: } michael@0: return offset; michael@0: } michael@0: michael@0: /** michael@0: * Makes sure that the underlying stream can backtrack the full range from michael@0: * {@code reset} thru {@code limit}. Since we can't call {@code mark()} michael@0: * without also adjusting the reset-to-position on the underlying stream this michael@0: * method resets first and then marks the union of the two byte ranges. On michael@0: * buffered streams this additional cursor motion shouldn't result in any michael@0: * additional I/O. michael@0: */ michael@0: private void setLimit(long limit) { michael@0: try { michael@0: if (reset < offset && offset <= this.limit) { michael@0: in.reset(); michael@0: in.mark((int) (limit - reset)); michael@0: skip(reset, offset); michael@0: } else { michael@0: reset = offset; michael@0: in.mark((int) (limit - offset)); michael@0: } michael@0: this.limit = limit; michael@0: } catch (IOException e) { michael@0: throw new IllegalStateException("Unable to mark: " + e); michael@0: } michael@0: } michael@0: michael@0: /** Resets the stream to the most recent {@link #mark mark}. */ michael@0: @Override public void reset() throws IOException { michael@0: reset(defaultMark); michael@0: } michael@0: michael@0: /** Resets the stream to the position recorded by {@code token}. */ michael@0: public void reset(long token) throws IOException { michael@0: if (offset > limit || token < reset) { michael@0: throw new IOException("Cannot reset"); michael@0: } michael@0: in.reset(); michael@0: skip(reset, token); michael@0: offset = token; michael@0: } michael@0: michael@0: /** Skips {@code target - current} bytes and returns. */ michael@0: private void skip(long current, long target) throws IOException { michael@0: while (current < target) { michael@0: long skipped = in.skip(target - current); michael@0: if (skipped == 0) { michael@0: if (read() == -1) { michael@0: break; // EOF michael@0: } else { michael@0: skipped = 1; michael@0: } michael@0: } michael@0: current += skipped; michael@0: } michael@0: } michael@0: michael@0: @Override public int read() throws IOException { michael@0: int result = in.read(); michael@0: if (result != -1) { michael@0: offset++; michael@0: } michael@0: return result; michael@0: } michael@0: michael@0: @Override public int read(byte[] buffer) throws IOException { michael@0: int count = in.read(buffer); michael@0: if (count != -1) { michael@0: offset += count; michael@0: } michael@0: return count; michael@0: } michael@0: michael@0: @Override public int read(byte[] buffer, int offset, int length) throws IOException { michael@0: int count = in.read(buffer, offset, length); michael@0: if (count != -1) { michael@0: this.offset += count; michael@0: } michael@0: return count; michael@0: } michael@0: michael@0: @Override public long skip(long byteCount) throws IOException { michael@0: long skipped = in.skip(byteCount); michael@0: offset += skipped; michael@0: return skipped; michael@0: } michael@0: michael@0: @Override public int available() throws IOException { michael@0: return in.available(); michael@0: } michael@0: michael@0: @Override public void close() throws IOException { michael@0: in.close(); michael@0: } michael@0: michael@0: @Override public boolean markSupported() { michael@0: return in.markSupported(); michael@0: } michael@0: }