1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/thirdparty/com/squareup/picasso/MarkableInputStream.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,157 @@ 1.4 +/* 1.5 + * Copyright (C) 2013 Square, Inc. 1.6 + * 1.7 + * Licensed under the Apache License, Version 2.0 (the "License"); 1.8 + * you may not use this file except in compliance with the License. 1.9 + * You may obtain a copy of the License at 1.10 + * 1.11 + * http://www.apache.org/licenses/LICENSE-2.0 1.12 + * 1.13 + * Unless required by applicable law or agreed to in writing, software 1.14 + * distributed under the License is distributed on an "AS IS" BASIS, 1.15 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1.16 + * See the License for the specific language governing permissions and 1.17 + * limitations under the License. 1.18 + */ 1.19 +package com.squareup.picasso; 1.20 + 1.21 +import java.io.BufferedInputStream; 1.22 +import java.io.IOException; 1.23 +import java.io.InputStream; 1.24 + 1.25 +/** 1.26 + * An input stream wrapper that supports unlimited independent cursors for 1.27 + * marking and resetting. Each cursor is a token, and it's the caller's 1.28 + * responsibility to keep track of these. 1.29 + */ 1.30 +final class MarkableInputStream extends InputStream { 1.31 + private final InputStream in; 1.32 + 1.33 + private long offset; 1.34 + private long reset; 1.35 + private long limit; 1.36 + 1.37 + private long defaultMark = -1; 1.38 + 1.39 + public MarkableInputStream(InputStream in) { 1.40 + if (!in.markSupported()) { 1.41 + in = new BufferedInputStream(in); 1.42 + } 1.43 + this.in = in; 1.44 + } 1.45 + 1.46 + /** Marks this place in the stream so we can reset back to it later. */ 1.47 + @Override public void mark(int readLimit) { 1.48 + defaultMark = savePosition(readLimit); 1.49 + } 1.50 + 1.51 + /** 1.52 + * Returns an opaque token representing the current position in the stream. 1.53 + * Call {@link #reset(long)} to return to this position in the stream later. 1.54 + * It is an error to call {@link #reset(long)} after consuming more than 1.55 + * {@code readLimit} bytes from this stream. 1.56 + */ 1.57 + public long savePosition(int readLimit) { 1.58 + long offsetLimit = offset + readLimit; 1.59 + if (limit < offsetLimit) { 1.60 + setLimit(offsetLimit); 1.61 + } 1.62 + return offset; 1.63 + } 1.64 + 1.65 + /** 1.66 + * Makes sure that the underlying stream can backtrack the full range from 1.67 + * {@code reset} thru {@code limit}. Since we can't call {@code mark()} 1.68 + * without also adjusting the reset-to-position on the underlying stream this 1.69 + * method resets first and then marks the union of the two byte ranges. On 1.70 + * buffered streams this additional cursor motion shouldn't result in any 1.71 + * additional I/O. 1.72 + */ 1.73 + private void setLimit(long limit) { 1.74 + try { 1.75 + if (reset < offset && offset <= this.limit) { 1.76 + in.reset(); 1.77 + in.mark((int) (limit - reset)); 1.78 + skip(reset, offset); 1.79 + } else { 1.80 + reset = offset; 1.81 + in.mark((int) (limit - offset)); 1.82 + } 1.83 + this.limit = limit; 1.84 + } catch (IOException e) { 1.85 + throw new IllegalStateException("Unable to mark: " + e); 1.86 + } 1.87 + } 1.88 + 1.89 + /** Resets the stream to the most recent {@link #mark mark}. */ 1.90 + @Override public void reset() throws IOException { 1.91 + reset(defaultMark); 1.92 + } 1.93 + 1.94 + /** Resets the stream to the position recorded by {@code token}. */ 1.95 + public void reset(long token) throws IOException { 1.96 + if (offset > limit || token < reset) { 1.97 + throw new IOException("Cannot reset"); 1.98 + } 1.99 + in.reset(); 1.100 + skip(reset, token); 1.101 + offset = token; 1.102 + } 1.103 + 1.104 + /** Skips {@code target - current} bytes and returns. */ 1.105 + private void skip(long current, long target) throws IOException { 1.106 + while (current < target) { 1.107 + long skipped = in.skip(target - current); 1.108 + if (skipped == 0) { 1.109 + if (read() == -1) { 1.110 + break; // EOF 1.111 + } else { 1.112 + skipped = 1; 1.113 + } 1.114 + } 1.115 + current += skipped; 1.116 + } 1.117 + } 1.118 + 1.119 + @Override public int read() throws IOException { 1.120 + int result = in.read(); 1.121 + if (result != -1) { 1.122 + offset++; 1.123 + } 1.124 + return result; 1.125 + } 1.126 + 1.127 + @Override public int read(byte[] buffer) throws IOException { 1.128 + int count = in.read(buffer); 1.129 + if (count != -1) { 1.130 + offset += count; 1.131 + } 1.132 + return count; 1.133 + } 1.134 + 1.135 + @Override public int read(byte[] buffer, int offset, int length) throws IOException { 1.136 + int count = in.read(buffer, offset, length); 1.137 + if (count != -1) { 1.138 + this.offset += count; 1.139 + } 1.140 + return count; 1.141 + } 1.142 + 1.143 + @Override public long skip(long byteCount) throws IOException { 1.144 + long skipped = in.skip(byteCount); 1.145 + offset += skipped; 1.146 + return skipped; 1.147 + } 1.148 + 1.149 + @Override public int available() throws IOException { 1.150 + return in.available(); 1.151 + } 1.152 + 1.153 + @Override public void close() throws IOException { 1.154 + in.close(); 1.155 + } 1.156 + 1.157 + @Override public boolean markSupported() { 1.158 + return in.markSupported(); 1.159 + } 1.160 +}