ChunkFromFileChannelRequestEntity.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /* ownCloud Android client application
  2. * Copyright (C) 2012-2013 ownCloud Inc.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2,
  6. * as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. */
  17. package eu.alefzero.webdav;
  18. import java.io.File;
  19. import java.io.IOException;
  20. import java.io.OutputStream;
  21. import java.nio.ByteBuffer;
  22. import java.nio.channels.FileChannel;
  23. import java.util.Collection;
  24. import java.util.HashSet;
  25. import java.util.Iterator;
  26. import java.util.Set;
  27. import org.apache.commons.httpclient.methods.RequestEntity;
  28. import com.owncloud.android.Log_OC;
  29. import com.owncloud.android.network.ProgressiveDataTransferer;
  30. import eu.alefzero.webdav.OnDatatransferProgressListener;
  31. /**
  32. * A RequestEntity that represents a PIECE of a file.
  33. *
  34. * @author David A. Velasco
  35. */
  36. public class ChunkFromFileChannelRequestEntity implements RequestEntity, ProgressiveDataTransferer {
  37. private static final String TAG = ChunkFromFileChannelRequestEntity.class.getSimpleName();
  38. //private final File mFile;
  39. private final FileChannel mChannel;
  40. private final String mContentType;
  41. private final long mChunkSize;
  42. private final File mFile;
  43. private long mOffset;
  44. private long mTransferred;
  45. Set<OnDatatransferProgressListener> mDataTransferListeners = new HashSet<OnDatatransferProgressListener>();
  46. private ByteBuffer mBuffer = ByteBuffer.allocate(4096);
  47. public ChunkFromFileChannelRequestEntity(final FileChannel channel, final String contentType, long chunkSize, final File file) {
  48. super();
  49. if (channel == null) {
  50. throw new IllegalArgumentException("File may not be null");
  51. }
  52. if (chunkSize <= 0) {
  53. throw new IllegalArgumentException("Chunk size must be greater than zero");
  54. }
  55. mChannel = channel;
  56. mContentType = contentType;
  57. mChunkSize = chunkSize;
  58. mFile = file;
  59. mOffset = 0;
  60. mTransferred = 0;
  61. }
  62. public void setOffset(long offset) {
  63. mOffset = offset;
  64. }
  65. public long getContentLength() {
  66. try {
  67. return Math.min(mChunkSize, mChannel.size() - mChannel.position());
  68. } catch (IOException e) {
  69. return mChunkSize;
  70. }
  71. }
  72. public String getContentType() {
  73. return mContentType;
  74. }
  75. public boolean isRepeatable() {
  76. return true;
  77. }
  78. @Override
  79. public void addDatatransferProgressListener(OnDatatransferProgressListener listener) {
  80. synchronized (mDataTransferListeners) {
  81. mDataTransferListeners.add(listener);
  82. }
  83. }
  84. @Override
  85. public void addDatatransferProgressListeners(Collection<OnDatatransferProgressListener> listeners) {
  86. synchronized (mDataTransferListeners) {
  87. mDataTransferListeners.addAll(listeners);
  88. }
  89. }
  90. @Override
  91. public void removeDatatransferProgressListener(OnDatatransferProgressListener listener) {
  92. synchronized (mDataTransferListeners) {
  93. mDataTransferListeners.remove(listener);
  94. }
  95. }
  96. public void writeRequest(final OutputStream out) throws IOException {
  97. int readCount = 0;
  98. Iterator<OnDatatransferProgressListener> it = null;
  99. try {
  100. mChannel.position(mOffset);
  101. long size = mFile.length();
  102. if (size == 0) size = -1;
  103. long maxCount = Math.min(mOffset + mChunkSize, mChannel.size());
  104. while (mChannel.position() < maxCount) {
  105. readCount = mChannel.read(mBuffer);
  106. out.write(mBuffer.array(), 0, readCount);
  107. mBuffer.clear();
  108. if (mTransferred < maxCount) { // condition to avoid accumulate progress for repeated chunks
  109. mTransferred += readCount;
  110. }
  111. synchronized (mDataTransferListeners) {
  112. it = mDataTransferListeners.iterator();
  113. while (it.hasNext()) {
  114. it.next().onTransferProgress(readCount, mTransferred, size, mFile.getName());
  115. }
  116. }
  117. }
  118. } catch (IOException io) {
  119. Log_OC.e(TAG, io.getMessage());
  120. throw new RuntimeException("Ugly solution to workaround the default policy of retries when the server falls while uploading ; temporal fix; really", io);
  121. }
  122. }
  123. }