Apache HttpClient 4 byte handling

After I wrote my own HttpClient ResponseHandler in Apache HttpClient 4 Custom Bytes ResponseHandler, I discovered that there might be a better way. I had used the following utility to get the bytes. I found that one of their classes also used it.

EntityUtils.toByteArray(entity);

Here is what I came up with…

    /**
     * Get the weather radar bytes image.
     * @return byte[] of weather radar data
     */
    @Nullable
    public BufferedHttpEntity getWeatherRadarBytes() {
        HttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet("http://services.intellicast.com/200904-01/387694749/Image/Radar/Winter%20Storm%20Mosaic/Loop/SectorName/bwg");


        BufferedHttpEntity bufferedHttpEntity = null;
        try {
            HttpResponse httpResponse = httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
            bufferedHttpEntity = new BufferedHttpEntity(httpEntity);
        } catch (IOException e) {
            log.error("IOException", e);
        } finally {
            // When HttpClient instance is no longer needed,
            // shut down the connection manager to ensure
            // immediate deallocation of all system resources
            httpClient.getConnectionManager().shutdown();
        }
        return bufferedHttpEntity;
    }

An example of using it would be something like the following. NOTE: I never ran this code. Use at your own risk.

    @Test
    public void testGetWeatherRadar() {
        BufferedHttpEntity bufferedHttpEntity = httpService.getWeatherRadarBytes();
        assertNotNull(bufferedHttpEntity);
        assertTrue(bufferedHttpEntity.getContentLength() > 0);

        FileOutputStream fileOutputStream = null;
        try {
            File file = new File("/staticfiles/weather-portlet/kyRadarImage.gif");
            fileOutputStream = new FileOutputStream(file);
            byte[] bytes = new byte[bufferedHttpEntity.getContent().available()];
            bufferedHttpEntity.getContent().read(bytes);
            fileOutputStream.write(bytes);
            fileOutputStream.flush();
            fileOutputStream.close();
        } catch (FileNotFoundException e) {
            fail(e.getMessage());
        } catch (IOException e) {
            fail(e.getMessage());
        } finally {
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    fail(e.getMessage());
                }
            }
        }
    }
This entry was posted in Java and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s