|
1 /* |
|
2 * Copyright (C) 2013 Square, Inc. |
|
3 * |
|
4 * Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 * you may not use this file except in compliance with the License. |
|
6 * You may obtain a copy of the License at |
|
7 * |
|
8 * http://www.apache.org/licenses/LICENSE-2.0 |
|
9 * |
|
10 * Unless required by applicable law or agreed to in writing, software |
|
11 * distributed under the License is distributed on an "AS IS" BASIS, |
|
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 * See the License for the specific language governing permissions and |
|
14 * limitations under the License. |
|
15 */ |
|
16 package com.squareup.picasso; |
|
17 |
|
18 import java.io.BufferedInputStream; |
|
19 import java.io.IOException; |
|
20 import java.io.InputStream; |
|
21 |
|
22 /** |
|
23 * An input stream wrapper that supports unlimited independent cursors for |
|
24 * marking and resetting. Each cursor is a token, and it's the caller's |
|
25 * responsibility to keep track of these. |
|
26 */ |
|
27 final class MarkableInputStream extends InputStream { |
|
28 private final InputStream in; |
|
29 |
|
30 private long offset; |
|
31 private long reset; |
|
32 private long limit; |
|
33 |
|
34 private long defaultMark = -1; |
|
35 |
|
36 public MarkableInputStream(InputStream in) { |
|
37 if (!in.markSupported()) { |
|
38 in = new BufferedInputStream(in); |
|
39 } |
|
40 this.in = in; |
|
41 } |
|
42 |
|
43 /** Marks this place in the stream so we can reset back to it later. */ |
|
44 @Override public void mark(int readLimit) { |
|
45 defaultMark = savePosition(readLimit); |
|
46 } |
|
47 |
|
48 /** |
|
49 * Returns an opaque token representing the current position in the stream. |
|
50 * Call {@link #reset(long)} to return to this position in the stream later. |
|
51 * It is an error to call {@link #reset(long)} after consuming more than |
|
52 * {@code readLimit} bytes from this stream. |
|
53 */ |
|
54 public long savePosition(int readLimit) { |
|
55 long offsetLimit = offset + readLimit; |
|
56 if (limit < offsetLimit) { |
|
57 setLimit(offsetLimit); |
|
58 } |
|
59 return offset; |
|
60 } |
|
61 |
|
62 /** |
|
63 * Makes sure that the underlying stream can backtrack the full range from |
|
64 * {@code reset} thru {@code limit}. Since we can't call {@code mark()} |
|
65 * without also adjusting the reset-to-position on the underlying stream this |
|
66 * method resets first and then marks the union of the two byte ranges. On |
|
67 * buffered streams this additional cursor motion shouldn't result in any |
|
68 * additional I/O. |
|
69 */ |
|
70 private void setLimit(long limit) { |
|
71 try { |
|
72 if (reset < offset && offset <= this.limit) { |
|
73 in.reset(); |
|
74 in.mark((int) (limit - reset)); |
|
75 skip(reset, offset); |
|
76 } else { |
|
77 reset = offset; |
|
78 in.mark((int) (limit - offset)); |
|
79 } |
|
80 this.limit = limit; |
|
81 } catch (IOException e) { |
|
82 throw new IllegalStateException("Unable to mark: " + e); |
|
83 } |
|
84 } |
|
85 |
|
86 /** Resets the stream to the most recent {@link #mark mark}. */ |
|
87 @Override public void reset() throws IOException { |
|
88 reset(defaultMark); |
|
89 } |
|
90 |
|
91 /** Resets the stream to the position recorded by {@code token}. */ |
|
92 public void reset(long token) throws IOException { |
|
93 if (offset > limit || token < reset) { |
|
94 throw new IOException("Cannot reset"); |
|
95 } |
|
96 in.reset(); |
|
97 skip(reset, token); |
|
98 offset = token; |
|
99 } |
|
100 |
|
101 /** Skips {@code target - current} bytes and returns. */ |
|
102 private void skip(long current, long target) throws IOException { |
|
103 while (current < target) { |
|
104 long skipped = in.skip(target - current); |
|
105 if (skipped == 0) { |
|
106 if (read() == -1) { |
|
107 break; // EOF |
|
108 } else { |
|
109 skipped = 1; |
|
110 } |
|
111 } |
|
112 current += skipped; |
|
113 } |
|
114 } |
|
115 |
|
116 @Override public int read() throws IOException { |
|
117 int result = in.read(); |
|
118 if (result != -1) { |
|
119 offset++; |
|
120 } |
|
121 return result; |
|
122 } |
|
123 |
|
124 @Override public int read(byte[] buffer) throws IOException { |
|
125 int count = in.read(buffer); |
|
126 if (count != -1) { |
|
127 offset += count; |
|
128 } |
|
129 return count; |
|
130 } |
|
131 |
|
132 @Override public int read(byte[] buffer, int offset, int length) throws IOException { |
|
133 int count = in.read(buffer, offset, length); |
|
134 if (count != -1) { |
|
135 this.offset += count; |
|
136 } |
|
137 return count; |
|
138 } |
|
139 |
|
140 @Override public long skip(long byteCount) throws IOException { |
|
141 long skipped = in.skip(byteCount); |
|
142 offset += skipped; |
|
143 return skipped; |
|
144 } |
|
145 |
|
146 @Override public int available() throws IOException { |
|
147 return in.available(); |
|
148 } |
|
149 |
|
150 @Override public void close() throws IOException { |
|
151 in.close(); |
|
152 } |
|
153 |
|
154 @Override public boolean markSupported() { |
|
155 return in.markSupported(); |
|
156 } |
|
157 } |