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
package Torello.Java;

import java.util.Base64;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

import java.io.IOException;

class B64
{
    static String objToB64Str(Object o) throws IOException
    {
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();

        try (
            final GZIPOutputStream      gzip    = new GZIPOutputStream(baos);
            final ObjectOutputStream    oos     = new ObjectOutputStream(gzip);
        )
            { oos.writeObject(o); }

        baos.flush();
        baos.close();

        return Base64
            .getEncoder()
            .encodeToString(baos.toByteArray());
    }

    static Object b64StrToObj(String str) throws IOException
    {
        final byte[] bArr = Base64.getDecoder().decode(str);

        Object ret = null;

        try(
            final ByteArrayInputStream  bais    = new ByteArrayInputStream(bArr);
            final GZIPInputStream       gzip    = new GZIPInputStream(bais);
            final ObjectInputStream     ois     = new ObjectInputStream(gzip);
        )

            { ret = ois.readObject(); }

        catch (ClassNotFoundException e)
        {
            throw new IOException(
                "There were no serialized objects found in your String.  See e.getCause();",
                e
            );
        }

        return ret;
    }

    static String objToB64MimeStr(Object o) throws IOException
    {
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();

        try (
            final GZIPOutputStream      gzip    = new GZIPOutputStream(baos);
            final ObjectOutputStream    oos     = new ObjectOutputStream(gzip);
        )

            { oos.writeObject(o); }

        baos.flush();
        baos.close();

        return Base64
            .getMimeEncoder()
            .encodeToString(baos.toByteArray());
    }

    static Object b64MimeStrToObj(String str) throws IOException
    {
        Object ret = null;

        final byte[] bArr = Base64.getMimeDecoder().decode(str);

        try(
            final ByteArrayInputStream  bais    = new ByteArrayInputStream(bArr);
            final GZIPInputStream       gzip    = new GZIPInputStream(bais);
            final ObjectInputStream     ois     = new ObjectInputStream(gzip);
        )
            { ret = ois.readObject(); }

        catch (ClassNotFoundException e)
        { 
            throw new IOException(
                "There were no serialized objects found in your String.  See e.getCause();",
                e
            );
        }

        return ret;
    }

}