1/*
2 * Copyright 2008 CoreMedia AG, Hamburg
3 *
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.coremedia.iso.boxes;
18
19import com.coremedia.iso.IsoTypeReader;
20import com.coremedia.iso.IsoTypeWriter;
21import com.coremedia.iso.Utf8;
22import com.googlecode.mp4parser.AbstractFullBox;
23
24import java.nio.ByteBuffer;
25
26/**
27 * List of keywords according to 3GPP 26.244.
28 */
29public class KeywordsBox extends AbstractFullBox {
30    public static final String TYPE = "kywd";
31
32    private String language;
33    private String[] keywords;
34
35    public KeywordsBox() {
36        super(TYPE);
37    }
38
39    public String getLanguage() {
40        return language;
41    }
42
43    public String[] getKeywords() {
44        return keywords;
45    }
46
47    public void setLanguage(String language) {
48        this.language = language;
49    }
50
51    public void setKeywords(String[] keywords) {
52        this.keywords = keywords;
53    }
54
55    protected long getContentSize() {
56        long contentSize = 7;
57        for (String keyword : keywords) {
58            contentSize += 1 + Utf8.utf8StringLengthInBytes(keyword) + 1;
59        }
60        return contentSize;
61    }
62
63    @Override
64    public void _parseDetails(ByteBuffer content) {
65        parseVersionAndFlags(content);
66        language = IsoTypeReader.readIso639(content);
67        int keywordCount = IsoTypeReader.readUInt8(content);
68        keywords = new String[keywordCount];
69        for (int i = 0; i < keywordCount; i++) {
70            IsoTypeReader.readUInt8(content);
71            keywords[i] = IsoTypeReader.readString(content);
72        }
73    }
74
75    @Override
76    protected void getContent(ByteBuffer byteBuffer) {
77        writeVersionAndFlags(byteBuffer);
78        IsoTypeWriter.writeIso639(byteBuffer, language);
79        IsoTypeWriter.writeUInt8(byteBuffer, keywords.length);
80        for (String keyword : keywords) {
81            IsoTypeWriter.writeUInt8(byteBuffer, Utf8.utf8StringLengthInBytes(keyword) + 1);
82            byteBuffer.put(Utf8.convert(keyword));
83        }
84    }
85
86    public String toString() {
87        StringBuffer buffer = new StringBuffer();
88        buffer.append("KeywordsBox[language=").append(getLanguage());
89        for (int i = 0; i < keywords.length; i++) {
90            buffer.append(";keyword").append(i).append("=").append(keywords[i]);
91        }
92        buffer.append("]");
93        return buffer.toString();
94    }
95}
96