FileRequestEntity.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* ownCloud Android client application
  2. * Copyright (C) 2012 Bartek Przybylski
  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 as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  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 eu.alefzero.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.network.ProgressiveDataTransferer;
  31. import eu.alefzero.webdav.OnDatatransferProgressListener;
  32. import android.util.Log;
  33. /**
  34. * A RequestEntity that represents a File.
  35. *
  36. */
  37. public class FileRequestEntity implements RequestEntity, ProgressiveDataTransferer {
  38. final File mFile;
  39. final String mContentType;
  40. Set<OnDatatransferProgressListener> mDataTransferListeners = new HashSet<OnDatatransferProgressListener>();
  41. public FileRequestEntity(final File file, final String contentType) {
  42. super();
  43. this.mFile = file;
  44. this.mContentType = contentType;
  45. if (file == null) {
  46. throw new IllegalArgumentException("File may not be null");
  47. }
  48. }
  49. @Override
  50. public long getContentLength() {
  51. return mFile.length();
  52. }
  53. @Override
  54. public String getContentType() {
  55. return mContentType;
  56. }
  57. @Override
  58. public boolean isRepeatable() {
  59. return true;
  60. }
  61. @Override
  62. public void addDatatransferProgressListener(OnDatatransferProgressListener listener) {
  63. synchronized (mDataTransferListeners) {
  64. mDataTransferListeners.add(listener);
  65. }
  66. }
  67. @Override
  68. public void addDatatransferProgressListeners(Collection<OnDatatransferProgressListener> listeners) {
  69. synchronized (mDataTransferListeners) {
  70. mDataTransferListeners.addAll(listeners);
  71. }
  72. }
  73. @Override
  74. public void removeDatatransferProgressListener(OnDatatransferProgressListener listener) {
  75. synchronized (mDataTransferListeners) {
  76. mDataTransferListeners.remove(listener);
  77. }
  78. }
  79. @Override
  80. public void writeRequest(final OutputStream out) throws IOException {
  81. //byte[] tmp = new byte[4096];
  82. ByteBuffer tmp = ByteBuffer.allocate(4096);
  83. int readResult = 0;
  84. // TODO(bprzybylski): each mem allocation can throw OutOfMemoryError we need to handle it
  85. // globally in some fashionable manner
  86. RandomAccessFile raf = new RandomAccessFile(mFile, "r");
  87. FileChannel channel = raf.getChannel();
  88. Iterator<OnDatatransferProgressListener> it = null;
  89. long transferred = 0;
  90. long size = mFile.length();
  91. if (size == 0) size = -1;
  92. try {
  93. while ((readResult = channel.read(tmp)) >= 0) {
  94. out.write(tmp.array(), 0, readResult);
  95. tmp.clear();
  96. transferred += readResult;
  97. synchronized (mDataTransferListeners) {
  98. it = mDataTransferListeners.iterator();
  99. while (it.hasNext()) {
  100. it.next().onTransferProgress(readResult, transferred, size, mFile.getName());
  101. }
  102. }
  103. }
  104. } catch (IOException io) {
  105. Log.e("FileRequestException", io.getMessage());
  106. throw new RuntimeException("Ugly solution to workaround the default policy of retries when the server falls while uploading ; temporal fix; really", io);
  107. } finally {
  108. channel.close();
  109. raf.close();
  110. }
  111. }
  112. }