This commit is contained in:
Braden Shepherdson 2012-09-21 13:38:42 -04:00
commit dd1cd46719

View File

@ -24,6 +24,7 @@ import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.FilterInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
@ -71,13 +72,46 @@ public class FileTransfer extends Plugin {
private SSLSocketFactory defaultSSLSocketFactory = null; private SSLSocketFactory defaultSSLSocketFactory = null;
private HostnameVerifier defaultHostnameVerifier = null; private HostnameVerifier defaultHostnameVerifier = null;
private static class AbortException extends Exception { private static final class AbortException extends Exception {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
public AbortException(String str) { public AbortException(String str) {
super(str); super(str);
} }
} }
/**
* Works around a bug on Android 2.3.
* http://code.google.com/p/android/issues/detail?id=14562
*/
private static final class DoneHandlerInputStream extends FilterInputStream {
private boolean done;
public DoneHandlerInputStream(InputStream stream) {
super(stream);
}
@Override
public int read() throws IOException {
int result = done ? -1 : super.read();
done = (result == -1);
return result;
}
@Override
public int read(byte[] buffer) throws IOException {
int result = done ? -1 : super.read(buffer);
done = (result == -1);
return result;
}
@Override
public int read(byte[] bytes, int offset, int count) throws IOException {
int result = done ? -1 : super.read(bytes, offset, count);
done = (result == -1);
return result;
}
}
/* (non-Javadoc) /* (non-Javadoc)
* @see org.apache.cordova.api.Plugin#execute(java.lang.String, org.json.JSONArray, java.lang.String) * @see org.apache.cordova.api.Plugin#execute(java.lang.String, org.json.JSONArray, java.lang.String)
*/ */
@ -330,13 +364,7 @@ public class FileTransfer extends Plugin {
//------------------ read the SERVER RESPONSE //------------------ read the SERVER RESPONSE
StringBuffer responseString = new StringBuffer(""); StringBuffer responseString = new StringBuffer("");
DataInputStream inStream; DataInputStream inStream = new DataInputStream(getInputStream(conn));
try {
inStream = new DataInputStream ( conn.getInputStream() );
} catch(FileNotFoundException e) {
Log.e(LOG_TAG, e.toString(), e);
throw new IOException("Received error from server");
}
String line; String line;
while (( line = inStream.readLine()) != null) { while (( line = inStream.readLine()) != null) {
@ -390,6 +418,13 @@ public class FileTransfer extends Plugin {
} }
} }
private InputStream getInputStream(HttpURLConnection conn) throws IOException {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
return new DoneHandlerInputStream(conn.getInputStream());
}
return conn.getInputStream();
}
// always verify the host - don't check for certificate // always verify the host - don't check for certificate
final static HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier() { final static HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) { public boolean verify(String hostname, SSLSession session) {
@ -551,13 +586,7 @@ public class FileTransfer extends Plugin {
connection.connect(); connection.connect();
Log.d(LOG_TAG, "Download file:" + url); Log.d(LOG_TAG, "Download file:" + url);
InputStream inputStream; InputStream inputStream = getInputStream(connection);
try {
inputStream = connection.getInputStream();
} catch(FileNotFoundException e) {
Log.e(LOG_TAG, e.toString(), e);
throw new IOException("Received error from server");
}
byte[] buffer = new byte[1024]; byte[] buffer = new byte[1024];
int bytesRead = 0; int bytesRead = 0;