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 com
.owncloud
.android
.operations
;
22 import java
.io
.IOException
;
23 import java
.io
.RandomAccessFile
;
24 import java
.nio
.channels
.FileChannel
;
25 import java
.nio
.channels
.FileLock
;
26 import java
.util
.Random
;
28 import org
.apache
.commons
.httpclient
.HttpException
;
29 import org
.apache
.commons
.httpclient
.methods
.PutMethod
;
31 import android
.util
.Log
;
33 import eu
.alefzero
.webdav
.ChunkFromFileChannelRequestEntity
;
34 import eu
.alefzero
.webdav
.OnDatatransferProgressListener
;
35 import eu
.alefzero
.webdav
.WebdavClient
;
36 import eu
.alefzero
.webdav
.WebdavUtils
;
38 public class ChunkedUploadFileOperation
extends UploadFileOperation
{
40 private static final long CHUNK_SIZE
= 102400;
41 private static final String OC_CHUNKED_HEADER
= "OC-Chunked";
42 private static final String TAG
= ChunkedUploadFileOperation
.class.getSimpleName();
44 public ChunkedUploadFileOperation( String localPath
,
48 boolean forceOverwrite
,
49 OnDatatransferProgressListener dataTransferProgressListener
) {
51 super(localPath
, remotePath
, mimeType
, isInstant
, forceOverwrite
, dataTransferProgressListener
);
55 protected int uploadFile(WebdavClient client
) throws HttpException
, IOException
{
59 FileChannel channel
= null
;
62 File file
= new File(getLocalPath());
63 channel
= new RandomAccessFile(file
, "rw").getChannel();
64 lock
= channel
.tryLock();
65 ChunkFromFileChannelRequestEntity entity
= new ChunkFromFileChannelRequestEntity(channel
, getMimeType(), CHUNK_SIZE
);
66 entity
.setOnDatatransferProgressListener(getDataTransferListener());
68 String uriPrefix
= client
.getBaseUri() + WebdavUtils
.encodePath(getRemotePath()) + "-chunking-" + Math
.abs((new Random()).nextInt(9000)+1000) + "-" ;
69 long chunkCount
= (long) Math
.ceil((double)file
.length() / CHUNK_SIZE
);
70 for (int chunkIndex
= 0; chunkIndex
< chunkCount
; chunkIndex
++, offset
+= CHUNK_SIZE
) {
71 put
= new PutMethod(uriPrefix
+ chunkCount
+ "-" + chunkIndex
);
72 put
.addRequestHeader(OC_CHUNKED_HEADER
, OC_CHUNKED_HEADER
);
73 entity
.setOffset(offset
);
74 put
.setRequestEntity(entity
);
75 status
= client
.executeMethod(put
);
76 client
.exhaustResponse(put
.getResponseBodyAsStream());
77 Log
.d(TAG
, "Upload of " + getLocalPath() + " to " + getRemotePath() + ", chunk index " + chunkIndex
+ ", count " + chunkCount
+ ", HTTP result status " + status
);
78 if (!isSuccess(status
))
88 put
.releaseConnection(); // let the connection available for other methods