Skip to content

Commit

Permalink
changed a bit of tifile append(TiFileProxy)
Browse files Browse the repository at this point in the history
  • Loading branch information
fmerzadyan committed Oct 25, 2016
1 parent 380eb42 commit 942370a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,21 @@
*/
package org.appcelerator.titanium;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import android.content.Context;
import android.content.ContextWrapper;
import org.appcelerator.kroll.KrollProxy;
import org.appcelerator.kroll.annotations.Kroll;
import org.appcelerator.kroll.common.Log;
import org.appcelerator.titanium.io.TiBaseFile;
import org.appcelerator.titanium.io.TiFile;
import org.appcelerator.titanium.io.TiFileFactory;
import org.appcelerator.titanium.util.TiConvert;
import org.appcelerator.titanium.util.TiFileHelper2;
Expand Down Expand Up @@ -137,9 +141,7 @@ public boolean getWritable()
public boolean append(Object data)
{
try {
if (data != null) {
return tbf.append(data);
}
return ((TiFile) tbf).append(data);
} catch (IOException e) {
Log.e(TAG, "append failed: ", e);
}
Expand Down
17 changes: 11 additions & 6 deletions android/titanium/src/java/org/appcelerator/titanium/io/TiFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import android.net.Uri;
import android.os.StatFs;
import org.appcelerator.titanium.TiFileProxy;

/**
* An extension of {@link TiBaseFile}, used for representing a file on the device's true file system.
Expand Down Expand Up @@ -101,8 +102,8 @@ public boolean append(Object data) throws IOException
{
if (data instanceof String) {
return append((String) data);
} else if (data instanceof File) {
return append((File) data);
} else if (data instanceof TiFileProxy) {
return append((TiFileProxy) data);
} else if (data instanceof TiBlob) {
return append((TiBlob) data);
}
Expand All @@ -123,15 +124,19 @@ private boolean append(String data) throws IOException
}
}

private boolean append(File data) throws IOException
private boolean append(TiFileProxy data) throws IOException, NullPointerException
{
FileInputStream inputStream = null;
FileOutputStream outputStream = null;
try {
inputStream = new FileInputStream(file);
byte[] body = new byte[(int) file.length()];
outputStream = new FileOutputStream(file, true);
outputStream.write(inputStream.read(body));
File appendee = new File(((TiFile) data.getBaseFile()).getFile().getPath());
inputStream = new FileInputStream(appendee);
byte[] bytes = new byte[(int) appendee.length()];
// method nesting doesn't let inputStream.read bytes into outputStream
// best to keep on separate line
inputStream.read(bytes);
outputStream.write(bytes);
return true;
} finally {
if (inputStream != null) {
Expand Down

0 comments on commit 942370a

Please sign in to comment.