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
package Torello.HTML.Tools.Images;

import Torello.Java.Additional.Ret2;

import java.awt.image.BufferedImage;

// Convert a B64-Image to a java.awt.image.BufferedImage instance
class ConvertB64Image 
{
    static Ret2<BufferedImage, IF> run(final RECORD r)
        throws ImageScraperException
    {
        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        // Internally, the request-class saves B64-Images as Two-Element String[]-Array's
        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***

        final String  imageFormatStr    = r.b64ImageData[0];
        final String  b64EncodedImage   = r.b64ImageData[1];


        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        // Skipping B64-Images entirely is one of the boolean-options in 'Request'
        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***

        if (r.request.skipBase64EncodedImages)
        {
            if      (r.logLevelEQ1)     r.append("x ");
            else if (r.logLevelGTEQ2)   r.appendI4
                ("Skipping - Skip Request for all Base64-Encoded Images\n");

            r.results.skipB64();

            return null;
        }


        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        // Try to do the B64-Converstion
        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***

        try
        {
            final IF ext = IF.get(imageFormatStr);

            final BufferedImage image = IF.decodeBase64ToImage(b64EncodedImage, ext);

            // SUCCESS!
            if (image != null) new Ret2<>(image, ext);
        }
    
        catch (Exception e)
        {
            // This call either returns null, or throws an ImageScraperException
            return r.reportEx(
                r.request.skipOnB64DecodeException,
                "Exception throw Java's Base-64 Image Decoder while decoding a Base-64 Image",
                "Base-64 Image Decoding",
                e
            );
        }


        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        // ELSE: The Image was null, so use 'NullImageException'
        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***

        final Exception niex = new NullImageException(
            "The B64-Image Encoding Regular-Expression matched the Source-URL, but " +
            "Java's B64-Image Decoder has returned null upon decoding it."
        );

        niex.fillInStackTrace();

        // Returns 'null', or throws an exception
        return r.reportEx(
            r.request.skipOnNullImageException,
            "Null Image returned by Java's B64 Image-Decoder",
            "Base-64 Image Decoding",
            niex
        );
    }

}