X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/be22e58b398f6bc6e47ecd1695ababa353e2ba3b..76ba00b2b23f5c2079a6635dc5e661850c3c5dfb:/src/eu/alefzero/webdav/FileRequestEntity.java diff --git a/src/eu/alefzero/webdav/FileRequestEntity.java b/src/eu/alefzero/webdav/FileRequestEntity.java index f8ba3204..df6bdff4 100644 --- a/src/eu/alefzero/webdav/FileRequestEntity.java +++ b/src/eu/alefzero/webdav/FileRequestEntity.java @@ -1,15 +1,12 @@ package eu.alefzero.webdav; import java.io.File; -import java.io.FileInputStream; import java.io.IOException; -import java.io.InputStream; import java.io.OutputStream; import java.io.RandomAccessFile; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import java.nio.channels.FileLock; -import java.nio.channels.OverlappingFileLockException; import org.apache.commons.httpclient.methods.RequestEntity; @@ -24,41 +21,48 @@ import android.util.Log; */ public class FileRequestEntity implements RequestEntity { - final File file; - final String contentType; - OnDatatransferProgressListener listener; + final File mFile; + final String mContentType; + OnDatatransferProgressListener mListener; public FileRequestEntity(final File file, final String contentType) { super(); + this.mFile = file; + this.mContentType = contentType; if (file == null) { throw new IllegalArgumentException("File may not be null"); } - this.file = file; - this.contentType = contentType; } + @Override public long getContentLength() { - return this.file.length(); + return mFile.length(); } + @Override public String getContentType() { - return this.contentType; + return mContentType; } + @Override public boolean isRepeatable() { return true; } public void setOnDatatransferProgressListener(OnDatatransferProgressListener listener) { - this.listener = listener; + mListener = listener; } - + + @Override public void writeRequest(final OutputStream out) throws IOException { //byte[] tmp = new byte[4096]; ByteBuffer tmp = ByteBuffer.allocate(4096); int i = 0; - FileChannel channel = new RandomAccessFile(this.file, "rw").getChannel(); + // TODO(bprzybylski): each mem allocation can throw OutOfMemoryError we need to handle it + // globally in some fashionable manner + RandomAccessFile raf = new RandomAccessFile(mFile, "rw"); + FileChannel channel = raf.getChannel(); FileLock lock = channel.tryLock(); //InputStream instream = new FileInputStream(this.file); @@ -67,8 +71,8 @@ public class FileRequestEntity implements RequestEntity { while ((i = channel.read(tmp)) >= 0) { out.write(tmp.array(), 0, i); tmp.clear(); - if (listener != null) - listener.transferProgress(i); + if (mListener != null) + mListener.transferProgress(i); } } catch (IOException io) { Log.e("FileRequestException", io.getMessage()); @@ -78,6 +82,7 @@ public class FileRequestEntity implements RequestEntity { //instream.close(); lock.release(); channel.close(); + raf.close(); } }