001/**
002 *
003 * Copyright 2016-2017 Fernando Ramirez, Florian Schmaus
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smackx.bob;
018
019import org.jivesoftware.smack.util.StringUtils;
020import org.jivesoftware.smack.util.stringencoder.Base64;
021
022/**
023 * Bits of Binary data class.
024 *
025 * @author Fernando Ramirez
026 * @author Florian Schmaus
027 * @see <a href="http://xmpp.org/extensions/xep-0231.html">XEP-0231: Bits of
028 *      Binary</a>
029 */
030public class BoBData {
031
032    private final Integer maxAge;
033    private final String type;
034
035    private byte[] contentBinary;
036    private String contentString;
037
038    private BoBData(String type, Integer maxAge) {
039        this.type = type;
040        this.maxAge = maxAge;
041    }
042
043    public BoBData(String type, byte[] content) {
044        this(type, content, null);
045    }
046
047    /**
048     * BoB data constructor.
049     *
050     * @param type TODO javadoc me please
051     * @param content TODO javadoc me please
052     * @param maxAge TODO javadoc me please
053     */
054    public BoBData(String type, byte[] content, Integer maxAge) {
055        this(type, maxAge);
056        this.contentBinary = content;
057    }
058
059    public BoBData(String type, String content) {
060        this(type, content, null);
061    }
062
063    public BoBData(String type, String content, Integer maxAge) {
064        this(type, maxAge);
065        this.contentString = content;
066    }
067
068    /**
069     * Get the max age.
070     *
071     * @return the max age
072     */
073    public Integer getMaxAge() {
074        return maxAge;
075    }
076
077    /**
078     * Get the type.
079     *
080     * @return the type
081     */
082    public String getType() {
083        return type;
084    }
085
086    private void setContentBinaryIfRequired() {
087        if (contentBinary == null) {
088            assert StringUtils.isNotEmpty(contentString);
089            contentBinary = Base64.decode(contentString);
090        }
091    }
092
093    /**
094     * Get the content.
095     *
096     * @return the content
097     */
098    public byte[] getContent() {
099        setContentBinaryIfRequired();
100        return contentBinary.clone();
101    }
102
103    /**
104     * Get the content in a Base64 encoded String.
105     *
106     * @return the content in a Base64 encoded String
107     */
108    public String getContentBase64Encoded() {
109        if (contentString == null) {
110            contentString = Base64.encodeToString(getContent());
111        }
112        return contentString;
113    }
114
115    /**
116     * Check if the data is of reasonable size. XEP-0231 suggest that the size should not be more than 8 KiB.
117     *
118     * @return true if the data if of reasonable size.
119     */
120    public boolean isOfReasonableSize() {
121        setContentBinaryIfRequired();
122        return contentBinary.length <= 8 * 1024;
123    }
124}