android-openrat

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit d9bd9c8c810efd3f5e2b1b4cd8373319f0ab5f7f
parent 74f4035a61471e3153f79b047505b69eb9db7ea1
Author: dankert <devnull@localhost>
Date:   Sat, 15 Oct 2011 01:48:22 +0200

Dateien immer als Byte-Array verarbeiten, nie als java.lang.String.

Diffstat:
src/de/openrat/android/blog/FolderActivity.java | 27++++++---------------------
src/de/openrat/android/blog/client/HTTPRequest.java | 48++++++++++++++++++++++++++++++++----------------
src/de/openrat/android/blog/util/FileUtils.java | 49+++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 87 insertions(+), 37 deletions(-)

diff --git a/src/de/openrat/android/blog/FolderActivity.java b/src/de/openrat/android/blog/FolderActivity.java @@ -4,12 +4,10 @@ package de.openrat.android.blog; import java.io.BufferedInputStream; -import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; -import java.io.FileReader; import java.io.IOException; -import java.io.PrintStream; +import java.io.InputStream; import java.util.ArrayList; import org.json.JSONArray; @@ -40,6 +38,7 @@ import android.widget.AdapterView.OnItemClickListener; import de.openrat.android.blog.FolderEntry.FType; import de.openrat.android.blog.adapter.FolderContentAdapter; import de.openrat.android.blog.client.CMSRequest; +import de.openrat.android.blog.util.FileUtils; /** * @author dankert @@ -48,7 +47,6 @@ import de.openrat.android.blog.client.CMSRequest; public class FolderActivity extends ListActivity { - private static final String BOUNDARY = "usadlkuuusdkcua43sfd"; private static final String ID2 = "id"; public static final String CLIENT = "client"; private static final String NAME = "name"; @@ -390,24 +388,11 @@ public class FolderActivity extends ListActivity try { final File file = new File(filePath); - BufferedInputStream br = new BufferedInputStream( - new FileInputStream(file)); - - StringBuffer fileContent = new StringBuffer(); - // create a byte array - byte[] contents = new byte[1024]; - - int bytesRead; - - while ((bytesRead = br.read(contents)) != -1) - { - fileContent.append(new String(contents, 0, bytesRead)); - // fileContent.append("Hallo "); - } - - // System.out.println("Body: \n\n\n\n\n"+body+"\n\n\n\n"+"Länge: "+body.length()+"\n\n"); - request.setFile("file", fileContent.toString().getBytes(), + byte[] fileBytes = FileUtils.getBytesFromFile(file); + request.setFile("file", fileBytes, file.getName(), "image/jpeg", "binary"); + // request.setFile("file", inputStream,file.length(), + // file.getName(), "image/jpeg", "binary"); String response = request.performRequest(); // String response = request.performRequest("TEST TEST"); diff --git a/src/de/openrat/android/blog/client/HTTPRequest.java b/src/de/openrat/android/blog/client/HTTPRequest.java @@ -21,9 +21,11 @@ Boston, MA 02110-1301, USA. package de.openrat.android.blog.client; import java.io.BufferedReader; +import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.io.OutputStream; import java.io.PrintWriter; import java.io.Serializable; import java.io.UnsupportedEncodingException; @@ -383,7 +385,7 @@ public class HTTPRequest implements Serializable // using HTTP/1.0 as this is supported by all HTTP-servers and // proxys. // We have no need for HTTP/1.1 at the moment. - header.append(this.method + " " + httpUrl + " HTTP/1.0"+CRLF); + header.append(this.method + " " + httpUrl + " HTTP/1.0" + CRLF); // Setting the HTTP Header Map<String, String> headers = new HashMap<String, String>(); @@ -408,9 +410,9 @@ public class HTTPRequest implements Serializable { headers.put("Content-Type", multipart.getContentType()); - headers.put("Content-Length", ""+multipart.getPayload().getBytes().length); - - + headers.put("Content-Length", "" + + multipart.getPayload().length); + } else { headers.put("Content-Type", "text/plain"); @@ -428,14 +430,18 @@ public class HTTPRequest implements Serializable header.append(CRLF); + final OutputStream outputStream = socket + .getOutputStream(); + outputStream.write(header.toString().getBytes()); + if (HTTP_POST.equals(this.method)) { if (body == null && multipart.parts.size() == 0) - header.append(parameterList); + outputStream.write(parameterList.toString().getBytes()); else if (multipart.parts.size() > 0) - header.append(multipart.getPayload()); + outputStream.write(multipart.getPayload()); else - header.append(body); + outputStream.write(body.getBytes()); } if (this.trace) @@ -443,11 +449,8 @@ public class HTTPRequest implements Serializable if (this.trace) System.out.println(header.toString()); - final PrintWriter printWriter = new PrintWriter(socket - .getOutputStream(), true); - printWriter.write(header.toString()); - printWriter.flush(); + outputStream.flush(); final InputStream inputStream = socket.getInputStream(); final int available = inputStream.available(); @@ -537,13 +540,13 @@ public class HTTPRequest implements Serializable private class Multipart implements Serializable { - private static final String CRLF = "\r\n"; + private static final String CRLF = "\r\n"; private static final String BOUNDARY = "614BA262123F3B29656A745C5DD26"; List<Part> parts = new ArrayList<Part>(); - public String getPayload() + public byte[] getPayload() throws IOException { - StringBuffer body = new StringBuffer(); + HttpOutputStream body = new HttpOutputStream(); for (Part part : parts) { @@ -561,13 +564,13 @@ public class HTTPRequest implements Serializable + part.filename + "\"") : "") + CRLF); body.append(CRLF); if (part.file.length > 0) - body.append(new String(part.file)); + body.write(part.file); else body.append(part.text); body.append(CRLF); } body.append("--" + BOUNDARY + "--"); - return body.toString(); + return body.toByteArray(); } public String getContentType() @@ -585,4 +588,17 @@ public class HTTPRequest implements Serializable public String contentType; public String encoding; } + + private class HttpOutputStream extends ByteArrayOutputStream + { + + public void write(String s) throws IOException + { + super.write(s.getBytes()); + } + public void append(String s) throws IOException + { + super.write(s.getBytes()); + } + } } diff --git a/src/de/openrat/android/blog/util/FileUtils.java b/src/de/openrat/android/blog/util/FileUtils.java @@ -0,0 +1,49 @@ +/** + * + */ +package de.openrat.android.blog.util; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; + +/** + * @author dankert + * + */ +public class FileUtils +{ + + public static byte[] getBytesFromFile(File file) throws IOException { + InputStream is = new FileInputStream(file); + + // Get the size of the file + long length = file.length(); + + if (length > Integer.MAX_VALUE) { + // File is too large + throw new IOException("File is too large"); + } + + // Create the byte array to hold the data + byte[] bytes = new byte[(int)length]; + + // Read in the bytes + int offset = 0; + int numRead = 0; + while (offset < bytes.length + && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) { + offset += numRead; + } + + // Ensure all the bytes have been read in + if (offset < bytes.length) { + throw new IOException("Could not completely read file "+file.getName()); + } + + // Close the input stream and return bytes + is.close(); + return bytes; + } +}