Tomasz Mikolajewski | 5ed6983 | 2016-11-30 17:43:57 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 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 | |
| 17 | package com.android.documentsui.archives; |
| 18 | |
felkachang | 1556c36 | 2018-08-30 16:21:59 +0800 | [diff] [blame^] | 19 | import android.os.FileUtils; |
Tomasz Mikolajewski | 5ed6983 | 2016-11-30 17:43:57 +0900 | [diff] [blame] | 20 | import android.os.ProxyFileDescriptorCallback; |
| 21 | import android.system.ErrnoException; |
| 22 | import android.system.OsConstants; |
Tomasz Mikolajewski | 5ed6983 | 2016-11-30 17:43:57 +0900 | [diff] [blame] | 23 | |
| 24 | import java.io.IOException; |
| 25 | import java.io.InputStream; |
Tomasz Mikolajewski | 5ed6983 | 2016-11-30 17:43:57 +0900 | [diff] [blame] | 26 | |
felkachang | 1556c36 | 2018-08-30 16:21:59 +0800 | [diff] [blame^] | 27 | import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; |
| 28 | import org.apache.commons.compress.archivers.zip.ZipFile; |
Tomasz Mikolajewski | 5ed6983 | 2016-11-30 17:43:57 +0900 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * Provides a backend for a seekable file descriptors for files in archives. |
| 32 | */ |
| 33 | public class Proxy extends ProxyFileDescriptorCallback { |
felkachang | 1556c36 | 2018-08-30 16:21:59 +0800 | [diff] [blame^] | 34 | private final ZipFile mFile; |
| 35 | private final ZipArchiveEntry mEntry; |
Tomasz Mikolajewski | 5ed6983 | 2016-11-30 17:43:57 +0900 | [diff] [blame] | 36 | private InputStream mInputStream = null; |
| 37 | private long mOffset = 0; |
| 38 | |
felkachang | 1556c36 | 2018-08-30 16:21:59 +0800 | [diff] [blame^] | 39 | Proxy(ZipFile file, ZipArchiveEntry entry) throws IOException { |
Tomasz Mikolajewski | 5ed6983 | 2016-11-30 17:43:57 +0900 | [diff] [blame] | 40 | mFile = file; |
| 41 | mEntry = entry; |
| 42 | recreateInputStream(); |
| 43 | } |
| 44 | |
| 45 | @Override |
| 46 | public long onGetSize() throws ErrnoException { |
| 47 | return mEntry.getSize(); |
| 48 | } |
| 49 | |
| 50 | @Override |
| 51 | public int onRead(long offset, int size, byte[] data) throws ErrnoException { |
| 52 | // TODO: Add a ring buffer to prevent expensive seeks. |
| 53 | if (offset < mOffset) { |
| 54 | try { |
| 55 | recreateInputStream(); |
| 56 | } catch (IOException e) { |
| 57 | throw new ErrnoException("onRead", OsConstants.EIO); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | while (mOffset < offset) { |
| 62 | try { |
| 63 | mOffset += mInputStream.skip(offset - mOffset); |
| 64 | } catch (IOException e) { |
| 65 | throw new ErrnoException("onRead", OsConstants.EIO); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | int remainingSize = size; |
| 70 | while (remainingSize > 0) { |
| 71 | try { |
| 72 | int bytes = mInputStream.read(data, size - remainingSize, remainingSize); |
| 73 | if (bytes <= 0) { |
| 74 | return size - remainingSize; |
| 75 | } |
| 76 | remainingSize -= bytes; |
| 77 | mOffset += bytes; |
| 78 | } catch (IOException e) { |
| 79 | throw new ErrnoException("onRead", OsConstants.EIO); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | return size - remainingSize; |
felkachang | 1556c36 | 2018-08-30 16:21:59 +0800 | [diff] [blame^] | 84 | } |
Tomasz Mikolajewski | 5ed6983 | 2016-11-30 17:43:57 +0900 | [diff] [blame] | 85 | |
| 86 | @Override public void onRelease() { |
Jeff Sharkey | 94785ef | 2018-07-09 16:37:41 -0600 | [diff] [blame] | 87 | FileUtils.closeQuietly(mInputStream); |
Tomasz Mikolajewski | 5ed6983 | 2016-11-30 17:43:57 +0900 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | private void recreateInputStream() throws IOException { |
Jeff Sharkey | 94785ef | 2018-07-09 16:37:41 -0600 | [diff] [blame] | 91 | FileUtils.closeQuietly(mInputStream); |
Tomasz Mikolajewski | 5ed6983 | 2016-11-30 17:43:57 +0900 | [diff] [blame] | 92 | mInputStream = mFile.getInputStream(mEntry); |
| 93 | mOffset = 0; |
| 94 | } |
| 95 | } |