1 /*****************************************************************
2 |
3 |    Copyright 2002-2008 Axiomatic Systems, LLC
4 |
5 |    $Id: DataAtom.java 196 2008-10-14 22:59:31Z bok $
6 |
7 |    This file is part of Bento4/AP4 (MP4 Atom Processing Library).
8 |
9 |    Unless you have obtained Bento4 under a difference license,
10 |    this version of Bento4 is Bento4|GPL.
11 |    Bento4|GPL is free software; you can redistribute it and/or modify
12 |    it under the terms of the GNU General Public License as published by
13 |    the Free Software Foundation; either version 2, or (at your option)
14 |    any later version.
15 |
16 |    Bento4|GPL is distributed in the hope that it will be useful,
17 |    but WITHOUT ANY WARRANTY; without even the implied warranty of
18 |    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 |    GNU General Public License for more details.
20 |
21 |    You should have received a copy of the GNU General Public License
22 |    along with Bento4|GPL; see the file COPYING.  If not, write to the
23 |    Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 |    02111-1307, USA.
25 |
26 ****************************************************************/
27 
28 package com.axiosys.bento4.metadata;
29 
30 import java.io.DataOutputStream;
31 import java.io.IOException;
32 import java.io.RandomAccessFile;
33 
34 import com.axiosys.bento4.Atom;
35 
36 public class DataAtom extends Atom {
37     public final static int  DATA_TYPE_BINARY             = 0;
38     public final static int  DATA_TYPE_STRING_UTF8        = 1;
39     public final static int  DATA_TYPE_STRING_UTF16       = 2;
40     public final static int  DATA_TYPE_STRING_MAC_ENCODED = 3;
41     public final static int  DATA_TYPE_JPEG               = 14;
42     public final static int  DATA_TYPE_SIGNED_INT_BE      = 21; /* the size of the integer is derived from the container size */
43     public final static int  DATA_TYPE_FLOAT32_BE         = 22;
44     public final static int  DATA_TYPE_FLOAT64_BE         = 23;
45 
46     private int              dataType;
47     private int              dataLang;
48     private RandomAccessFile source;
49     private long             sourceOffset;
50     private int              payloadSize;
51 
DataAtom(int size, RandomAccessFile source)52     public DataAtom(int size, RandomAccessFile source) throws IOException {
53         super(MetaData.TYPE_DATA, size, false);
54 
55         if (size < Atom.HEADER_SIZE+8) return;
56 
57         dataType = source.readInt();
58         dataLang = source.readInt();
59 
60         // the stream for the data is a substream of this source
61         this.source = source;
62         sourceOffset = source.getFilePointer();
63         payloadSize  = size-Atom.HEADER_SIZE-8;
64     }
65 
66     // accessors
getDataType()67     int getDataType() { return dataType; }
getDataLang()68     int getDataLang() { return dataLang; }
69 
70     // methods
loadString()71     String loadString() throws IOException {
72         return new String(loadBytes(), "UTF-8");
73     }
74 
loadBytes()75     byte[] loadBytes() throws IOException {
76         source.seek(sourceOffset);
77         byte[] str = new byte[payloadSize];
78         source.read(str);
79 
80         return str;
81     }
82 
loadInteger()83     long loadInteger() throws IOException {
84         source.seek(sourceOffset);
85         switch (payloadSize) {
86             case 1:
87                 return source.readByte();
88 
89             case 2:
90                 return source.readShort();
91 
92             case 4:
93                 return source.readInt();
94 
95             case 8:
96                 return source.readLong();
97 
98             default:
99                 throw new IOException("invalid integer size");
100         }
101     }
102 
writeFields(DataOutputStream stream)103     protected void writeFields(DataOutputStream stream) throws IOException {
104         // not implemented yet
105         throw new RuntimeException("not implemented yet");
106     }
107 
toString(String indentation)108     public String toString(String indentation) {
109         StringBuffer result = new StringBuffer(super.toString(indentation));
110         result.append("\n" + indentation + " data_type = " + dataType);
111         result.append("\n" + indentation + " data_lang = " + dataLang);
112         String dataString;
113         try {
114             switch (dataType) {
115                 case DATA_TYPE_STRING_UTF8: dataString = loadString(); break;
116                 case DATA_TYPE_SIGNED_INT_BE: dataString = Integer.toString((int) loadInteger());  break;
117                 default: dataString = Integer.toString(payloadSize) + " bytes"; break;
118             }
119         } catch (Exception e) {
120             dataString = "";
121         }
122 
123         result.append("\n" + indentation + " data      = " + dataString);
124         return result.toString();
125     }
126 }
127