FileRequestEntity.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /* ownCloud Android client application
  2. * Copyright (C) 2012 Bartek Przybylski
  3. * Copyright (C) 2012-2013 ownCloud Inc.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2,
  7. * as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. */
  18. package com.owncloud.android.network.webdav;
  19. import java.io.File;
  20. import java.io.IOException;
  21. import java.io.OutputStream;
  22. import java.io.RandomAccessFile;
  23. import java.nio.ByteBuffer;
  24. import java.nio.channels.FileChannel;
  25. import java.util.Collection;
  26. import java.util.HashSet;
  27. import java.util.Iterator;
  28. import java.util.Set;
  29. import org.apache.commons.httpclient.methods.RequestEntity;
  30. import com.owncloud.android.Log_OC;
  31. import com.owncloud.android.network.ProgressiveDataTransferer;
  32. /**
  33. * A RequestEntity that represents a File.
  34. *
  35. */
  36. public class FileRequestEntity implements RequestEntity, ProgressiveDataTransferer {
  37. final File mFile;
  38. final String mContentType;
  39. Set<OnDatatransferProgressListener> mDataTransferListeners = new HashSet<OnDatatransferProgressListener>();
  40. public FileRequestEntity(final File file, final String contentType) {
  41. super();
  42. this.mFile = file;
  43. this.mContentType = contentType;
  44. if (file == null) {
  45. throw new IllegalArgumentException("File may not be null");
  46. }
  47. }
  48. @Override
  49. public long getContentLength() {
  50. return mFile.length();
  51. }
  52. @Override
  53. public String getContentType() {
  54. return mContentType;
  55. }
  56. @Override
  57. public boolean isRepeatable() {
  58. return true;
  59. }
  60. @Override
  61. public void addDatatransferProgressListener(OnDatatransferProgressListener listener) {
  62. synchronized (mDataTransferListeners) {
  63. mDataTransferListeners.add(listener);
  64. }
  65. }
  66. @Override
  67. public void addDatatransferProgressListeners(Collection<OnDatatransferProgressListener> listeners) {
  68. synchronized (mDataTransferListeners) {
  69. mDataTransferListeners.addAll(listeners);
  70. }
  71. }
  72. @Override
  73. public void removeDatatransferProgressListener(OnDatatransferProgressListener listener) {
  74. synchronized (mDataTransferListeners) {
  75. mDataTransferListeners.remove(listener);
  76. }
  77. }
  78. @Override
  79. public void writeRequest(final OutputStream out) throws IOException {
  80. //byte[] tmp = new byte[4096];
  81. ByteBuffer tmp = ByteBuffer.allocate(4096);
  82. int readResult = 0;
  83. // TODO(bprzybylski): each mem allocation can throw OutOfMemoryError we need to handle it
  84. // globally in some fashionable manner
  85. RandomAccessFile raf = new RandomAccessFile(mFile, "r");
  86. FileChannel channel = raf.getChannel();
  87. Iterator<OnDatatransferProgressListener> it = null;
  88. long transferred = 0;
  89. long size = mFile.length();
  90. if (size == 0) size = -1;
  91. try {
  92. while ((readResult = channel.read(tmp)) >= 0) {
  93. out.write(tmp.array(), 0, readResult);
  94. tmp.clear();
  95. transferred += readResult;
  96. synchronized (mDataTransferListeners) {
  97. it = mDataTransferListeners.iterator();
  98. while (it.hasNext()) {
  99. it.next().onTransferProgress(readResult, transferred, size, mFile.getName());
  100. }
  101. }
  102. }
  103. } catch (IOException io) {
  104. Log_OC.e("FileRequestException", io.getMessage());
  105. throw new RuntimeException("Ugly solution to workaround the default policy of retries when the server falls while uploading ; temporal fix; really", io);
  106. } finally {
  107. channel.close();
  108. raf.close();
  109. }
  110. }
  111. }