private String readResponse(HttpURLConnection connection) throws IOException {
        if (connection.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND)
            throw new SdkClientException("The requested metadata is not found at " + connection.getURL());

        InputStream inputStream = connection.getInputStream();

        try {
            StringBuilder buffer = new StringBuilder();
            while (true) {
                int c = inputStream.read();
                if (c == -1) break;
                buffer.append((char)c);
            }

            return buffer.toString();
        } finally {
            inputStream.close();
        }
    }
Reads a response from the Amazon EC2 Instance Metadata Service and
returns the content as a string.

@param connection
           The connection to the Amazon EC2 Instance Metadata Service.

@return The content contained in the response from the Amazon EC2
        Instance Metadata Service.

@throws IOException
            If any problems ocurred while reading the response.
aws-java-sdk-core/src/main/java/com/amazonaws/internal/EC2MetadataClient.java