-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRIFFChunk.java
More file actions
187 lines (171 loc) · 4.9 KB
/
RIFFChunk.java
File metadata and controls
187 lines (171 loc) · 4.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
* @(#)RIFFChunk.java
*
* Copyright (c) 2005-2012 Werner Randelshofer, Immensee, 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.
*/
import java.util.*;
/**
* RIFF Chunks form the building blocks of a RIFF file.
*
* @author Werner Randelshofer, Hausmatt 10, CH-6405 Immensee, Switzerland
* @version $Id: RIFFChunk.java 186 2012-03-28 11:18:42Z werner $
*/
public class RIFFChunk {
private int id;
private int type;
private long size;
private long scan;
private byte[] data;
private Hashtable<RIFFChunk,RIFFChunk> propertyChunks;
private ArrayList<RIFFChunk> 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<RIFFChunk,RIFFChunk>(propGroup.propertyChunks);
}
if (propGroup.collectionChunks != null) {
collectionChunks = new ArrayList<RIFFChunk>(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<RIFFChunk,RIFFChunk> ();
}
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<RIFFChunk,RIFFChunk> ();
}
return propertyChunks.keys();
}
public void addCollectionChunk(RIFFChunk chunk) {
if (collectionChunks == null) {
collectionChunks = new ArrayList<RIFFChunk>();
}
collectionChunks.add(chunk);
}
public RIFFChunk[] getCollectionChunks(int id) {
if (collectionChunks == null) {
return new RIFFChunk[0];
}
Iterator<RIFFChunk> 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<RIFFChunk>();
}
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())+"}";
}
}