0

What I'm trying to do is to generate a byte array from a url.

byte[] data = WebServiceClient.download(url);

The url returns json

public static byte[] download(String url) {
    HttpClient client = new DefaultHttpClient();
    HttpGet get = new HttpGet(url);
    try {
        HttpResponse response = client.execute(get);
        StatusLine status = response.getStatusLine();
        int code = status.getStatusCode();
        switch (code) {
            case 200:
                StringBuffer sb = new StringBuffer();
                HttpEntity entity = response.getEntity();
                InputStream is = entity.getContent();
                BufferedReader br = new BufferedReader(new InputStreamReader(is));
                String line;
                while ((line = br.readLine()) != null) {
                    sb.append(line);
                }
                is.close();

                sContent = sb.toString();

                break;       
        }
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return sContent.getBytes();
}

This data is used as a parameter for String

String json = new String(data, "UTF-8");
JSONObject obj = new JSONObject(json);

for some reason, I get this error

I/global  (  631): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.

I think something there must be missing here sContent = sb.toString(); or here return sContent.getBytes(); but I'm not sure though.

6
  • 2
    Is this android (Dalvik VM)? Commented Apr 26, 2013 at 13:00
  • 2
    Does the error go away if you do new BufferedReader(new InputStreamReader(is), 8192); ? Commented Apr 26, 2013 at 13:00
  • I'd suggest you use the BufferedInputStream to read 4 or 8 kilo bytes chunk of data, rather than dealing with character data and messing with Character sets. Commented Apr 26, 2013 at 13:01
  • 2
    possible duplicate of wrong usage of BufferedReader Commented Apr 26, 2013 at 13:02
  • BTW, you can use a StringBuilder instead a StringBuffer: in a local variable, synchronous operations are not needed Commented Apr 26, 2013 at 13:21

1 Answer 1

3

1. Consider using Apache commons-io to read the bytes from InputStream

InputStream is = entity.getContent();
try {
    return IOUtils.toByteArray(is);
}finally{
    is.close();
}

Currently you're unnecessarily converting the bytes to characters and back.

2. Avoid using String.getBytes() without passing the charset as a parameter. Instead use

String s = ...;
s.getBytes("utf-8")


As a whole I'd rewrite you're method like this:

public static byte[] download(String url) throws IOException {
    HttpClient client = new DefaultHttpClient();
    HttpGet get = new HttpGet(url);
    HttpResponse response = client.execute(get);
    StatusLine status = response.getStatusLine();
    int code = status.getStatusCode();
    if(code != 200) {
        throw new IOException(code+" response received.");
    }
    HttpEntity entity = response.getEntity();
    InputStream is = entity.getContent();
    try {
        return IOUtils.toByteArray(is);
    }finally{
        IOUtils.closeQuietly(is.close());
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Probably also use IOUtils.closeQuietly().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.