public static ByteArrayInputStream readStreamToMemory(InputStream inputStream) throws IOException {
        if (inputStream instanceof ByteArrayInputStream) {
            inputStream.reset();
            return (ByteArrayInputStream) inputStream;
        }
        try {
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream(1024 * 80);
            IOUtils.copy(inputStream, outputStream);
            return new ByteArrayInputStream(outputStream.toByteArray());
        }
        finally {
            IOUtils.closeQuietly(inputStream);
        }
    }