@@ -58,14 +58,14 @@ public InputStreamAt(byte[] data) {
5858 mData = data ;
5959 }
6060
61- public long getCrc32 (long offset , int length ) {
61+ public long getCrc32 (long offset , int length ) throws IOException {
6262 CRC32 crc32 = new CRC32 ();
6363 byte [] data = read (offset , length );
6464 crc32 .update (data );
6565 return crc32 .getValue ();
6666 }
6767
68- public long crc32 () {
68+ public long crc32 () throws IOException {
6969 if (mCrc32 >= 0 ) return mCrc32 ;
7070 CRC32 crc32 = new CRC32 ();
7171 long index = 0 ;
@@ -120,34 +120,31 @@ protected static File storeToFile(Context context, InputStream is) {
120120 }
121121 }
122122
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 ();
123+ public byte [] read (long offset , int length ) throws IOException {
124+ if (mClosed ) throw new IOException ("inputStreamAt closed" );
125+ if (mFileStream != null ) {
126+ return fileStreamRead (offset , length );
136127 }
137-
138- return null ;
128+ if (mData != null ) {
129+ byte [] ret = new byte [length ];
130+ System .arraycopy (mData , (int ) offset , ret , 0 , length );
131+ return ret ;
132+ }
133+ throw new IOException ("inputStreamAt not init" );
139134 }
140135
141136 protected byte [] fileStreamRead (long offset , int length ) throws IOException {
142137 if (mFileStream == null ) return null ;
138+ long fileLength = mFileStream .length ();
139+ if (length + offset > fileLength ) length = (int ) (fileLength - offset );
143140 byte [] data = new byte [length ];
144141
145142 int read ;
146143 int totalRead = 0 ;
147144 synchronized (data ) {
148145 mFileStream .seek (offset );
149146 do {
150- read = mFileStream .read (data , totalRead , length );
147+ read = mFileStream .read (data , totalRead , length - totalRead );
151148 if (read <= 0 ) break ;
152149 totalRead += read ;
153150 } while (length > totalRead );
0 commit comments