class ByteUtilities {

    public static int readNBytes(InputStream in, OutputStream out, int byteCount) throws IOException
    {
        int totalBytesRead = 0;
        int bytesRead;
        byte[] bytes = new byte[BUFFER_SIZE];
        while (totalBytesRead < byteCount)
        {
            bytesRead = in.read(bytes, 0, Math.min(bytes.length, byteCount - totalBytesRead));
            if (bytesRead > 0)
            {
                out.write(bytes, 0, bytesRead);
                totalBytesRead += bytesRead;
            }
            else if (bytesRead == -1)
            {
                break;
            }
        }
        return totalBytesRead;
    }

    private  ByteUtilities();

}