ByteContent.java

 1package im.conversations.android.xmpp.model;
 2
 3import com.google.common.base.CharMatcher;
 4import com.google.common.base.Strings;
 5import com.google.common.io.BaseEncoding;
 6
 7import eu.siacs.conversations.xml.Element;
 8
 9public interface ByteContent {
10
11    String getContent();
12
13    default byte[] asBytes() {
14        final var content = this.getContent();
15        if (Strings.isNullOrEmpty(content)) {
16            throw new IllegalStateException(
17                    String.format("%s element is lacking content", getClass().getName()));
18        }
19        final var contentCleaned = CharMatcher.whitespace().removeFrom(content);
20        if (BaseEncoding.base64().canDecode(contentCleaned)) {
21            return BaseEncoding.base64().decode(contentCleaned);
22        } else {
23            throw new IllegalStateException(
24                    String.format("%s element contains invalid base64", getClass().getName()));
25        }
26    }
27
28    default void setContent(final byte[] bytes) {
29        setContent(BaseEncoding.base64().encode(bytes));
30    }
31
32    Element setContent(final String content);
33}