public byte[] toByteArray() {
        byte[] returnValue = null;
        try {
            // if in cached mode read from the tmp file
            if (isCached) {
                if (tempFile != null) {
                    // Create a writable file channel, can't use instance as
                    // it will have funky side effects, for muliple calls.
                    FileInputStream fileInputStream = new FileInputStream(tempFile);
                    returnValue = new byte[length];
                    // copy the bytes
                    fileInputStream.read(returnValue);
                    fileInputStream.close();
                }
            } else {
                // copy origional data to output stream
                if (byteArrayOutputStream != null)
                    returnValue = byteArrayOutputStream.toByteArray();
            }
        }
        catch (IOException e) {
            logger.log(Level.FINE, "Error reading from temporary file.", e);
        }
        return returnValue;
    }