1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package Torello.HTML.Tools.Images;

import Torello.Java.Additional.Ret2;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;

import javax.imageio.ImageIO;

// Convert the java.awt.image.BufferedImage **INTO** a Java byte[]-Array
// 
// This just converts an image in the format of a 'BufferedImage' into an image that is an
// array of bytes.  This method will attempt to save the image using the format that was
// extracted using the URL-Name / FileName.  If that fails, there is a for-loop that will
// attempt to save the image using the other formats.

class BufferedImageToByteArray 
{
    static Ret2<byte[], IF> convert
        (final RECORD r, final BufferedImage image, IF extGuess)
        throws ImageScraperException
    {
        // This is merely an array of all available formats that may be used to save or download
        // an image.

        final IF[] allFormats = IF.values();

        // This is used to generated the returned byte[] array.
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();

        // This is used if the image could not be converted
        Exception saveItEx = null;


        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        // If the provided Image-Type is NON-NULL, try to save and return the Byte[]-Array
        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***

        if (extGuess != null)

            try
            {
                ImageIO.write(image, extGuess.extension, baos);
                baos.flush();
                baos.close();

                if (r.logLevelEQ3) r.appendI4(
                    "Successfully Saved '." + extGuess.extension + "' URL to a '." +
                    extGuess.extension + "' Formatted Byte-Array.\n"
                );

                return new Ret2<>(baos.toByteArray(), extGuess);
            }

            catch (Exception e)
            {
                // IMPORTANT: It **IS NOT** time to quit yet!  Try the other Image-Types before
                //            reporting this as a Failed / Exception Case.

                saveItEx = e;

                if (r.logLevelEQ3) r.appendI4(
                    "Failed to Convert '." + extGuess.extension + "' URL to a '." +
                    extGuess.extension + "' Formatted Byte-Array.\n"
                );

                for (int i=0; i < allFormats.length; i++)

                    if (allFormats[i] == extGuess) { allFormats[i] = null;  break; }                
            }


        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        // Try any / all other formats that have not yet been attempted
        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***

        for (final IF format : allFormats)

            try
            {
                baos.reset();
                ImageIO.write(image, format.extension, baos);
                baos.flush();
                baos.close();

                if (r.logLevelEQ3) r.appendI4(
                    "Successfully Saved Image-URL to Byte-Array, Using as Guess '." +
                    format.extension + "' Format\n"
                );

                return new Ret2<>(baos.toByteArray(), format);
            }

            catch (Exception e)
                { if (saveItEx == null) saveItEx = e; }


        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        // All attempts to write using a specific format have failed.  Handle the Failure.
        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        //
        // This call either returns null, or throws an ImageScraperException 
        // Depending upon the boolean 'r.request.skipOnImageWritingFail'

        return r.reportEx(
            r.request.skipOnImageWritingFail,
            "Could not translate java.awt.image.BufferedImage to a byte[]-Array with *Any* " +
                "Standard Image-Format",
            "BufferedImage to byte[]-Array",
            saveItEx
        );
    }

}