remove all warning from project
[pub/Android/ownCloud.git] / src / com / owncloud / android / operations / ChunkedUploadFileOperation.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 com.owncloud.android.operations;
20
21 import java.io.File;
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;
27
28 import org.apache.commons.httpclient.HttpException;
29 import org.apache.commons.httpclient.methods.PutMethod;
30
31 import android.util.Log;
32
33 import eu.alefzero.webdav.ChunkFromFileChannelRequestEntity;
34 import eu.alefzero.webdav.OnDatatransferProgressListener;
35 import eu.alefzero.webdav.WebdavClient;
36 import eu.alefzero.webdav.WebdavUtils;
37
38 public class ChunkedUploadFileOperation extends UploadFileOperation {
39
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();
43
44 public ChunkedUploadFileOperation( String localPath,
45 String remotePath,
46 String mimeType,
47 boolean isInstant,
48 boolean forceOverwrite,
49 OnDatatransferProgressListener dataTransferProgressListener) {
50
51 super(localPath, remotePath, mimeType, isInstant, forceOverwrite, dataTransferProgressListener);
52 }
53
54 @Override
55 protected int uploadFile(WebdavClient client) throws HttpException, IOException {
56 int status = -1;
57
58 PutMethod put = null;
59 FileChannel channel = null;
60 FileLock lock = null;
61 RandomAccessFile raf = null;
62 try {
63 File file = new File(getLocalPath());
64 raf = new RandomAccessFile(file, "rw");
65 channel = raf.getChannel();
66 lock = channel.tryLock();
67 ChunkFromFileChannelRequestEntity entity = new ChunkFromFileChannelRequestEntity(channel, getMimeType(), CHUNK_SIZE);
68 entity.setOnDatatransferProgressListener(getDataTransferListener());
69 long offset = 0;
70 String uriPrefix = client.getBaseUri() + WebdavUtils.encodePath(getRemotePath()) + "-chunking-" + Math.abs((new Random()).nextInt(9000)+1000) + "-" ;
71 long chunkCount = (long) Math.ceil((double)file.length() / CHUNK_SIZE);
72 for (int chunkIndex = 0; chunkIndex < chunkCount ; chunkIndex++, offset += CHUNK_SIZE) {
73 put = new PutMethod(uriPrefix + chunkCount + "-" + chunkIndex);
74 put.addRequestHeader(OC_CHUNKED_HEADER, OC_CHUNKED_HEADER);
75 entity.setOffset(offset);
76 put.setRequestEntity(entity);
77 status = client.executeMethod(put);
78 client.exhaustResponse(put.getResponseBodyAsStream());
79 Log.d(TAG, "Upload of " + getLocalPath() + " to " + getRemotePath() + ", chunk index " + chunkIndex + ", count " + chunkCount + ", HTTP result status " + status);
80 if (!isSuccess(status))
81 break;
82 }
83
84 } finally {
85 if (lock != null)
86 lock.release();
87 if (channel != null)
88 channel.close();
89 if (raf != null)
90 raf.close();
91 if (put != null)
92 put.releaseConnection(); // let the connection available for other methods
93 }
94 return status;
95 }
96
97 }