Permalink
Browse files

work-around potential Android 2.3 bug with reading streams

  • Loading branch information...
1 parent 7cfc9d8 commit 8d8e417f7b7cb1b1132da90a2d65990f56d3d610 @beders committed Feb 27, 2013
Showing with 32 additions and 6 deletions.
  1. +32 −6 src/main/java/us/monoid/web/AbstractResource.java
@@ -5,6 +5,9 @@
import java.io.*;
import java.net.*;
+import java.util.List;
+import java.util.Map;
+import java.util.logging.Logger;
/**
* Abstract base class for all resource handlers you want to use with Resty.
@@ -15,14 +18,15 @@
*
*/
public abstract class AbstractResource extends Resty {
+ static final Logger log = Logger.getLogger(AbstractResource.class.getName());
protected URLConnection urlConnection;
protected InputStream inputStream;
public AbstractResource(Option... options) {
super(options);
}
- abstract String getAcceptedTypes();
+ protected abstract String getAcceptedTypes();
void fill(URLConnection anUrlConnection) throws IOException {
urlConnection = anUrlConnection;
@@ -38,11 +42,15 @@ void fill(URLConnection anUrlConnection) throws IOException {
InputStream es = new BufferedInputStream(conn.getErrorStream());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
- // read the response body
- byte[] buf = new byte[1024];
- int read = -1;
- while ((read = es.read(buf)) > 0) {
- baos.write(buf, 0, read);
+ try {
+ // read the response body
+ byte[] buf = new byte[1024];
+ int read = -1;
+ while ((read = es.read(buf)) > 0) {
+ baos.write(buf, 0, read);
+ }
+ } catch (IOException e1) {
+ log.warning("IOException when reading the error stream. Igored");
}
// close the errorstream
@@ -98,4 +106,22 @@ public URI location() {
}
return null;
}
+
+ /** Print out the response headers for this resource.
+ *
+ * @return
+ */
+ public String printResponseHeaders() {
+ StringBuilder sb = new StringBuilder();
+ HttpURLConnection http = http();
+ if (http != null) {
+ Map<String, List<String>> header = http.getHeaderFields();
+ for (String key : header.keySet()) {
+ for (String val : header.get(key)) {
+ sb.append(key).append(": ").append(val).append("\n");
+ }
+ }
+ }
+ return sb.toString();
+ }
}

0 comments on commit 8d8e417

Please sign in to comment.