|
| 1 | +/* |
| 2 | + * Nextcloud Android Library |
| 3 | + * |
| 4 | + * SPDX-FileCopyrightText: 2018-2024 Nextcloud GmbH and Nextcloud contributors |
| 5 | + * SPDX-FileCopyrightText: 2018-2019 Tobias Kaminsky <tobias@kaminsky.me> |
| 6 | + * SPDX-FileCopyrightText: 2014-2015 ownCloud Inc. |
| 7 | + * SPDX-FileCopyrightText: 2014 David A. Velasco <dvelasco@solidgear.es> |
| 8 | + * SPDX-FileCopyrightText: 2012 Bartosz Przybylski <bart.p.pl@gmail.com> |
| 9 | + * SPDX-License-Identifier: MIT |
| 10 | + */ |
| 11 | +package com.owncloud.android.lib.common.network; |
| 12 | + |
| 13 | +import org.apache.commons.httpclient.methods.RequestEntity; |
| 14 | + |
| 15 | +import java.io.File; |
| 16 | +import java.io.FileNotFoundException; |
| 17 | +import java.io.IOException; |
| 18 | +import java.io.OutputStream; |
| 19 | +import java.nio.ByteBuffer; |
| 20 | +import java.nio.channels.FileChannel; |
| 21 | +import java.util.Collection; |
| 22 | +import java.util.HashSet; |
| 23 | +import java.util.Iterator; |
| 24 | +import java.util.Set; |
| 25 | + |
| 26 | + |
| 27 | +/** |
| 28 | + * A RequestEntity that represents a PIECE of a file. |
| 29 | + * |
| 30 | + * @author David A. Velasco |
| 31 | + */ |
| 32 | +public class ChunkFromFileChannelRequestEntity implements RequestEntity, ProgressiveDataTransfer { |
| 33 | + private final FileChannel mChannel; |
| 34 | + private final String mContentType; |
| 35 | + private final long length; |
| 36 | + private final File mFile; |
| 37 | + private long mOffset; |
| 38 | + private long mTransferred; |
| 39 | + private final Set<OnDatatransferProgressListener> mDataTransferListeners = new HashSet<>(); |
| 40 | + private ByteBuffer mBuffer = ByteBuffer.allocate(4096); |
| 41 | + |
| 42 | + public ChunkFromFileChannelRequestEntity(final FileChannel channel, final String contentType, long offset, |
| 43 | + long chunkSize, final File file) { |
| 44 | + super(); |
| 45 | + if (channel == null) { |
| 46 | + throw new IllegalArgumentException("File may not be null"); |
| 47 | + } |
| 48 | + if (chunkSize <= 0) { |
| 49 | + throw new IllegalArgumentException("Chunk length must be greater than zero"); |
| 50 | + } |
| 51 | + mChannel = channel; |
| 52 | + mContentType = contentType; |
| 53 | + length = chunkSize; |
| 54 | + mFile = file; |
| 55 | + mOffset = offset; |
| 56 | + mTransferred = offset; |
| 57 | + } |
| 58 | + |
| 59 | + public long getContentLength() { |
| 60 | + try { |
| 61 | + return Math.min(length, mChannel.size() - mOffset); |
| 62 | + } catch (IOException e) { |
| 63 | + return length; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + public String getContentType() { |
| 68 | + return mContentType; |
| 69 | + } |
| 70 | + |
| 71 | + public boolean isRepeatable() { |
| 72 | + return true; |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public void addDataTransferProgressListener(OnDatatransferProgressListener listener) { |
| 77 | + synchronized (mDataTransferListeners) { |
| 78 | + mDataTransferListeners.add(listener); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public void addDataTransferProgressListeners(Collection<OnDatatransferProgressListener> listeners) { |
| 84 | + synchronized (mDataTransferListeners) { |
| 85 | + mDataTransferListeners.addAll(listeners); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public void removeDataTransferProgressListener(OnDatatransferProgressListener listener) { |
| 91 | + synchronized (mDataTransferListeners) { |
| 92 | + mDataTransferListeners.remove(listener); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + public void writeRequest(final OutputStream out) throws IOException { |
| 97 | + int readCount; |
| 98 | + Iterator<OnDatatransferProgressListener> progressListenerIterator; |
| 99 | + |
| 100 | + try { |
| 101 | + mChannel.position(mOffset); |
| 102 | + long size = mFile.length(); |
| 103 | + if (size == 0) { |
| 104 | + size = -1; |
| 105 | + } |
| 106 | + long maxCount = Math.min(mOffset + length, mChannel.size()); |
| 107 | + while (mChannel.position() < maxCount) { |
| 108 | + readCount = mChannel.read(mBuffer); |
| 109 | + try { |
| 110 | + out.write(mBuffer.array(), 0, readCount); |
| 111 | + } catch (IOException io) { |
| 112 | + // work-around try catch to filter exception in writing |
| 113 | + throw new FileRequestEntity.WriteException(io); |
| 114 | + } |
| 115 | + mBuffer.clear(); |
| 116 | + if (mTransferred < maxCount) { // condition to avoid accumulate progress for repeated chunks |
| 117 | + mTransferred += readCount; |
| 118 | + } |
| 119 | + synchronized (mDataTransferListeners) { |
| 120 | + progressListenerIterator = mDataTransferListeners.iterator(); |
| 121 | + |
| 122 | + while (progressListenerIterator.hasNext()) { |
| 123 | + progressListenerIterator.next().onTransferProgress(readCount, mTransferred, size, |
| 124 | + mFile.getAbsolutePath()); |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + } catch (IOException io) { |
| 130 | + // any read problem will be handled as if the file is not there |
| 131 | + if (io instanceof FileNotFoundException) { |
| 132 | + throw io; |
| 133 | + } else { |
| 134 | + FileNotFoundException fnf = new FileNotFoundException("Exception reading source file"); |
| 135 | + fnf.initCause(io); |
| 136 | + throw fnf; |
| 137 | + } |
| 138 | + |
| 139 | + } catch (FileRequestEntity.WriteException we) { |
| 140 | + throw we.getWrapped(); |
| 141 | + } |
| 142 | + |
| 143 | + } |
| 144 | + |
| 145 | +} |
0 commit comments