diff options
author | Alex Clayton <alex.clayton@isode.com> | 2016-03-16 13:48:37 (GMT) |
---|---|---|
committer | Alex Clayton <alex.clayton@isode.com> | 2016-03-16 14:05:38 (GMT) |
commit | 8b9891afc85d114ff1e9c9a0291a4aaee8baeb09 (patch) | |
tree | 5027dc69d6ca785e0ed94aebc7232b3790b51464 /src/com/isode/stroke/filetransfer/FileWriteBytestream.java | |
parent | 892af8539f2b46e840d7344489529259d1df03b9 (diff) | |
download | stroke-8b9891afc85d114ff1e9c9a0291a4aaee8baeb09.zip stroke-8b9891afc85d114ff1e9c9a0291a4aaee8baeb09.tar.bz2 |
Add FileWriteBytestream class and test.
Adds a FileWriteBytestream class plus a test for it. These had been
missed out previously. Also as per patch 'Fix crash when saving a
received file to non-writable location' changed
WriteBytestream.write() method to return a boolean indicating success
or failure.
Test-information: Tests pass ok.
Change-Id: I0c3676db8b67573142e8628f439cecf54f3f8f1a
Diffstat (limited to 'src/com/isode/stroke/filetransfer/FileWriteBytestream.java')
-rw-r--r-- | src/com/isode/stroke/filetransfer/FileWriteBytestream.java | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/src/com/isode/stroke/filetransfer/FileWriteBytestream.java b/src/com/isode/stroke/filetransfer/FileWriteBytestream.java new file mode 100644 index 0000000..7fa068d --- /dev/null +++ b/src/com/isode/stroke/filetransfer/FileWriteBytestream.java @@ -0,0 +1,73 @@ +/* Copyright (c) 2016, Isode Limited, London, England. + * All rights reserved. + * + * Acquisition and use of this software and related materials for any + * purpose requires a written license agreement from Isode Limited, + * or a written license from an organisation licensed by Isode Limited + * to grant such a license. + * + */ +package com.isode.stroke.filetransfer; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; + +import com.isode.stroke.base.ByteArray; + +public class FileWriteBytestream extends WriteBytestream { + + private final String filePath_; + + private FileOutputStream stream_ = null; + + public FileWriteBytestream(String filePath) { + filePath_ = filePath; + } + + @Override + protected void finalize() throws Throwable { + try { + close(); + } + finally { + super.finalize(); + } + } + + @Override + public boolean write(ByteArray data) { + if (data.isEmpty()) { + return true; + } + if (stream_ == null) { + try { + stream_ = new FileOutputStream(filePath_); + } catch (FileNotFoundException e) { + return false; + } + } + try { + stream_.write(data.getData()); + stream_.flush(); + } catch (IOException e) { + return false; + } + onWrite.emit(data); + return true; + } + + public void close() { + if (stream_ != null) { + try { + stream_.close(); + } catch (IOException e) { + // Ignore exception + } + stream_ = null; + } + } + + +} |