|
7 | 7 | import org.apache.http.entity.AbstractHttpEntity;
|
8 | 8 |
|
9 | 9 | import java.io.*;
|
10 |
| -import java.util.Arrays; |
11 | 10 | import java.util.zip.CRC32;
|
12 | 11 |
|
13 | 12 | public class InputStreamAt implements Closeable {
|
@@ -58,14 +57,14 @@ public InputStreamAt(byte[] data) {
|
58 | 57 | mData = data;
|
59 | 58 | }
|
60 | 59 |
|
61 |
| - public long getCrc32(long offset, int length) { |
| 60 | + public long getCrc32(long offset, int length) throws IOException { |
62 | 61 | CRC32 crc32 = new CRC32();
|
63 | 62 | byte[] data = read(offset, length);
|
64 | 63 | crc32.update(data);
|
65 | 64 | return crc32.getValue();
|
66 | 65 | }
|
67 | 66 |
|
68 |
| - public long crc32() { |
| 67 | + public long crc32() throws IOException { |
69 | 68 | if (mCrc32 >= 0) return mCrc32;
|
70 | 69 | CRC32 crc32 = new CRC32();
|
71 | 70 | long index = 0;
|
@@ -120,45 +119,50 @@ protected static File storeToFile(Context context, InputStream is) {
|
120 | 119 | }
|
121 | 120 | }
|
122 | 121 |
|
123 |
| - public byte[] read(long offset, int length) { |
124 |
| - if (mClosed) return null; |
125 |
| - try { |
126 |
| - if (mFileStream != null) { |
127 |
| - return fileStreamRead(offset, length); |
128 |
| - } |
129 |
| - if (mData != null) { |
130 |
| - byte[] ret = new byte[length]; |
131 |
| - System.arraycopy(mData, (int) offset, ret, 0, length); |
132 |
| - return ret; |
133 |
| - } |
134 |
| - } catch (IOException e) { |
135 |
| - e.printStackTrace(); |
| 122 | + public byte[] read(long offset, int length) throws IOException { |
| 123 | + if (mClosed) throw new IOException("inputStreamAt closed"); |
| 124 | + if (mFileStream != null) { |
| 125 | + return fileStreamRead(offset, length); |
136 | 126 | }
|
137 |
| - |
138 |
| - return null; |
| 127 | + if (mData != null) { |
| 128 | + byte[] ret = new byte[length]; |
| 129 | + System.arraycopy(mData, (int) offset, ret, 0, length); |
| 130 | + return ret; |
| 131 | + } |
| 132 | + throw new IOException("inputStreamAt not init"); |
139 | 133 | }
|
140 | 134 |
|
141 | 135 | protected byte[] fileStreamRead(long offset, int length) throws IOException {
|
142 | 136 | if (mFileStream == null) return null;
|
| 137 | + long fileLength = mFileStream.length(); |
| 138 | + if (length + offset > fileLength) length = (int) (fileLength - offset); |
143 | 139 | byte[] data = new byte[length];
|
144 | 140 |
|
145 | 141 | int read;
|
146 | 142 | int totalRead = 0;
|
147 | 143 | synchronized (data) {
|
148 | 144 | mFileStream.seek(offset);
|
149 | 145 | do {
|
150 |
| - read = mFileStream.read(data, totalRead, length); |
| 146 | + read = mFileStream.read(data, totalRead, length - totalRead); |
151 | 147 | if (read <= 0) break;
|
152 | 148 | totalRead += read;
|
153 | 149 | } while (length > totalRead);
|
154 | 150 | }
|
155 | 151 |
|
156 | 152 | if (totalRead != data.length) {
|
157 |
| - data = Arrays.copyOfRange(data, 0, totalRead); |
| 153 | + data = copyOfRange(data, 0, totalRead); |
158 | 154 | }
|
159 | 155 | return data;
|
160 | 156 | }
|
161 | 157 |
|
| 158 | + public static byte[] copyOfRange(byte[] original, int from, int to) { |
| 159 | + int newLength = to - from; |
| 160 | + if (newLength < 0) throw new IllegalArgumentException(from + " > " + to); |
| 161 | + byte[] copy = new byte[newLength]; |
| 162 | + System.arraycopy(original, from, copy, 0, Math.min(original.length - from, newLength)); |
| 163 | + return copy; |
| 164 | + } |
| 165 | + |
162 | 166 | @Override
|
163 | 167 | public synchronized void close(){
|
164 | 168 | if (mClosed) return;
|
|
0 commit comments