1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
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.
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.
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/>.
19 package eu
.alefzero
.webdav
;
21 import java
.io
.IOException
;
22 import java
.io
.OutputStream
;
23 import java
.nio
.ByteBuffer
;
24 import java
.nio
.channels
.FileChannel
;
26 import org
.apache
.commons
.httpclient
.methods
.RequestEntity
;
28 import eu
.alefzero
.webdav
.OnDatatransferProgressListener
;
30 import android
.util
.Log
;
34 * A RequestEntity that represents a PIECE of a file.
36 * @author David A. Velasco
38 public class ChunkFromFileChannelRequestEntity
implements RequestEntity
{
40 private static final String TAG
= ChunkFromFileChannelRequestEntity
.class.getSimpleName();
42 //private final File mFile;
43 private final FileChannel mChannel
;
44 private final String mContentType
;
45 private final long mSize
;
47 private OnDatatransferProgressListener mListener
;
48 private ByteBuffer mBuffer
= ByteBuffer
.allocate(4096);
50 public ChunkFromFileChannelRequestEntity(final FileChannel channel
, final String contentType
, long size
) {
52 if (channel
== null
) {
53 throw new IllegalArgumentException("File may not be null");
56 throw new IllegalArgumentException("Size must be greater than zero");
59 mContentType
= contentType
;
64 public void setOffset(long offset
) {
68 public long getContentLength() {
70 return Math
.min(mSize
, mChannel
.size() - mChannel
.position());
71 } catch (IOException e
) {
76 public String
getContentType() {
80 public boolean isRepeatable() {
84 public void setOnDatatransferProgressListener(OnDatatransferProgressListener listener
) {
88 public void writeRequest(final OutputStream out
) throws IOException
{
92 //while ((i = instream.read(tmp)) >= 0) {
93 mChannel
.position(mOffset
);
94 while (mChannel
.position() < mOffset
+ mSize
) {
95 readCount
= mChannel
.read(mBuffer
);
96 out
.write(mBuffer
.array(), 0, readCount
);
98 if (mListener
!= null
)
99 mListener
.transferProgress(readCount
);
102 } catch (IOException io
) {
103 Log
.e(TAG
, io
.getMessage());
104 throw new RuntimeException("Ugly solution to workaround the default policy of retries when the server falls while uploading ; temporal fix; really", io
);