1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package entagged.audioformats.asf.data;
20
21 import java.math.BigInteger;
22
23 /***
24 * This class represents a chunk within asf streams. <br>
25 * Each chunk starts with a 16byte guid identifying the type. After that a
26 * number (represented by 8 bytes) follows which shows the size in bytes of the
27 * chunk. Finally there is the data of the chunk.
28 *
29 * @author Christian Laireiter
30 */
31 public class Chunk {
32
33 /***
34 * The length of current chunk. <br>
35 */
36 protected final BigInteger chunkLength;
37
38 /***
39 * The guid of represented chunk header.
40 */
41 protected final GUID guid;
42
43 /***
44 * The position of current header object within file or stream.
45 */
46 protected final long position;
47
48 /***
49 * Creates an instance
50 *
51 * @param headerGuid
52 * The GUID of header object.
53 * @param pos
54 * Position of header object within stream or file.
55 * @param chunkLen
56 * Length of current chunk.
57 */
58 public Chunk(GUID headerGuid, long pos, BigInteger chunkLen) {
59 if (headerGuid == null) {
60 throw new IllegalArgumentException(
61 "GUID must not be null nor anything else than "
62 + GUID.GUID_LENGTH + " entries long.");
63 }
64 if (pos < 0) {
65 throw new IllegalArgumentException(
66 "Position of header can't be negative.");
67 }
68 if (chunkLen == null || chunkLen.compareTo(BigInteger.ZERO) < 0) {
69 throw new IllegalArgumentException(
70 "chunkLen must not be null nor negative.");
71 }
72 this.guid = headerGuid;
73 this.position = pos;
74 this.chunkLength = chunkLen;
75 }
76
77 /***
78 * This method returns the End of the current chunk introduced by current
79 * header object.
80 *
81 * @return Position after current chunk.
82 */
83 public long getChunckEnd() {
84 return position + chunkLength.longValue();
85 }
86
87 /***
88 * @return Returns the chunkLength.
89 */
90 public BigInteger getChunkLength() {
91 return chunkLength;
92 }
93
94 /***
95 * @return Returns the guid.
96 */
97 public GUID getGuid() {
98 return guid;
99 }
100
101 /***
102 * @return Returns the position.
103 */
104 public long getPosition() {
105 return position;
106 }
107
108 /***
109 * This method creates a String containing usefull information prepared to
110 * be printed on stdout. <br>
111 * This method is intended to be overwritten by inheriting classes.
112 *
113 * @return Information of current Chunk Object.
114 */
115 public String prettyPrint() {
116 StringBuffer result = new StringBuffer();
117 result.append("GUID: " + GUID.getGuidDescription(guid));
118 result.append("\n Starts at position: " + getPosition() + "\n");
119 result.append(" Last byte at: " + (getChunckEnd() - 1) + "\n\n");
120 return result.toString();
121 }
122
123 /***
124 * (overridden)
125 *
126 * @see java.lang.Object#toString()
127 */
128 public String toString() {
129 return prettyPrint();
130 }
131
132 }