c3c9a69cf2155d65b8211a92ca02c34b0bc0f99c
[pub/Android/ownCloud.git] / src / eu / alefzero / webdav / ChunkFromFileChannelRequestEntity.java
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
19 package eu.alefzero.webdav;
20
21 import java.io.IOException;
22 import java.io.OutputStream;
23 import java.nio.ByteBuffer;
24 import java.nio.channels.FileChannel;
25
26 import org.apache.commons.httpclient.methods.RequestEntity;
27
28 import eu.alefzero.webdav.OnDatatransferProgressListener;
29
30 import android.util.Log;
31
32
33 /**
34 * A RequestEntity that represents a PIECE of a file.
35 *
36 * @author David A. Velasco
37 */
38 public class ChunkFromFileChannelRequestEntity implements RequestEntity {
39
40 private static final String TAG = ChunkFromFileChannelRequestEntity.class.getSimpleName();
41
42 //private final File mFile;
43 private final FileChannel mChannel;
44 private final String mContentType;
45 private final long mSize;
46 private long mOffset;
47 private OnDatatransferProgressListener mListener;
48 private ByteBuffer mBuffer = ByteBuffer.allocate(4096);
49
50 public ChunkFromFileChannelRequestEntity(final FileChannel channel, final String contentType, long size) {
51 super();
52 if (channel == null) {
53 throw new IllegalArgumentException("File may not be null");
54 }
55 if (size <= 0) {
56 throw new IllegalArgumentException("Size must be greater than zero");
57 }
58 mChannel = channel;
59 mContentType = contentType;
60 mSize = size;
61 mOffset = 0;
62 }
63
64 public void setOffset(long offset) {
65 mOffset = offset;
66 }
67
68 public long getContentLength() {
69 try {
70 return Math.min(mSize, mChannel.size() - mChannel.position());
71 } catch (IOException e) {
72 return mSize;
73 }
74 }
75
76 public String getContentType() {
77 return mContentType;
78 }
79
80 public boolean isRepeatable() {
81 return true;
82 }
83
84 public void setOnDatatransferProgressListener(OnDatatransferProgressListener listener) {
85 mListener = listener;
86 }
87
88 public void writeRequest(final OutputStream out) throws IOException {
89 int readCount = 0;
90
91 try {
92 mChannel.position(mOffset);
93 while (mChannel.position() < mOffset + mSize && mChannel.position() < mChannel.size()) {
94 readCount = mChannel.read(mBuffer);
95 out.write(mBuffer.array(), 0, readCount);
96 mBuffer.clear();
97 if (mListener != null)
98 mListener.transferProgress(readCount);
99 }
100
101 } catch (IOException io) {
102 Log.e(TAG, io.getMessage());
103 throw new RuntimeException("Ugly solution to workaround the default policy of retries when the server falls while uploading ; temporal fix; really", io);
104
105 }
106 }
107
108 }