All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.monte.media.riff.RIFFChunk Maven / Gradle / Ivy

There is a newer version: 1.1
Show newest version
/*
 * @(#)RIFFChunk.java  
 *
 * Copyright (c) 2005-2012 Werner Randelshofer, Goldau, Switzerland.
 * All rights reserved.
 *
 * You may not use, copy or modify this file, except in compliance with the
 * license agreement you entered into with Werner Randelshofer.
 * For details see accompanying license terms.
 */
package org.monte.media.riff;

import java.util.*;
/**
 * RIFF Chunks form the building blocks of a RIFF file.
 *
 * @author  Werner Randelshofer, Hausmatt 10, CH-6405 Goldau, Switzerland
 * @version $Id: RIFFChunk.java 299 2013-01-03 07:40:18Z werner $
 */
public class RIFFChunk {
    private int id;
    private int type;
    private long size;
    private long scan;
    private byte[] data;
    private Hashtable propertyChunks;
    private ArrayList collectionChunks;
    /**
     * This is used to display parser messages, when the parser encounters and
     * error while parsing the chunk.
     */
    private String parserMessage;
    
    public RIFFChunk(int type, int id) {
        this.id = id;
        this.type = type;
        size = -1;
        scan = -1;
    }
    public RIFFChunk(int type, int id, long size, long scan) {
        this.id = id;
        this.type = type;
        this.size = size;
        this.scan = scan;
    }
    public RIFFChunk(int type, int id, long size, long scan, RIFFChunk propGroup) {
        this.id = id;
        this.type = type;
        this.size = size;
        this.scan = scan;
        if (propGroup != null) {
            if (propGroup.propertyChunks != null) {
                propertyChunks = new Hashtable(propGroup.propertyChunks);
            }
            if (propGroup.collectionChunks != null) {
                collectionChunks = new ArrayList(propGroup.collectionChunks);
            }
        }
    }
    
    /**
     * @return  ID of chunk.
     */
    public int getID() {
        return id; 
    }
    
    /**
     * @return  Type of chunk.
     */
    public int getType() {
        return type; 
    }
    
    /**
     * @return  Size of chunk.
     */
    public long getSize() { 
        return size; 
    }
    
    /**
     * @return  Scan position of chunk within the file.
     */
    public long getScan() { 
        return scan; 
    }
    
    public void putPropertyChunk(RIFFChunk chunk) {
        if (propertyChunks == null) { 
            propertyChunks = new Hashtable (); 
        }
        propertyChunks.put(chunk,chunk);
    }
    
    public RIFFChunk getPropertyChunk(int id) {
        if (propertyChunks == null) { 
            return null; 
        }
        RIFFChunk chunk = new RIFFChunk(type, id);
        return propertyChunks.get(chunk);
    }
    
    public Enumeration propertyChunks() {
        if (propertyChunks == null) { 
            propertyChunks = new Hashtable (); 
        }
        return propertyChunks.keys();
    }
    
    public void addCollectionChunk(RIFFChunk chunk) {
        if (collectionChunks == null) { 
            collectionChunks = new ArrayList(); 
        }
        collectionChunks.add(chunk);
    }
    
    public RIFFChunk[] getCollectionChunks(int id) {
        if (collectionChunks == null) { 
            return new RIFFChunk[0]; 
        }
        Iterator enm = collectionChunks.iterator();
        int i = 0;
        while ( enm.hasNext() ) {
            RIFFChunk chunk = enm.next();
            if (chunk.id==id) {
                i++; 
            }
        }
        RIFFChunk[] array = new RIFFChunk[i];
        i = 0;
        enm = collectionChunks.iterator();
        while ( enm.hasNext() ) {
            RIFFChunk chunk = enm.next();
            if (chunk.id==id) {
                array[i++] = chunk; 
            }
        }
        return array;
    }
    public Iterator collectionChunks() {
        if (collectionChunks == null) { 
            collectionChunks = new ArrayList(); 
        }
        return collectionChunks.iterator();
    }
    
    /**
     * Sets the data.
     * Note: The array will not be cloned.
     */
    public void setData(byte[] data) { 
        this.data = data; 
    }
    /**
     * Gets the data.
     * Note: The array will not be cloned.
     */
    public byte[] getData() { 
        return data; 
    }
    
    @Override
    public boolean equals(Object another) {
        if (another instanceof RIFFChunk) {
            RIFFChunk that = (RIFFChunk) another;
            return (that.id==this.id) && (that.type==this.type);
        }
        return false;
    }
    
    @Override
    public int hashCode() { 
        return id; 
    }
    
    public void setParserMessage(String newValue) {
        this.parserMessage = newValue;
        }
    public String getParserMessage() {
        return this.parserMessage;
        }
    
    @Override
    public String toString() {
        return super.toString()+"{"+RIFFParser.idToString(getType())+","+RIFFParser.idToString(getID())+"}";
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy