Data Transmission Without Compression

Context

Implementation,Network

Affects

Energy Efficiency

Problem

Höpfner and Bunse discussed in their publication “Towards an energy-consumption based complexity classification for resource substitution strategies”
that transmitting a file over a network infrastructure without compressing it consumes more energy.
More precisely, Energy Efficiency
is improved in case the data is compressed at least by 10%, transmitted and decompressed at the other network node.

The following example show file transmission implemented with the Apache HTTP Client Library (the example is taken from ClientMultipartFormPost.java).

public static void main(String[] args) throws Exception {
    if (args.length != 1)  {
        System.out.println("File path not given");
        System.exit(1);
    }
    HttpClient httpclient = new DefaultHttpClient();
    try {

        HttpPost httppost = new HttpPost("http://some.url:8080/servlets-examples/servlet/RequestInfoExample");
        // the passed File object in this constructor is transmitted without compression
        FileBody bin = new FileBody(new File(args[0]));
        StringBody comment = new StringBody("A binary file of some kind");

        MultipartEntity reqEntity = new MultipartEntity();
        reqEntity.addPart("bin", bin);
        reqEntity.addPart("comment", comment);

        httppost.setEntity(reqEntity);

        System.out.println("executing request " + httppost.getRequestLine());
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity resEntity = response.getEntity();

        System.out.println("----------------------------------------");
        System.out.println(response.getStatusLine());
        if (resEntity != null) {
            System.out.println("Response content length: " + resEntity.getContentLength());
        }
        EntityUtils.consume(resEntity);
    } finally {
        try { httpclient.getConnectionManager().shutdown(); } catch (Exception ignore) {}
    }

}

Refactorings

Add Data Compression to Apache HTTP Client based file transmission

Resolves

Energy Efficiency

Affects

Solution

Compress the File object before transmitting it.

    public static void main(String[] args) throws Exception {
        // ...
        // the passed File object now is compressed
        FileBody bin = new FileBody(gzipFile(file)) ;
        // ...
    }
    private static File gzipFile(File uncompressedFile){
        try {
            assert uncompressedFile != null && uncompressedFile.exists();
            File gzippedFile = File.createTempFile(uncompressedFile.getName(), "gz");
            FileInputStream fis = new FileInputStream(uncompressedFile);
            GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(gzippedFile));
            byte[] buffer = new byte[4096];
            int bytesRead;
            while ((bytesRead = fis.read(buffer)) != -1){
                out.write(buffer,0, bytesRead);
            }
            fis.close();
            out.close();
            return gzippedFile;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

Links

Links

Related