Atom.java
上传用户:xjjlds
上传日期:2015-12-05
资源大小:22823k
文件大小:4k
源码类别:

多媒体编程

开发平台:

Visual C++

  1. package com.axiosys.bento4;
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.DataOutputStream;
  4. import java.io.IOException;
  5. import java.io.RandomAccessFile;
  6. public abstract class Atom {
  7.     public final static int TYPE_MOOV = 0x6d6f6f76;
  8.     public final static int TYPE_TRAK = 0x7472616b;
  9.     public final static int TYPE_HNTI = 0x686e7469;
  10.     public final static int TYPE_STBL = 0x7374626c;
  11.     public final static int TYPE_MDIA = 0x6d646961;
  12.     public final static int TYPE_DINF = 0x64696e66;
  13.     public final static int TYPE_MINF = 0x6d696e66;
  14.     public final static int TYPE_SCHI = 0x73636869;
  15.     public final static int TYPE_SINF = 0x73696e66;
  16.     public final static int TYPE_UDTA = 0x75647461;
  17.     public final static int TYPE_ILST = 0x696c7374;
  18.     public final static int TYPE_EDTS = 0x65647473;
  19.     public final static int TYPE_META = 0x6d657461;
  20.     public final static int TYPE_STSD = 0x73747364;    
  21.     public final static int TYPE_MP4A = 0x6d703461;
  22.     public final static int TYPE_ENCA = 0x656e6361;
  23.     public final static int TYPE_MP4V = 0x6d703476;
  24.     public final static int TYPE_ENCV = 0x656e6376;
  25.     public final static int TYPE_IKMS = 0x694b4d53;
  26.     public final static int TYPE_TKHD = 0x746b6864;
  27.     public final static int TYPE_SCHM = 0x7363686d;
  28.     public final static int TYPE_HDLR = 0x68646c72;
  29.     
  30.     public final static int HEADER_SIZE      = 8;
  31.     public final static int FULL_HEADER_SIZE = 12;
  32.     
  33.     // members
  34.     protected int type;
  35.     protected int size;
  36.     protected int flags;
  37.     protected int version;
  38.     protected boolean isFull;
  39.     
  40.     public static String typeString(int type) {
  41.         StringBuffer result = new StringBuffer(4);
  42.     
  43.         result.append((char)((type>>24)&0xFF));
  44.         result.append((char)((type>>16)&0xFF));
  45.         result.append((char)((type>> 8)&0xFF));
  46.         result.append((char)((type    )&0xFF));
  47.         return result.toString();
  48.     }
  49.     
  50.     public static int nameToType(String name) {
  51.         return ((name.charAt(0)&0xFF)<<24) |
  52.                ((name.charAt(1)&0xFF)<<16) |
  53.                ((name.charAt(2)&0xFF)<< 8) |
  54.                ((name.charAt(3)&0xFF));
  55.     }
  56.         
  57.     public Atom(int type, int size, boolean isFull) {
  58.         this.type = type;
  59.         this.size = size;
  60.         this.isFull = isFull;        
  61.     }
  62.     
  63.     public Atom(int type, int size, boolean isFull, RandomAccessFile source) throws IOException {
  64.         this(type, size, isFull);
  65.         if (isFull) {
  66.             // read the version and flags
  67.             int extension = source.readInt();
  68.             version = (extension>>24)&0xFF;
  69.             flags   =  extension&0xFFFFFF;
  70.         } else {
  71.             this.flags = 0;
  72.             this.version = 0;
  73.         }
  74.     }
  75.     public int getType()        { return type;                                }
  76.     public int getSize()        { return size;                                }
  77.     public int getHeaderSize()  { return isFull?FULL_HEADER_SIZE:HEADER_SIZE; }
  78.     public int getPayloadSize() { return size-getHeaderSize();                }
  79.     
  80.     public void write(DataOutputStream stream) throws IOException {
  81.         // write the header
  82.         writeHeader(stream);
  83.         // write the fields
  84.         writeFields(stream);
  85.     }
  86.     
  87.     public void writeHeader(DataOutputStream stream) throws IOException {
  88.         // write the size
  89.         stream.writeInt(size);
  90.         // write the type
  91.         stream.writeInt(type);
  92.         // for full atoms, write version and flags
  93.         if (isFull) {
  94.             stream.writeInt(version<<24 | flags);
  95.         }        
  96.     }
  97.     
  98.     protected abstract void writeFields(DataOutputStream stream) throws IOException;
  99.     
  100.     public byte[] toBytes() throws IOException {
  101.         ByteArrayOutputStream stream = new ByteArrayOutputStream();
  102.         DataOutputStream output = new DataOutputStream(stream);
  103.         write(output);
  104.         
  105.         return stream.toByteArray();
  106.     }
  107.     
  108.     public byte[] getPayload() throws IOException {
  109.         byte[] bytes = toBytes();
  110.         byte[] result = new byte[getPayloadSize()];
  111.         System.arraycopy(bytes, getHeaderSize(), result, 0, result.length);
  112.         
  113.         return result;
  114.     }
  115.     
  116.     public String toString(String indentation) {
  117.         return indentation+"[" + typeString(type) + "] size=" + getHeaderSize() + "+" + getPayloadSize();
  118.     }
  119.     
  120.     public String toString() {
  121.         return toString("");
  122.     }
  123. }