Fix warnings in FileTransfer.java

This commit is contained in:
Andrew Grieve 2012-10-02 10:40:37 -04:00
parent d1ab1b59be
commit ff25be8839

View File

@ -18,8 +18,8 @@
*/ */
package org.apache.cordova; package org.apache.cordova;
import java.io.ByteArrayOutputStream;
import java.io.Closeable; import java.io.Closeable;
import java.io.DataInputStream;
import java.io.DataOutputStream; import java.io.DataOutputStream;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
@ -29,6 +29,7 @@ import java.io.FilterInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
@ -137,7 +138,12 @@ public class FileTransfer extends CordovaPlugin {
String target = args.getString(1); String target = args.getString(1);
if (action.equals("upload")) { if (action.equals("upload")) {
upload(URLDecoder.decode(source), target, args, callbackContext); try {
source = URLDecoder.decode(source, "UTF-8");
upload(source, target, args, callbackContext);
} catch (UnsupportedEncodingException e) {
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.MALFORMED_URL_EXCEPTION, "UTF-8 error."));
}
} else { } else {
download(source, target, args, callbackContext); download(source, target, args, callbackContext);
} }
@ -265,7 +271,7 @@ public class FileTransfer extends CordovaPlugin {
// Handle the other headers // Handle the other headers
if (headers != null) { if (headers != null) {
try { try {
for (Iterator iter = headers.keys(); iter.hasNext(); ) { for (Iterator<?> iter = headers.keys(); iter.hasNext(); ) {
String headerKey = iter.next().toString(); String headerKey = iter.next().toString();
JSONArray headerValues = headers.optJSONArray(headerKey); JSONArray headerValues = headers.optJSONArray(headerKey);
if (headerValues == null) { if (headerValues == null) {
@ -288,7 +294,7 @@ public class FileTransfer extends CordovaPlugin {
*/ */
String extraParams = ""; String extraParams = "";
try { try {
for (Iterator iter = params.keys(); iter.hasNext();) { for (Iterator<?> iter = params.keys(); iter.hasNext();) {
Object key = iter.next(); Object key = iter.next();
if(!String.valueOf(key).equals("headers")) if(!String.valueOf(key).equals("headers"))
{ {
@ -392,33 +398,37 @@ public class FileTransfer extends CordovaPlugin {
context.currentOutputStream = null; context.currentOutputStream = null;
//------------------ read the SERVER RESPONSE //------------------ read the SERVER RESPONSE
StringBuffer responseString = new StringBuffer(""); String responseString;
int responseCode = conn.getResponseCode();
DataInputStream inStream = null; InputStream inStream = null;
try { try {
synchronized (context) { synchronized (context) {
if (context.aborted) { if (context.aborted) {
throw new IOException("Request aborted"); throw new IOException("Request aborted");
} }
inStream = new DataInputStream(getInputStream(conn)); inStream = getInputStream(conn);
context.currentInputStream = inStream; context.currentInputStream = inStream;
} }
String line; ByteArrayOutputStream out = new ByteArrayOutputStream();
while (( line = inStream.readLine()) != null) { byte[] buffer = new byte[1024];
responseString.append(line); int bytesRead = 0;
// write bytes to file
while ((bytesRead = inStream.read(buffer)) > 0) {
out.write(buffer, 0, bytesRead);
} }
responseString = out.toString("UTF-8");
} finally { } finally {
safeClose(inStream); safeClose(inStream);
} }
Log.d(LOG_TAG, "got response from server"); Log.d(LOG_TAG, "got response from server");
Log.d(LOG_TAG, responseString.toString()); Log.d(LOG_TAG, responseString.substring(0, Math.min(256, responseString.length())));
// send request and retrieve response // send request and retrieve response
result.setResponseCode(conn.getResponseCode()); result.setResponseCode(responseCode);
result.setResponse(responseString.toString()); result.setResponse(responseString);
context.currentInputStream = null; context.currentInputStream = null;
synchronized (activeRequests) { synchronized (activeRequests) {
activeRequests.remove(objectId); activeRequests.remove(objectId);