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 int maxAge;
033    private final String type;
034
035    private byte[] contentBinary;
036    private String contentString;
037
038    public BoBData(String type, byte[] content) {
039        this(type, content, -1);
040    }
041
042    /**
043     * BoB data constructor.
044     *
045     * @param type
046     * @param content
047     * @param maxAge
048     */
049    public BoBData(String type, byte[] content, int maxAge) {
050        this.type = type;
051        this.contentBinary = content;
052        this.maxAge = maxAge;
053    }
054
055    public BoBData(String type, String content) {
056        this(type, content, -1);
057    }
058
059    public BoBData(String type, String content, int maxAge) {
060        this.type = type;
061        this.contentString = content;
062        this.maxAge = maxAge;
063    }
064
065    /**
066     * Get the max age.
067     *
068     * @return the max age
069     */
070    public int getMaxAge() {
071        return maxAge;
072    }
073
074    /**
075     * Get the type.
076     *
077     * @return the type
078     */
079    public String getType() {
080        return type;
081    }
082
083    private void setContentBinaryIfRequired() {
084        if (contentBinary == null) {
085            assert (StringUtils.isNotEmpty(contentString));
086            contentBinary = Base64.decode(contentString);
087        }
088    }
089
090    /**
091     * Get the content.
092     *
093     * @return the content
094     */
095    public byte[] getContent() {
096        setContentBinaryIfRequired();
097        return contentBinary.clone();
098    }
099
100    /**
101     * Get the content in a Base64 encoded String.
102     *
103     * @return the content in a Base64 encoded String
104     */
105    public String getContentBase64Encoded() {
106        if (contentString == null) {
107            contentString = Base64.encodeToString(getContent());
108        }
109        return contentString;
110    }
111
112    /**
113     * Check if the data is of reasonable size. XEP-0231 suggest that the size should not be more than 8 KiB.
114     *
115     * @return true if the data if of reasonable size.
116     */
117    public boolean isOfReasonableSize() {
118        setContentBinaryIfRequired();
119        return contentBinary.length <= 8 * 1024;
120    }
121}