| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| Chunk |
|
| 2.2;2.2 |
| 1 | /* | |
| 2 | * Entagged Audio Tag library | |
| 3 | * Copyright (c) 2004-2005 Christian Laireiter <liree@web.de> | |
| 4 | * | |
| 5 | * This library is free software; you can redistribute it and/or | |
| 6 | * modify it under the terms of the GNU Lesser General Public | |
| 7 | * License as published by the Free Software Foundation; either | |
| 8 | * version 2.1 of the License, or (at your option) any later version. | |
| 9 | * | |
| 10 | * This library is distributed in the hope that it will be useful, | |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 13 | * Lesser General Public License for more details. | |
| 14 | * | |
| 15 | * You should have received a copy of the GNU Lesser General Public | |
| 16 | * License along with this library; if not, write to the Free Software | |
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
| 18 | */ | |
| 19 | package org.jaudiotagger.audio.asf.data; | |
| 20 | ||
| 21 | import org.jaudiotagger.audio.asf.util.Utils; | |
| 22 | ||
| 23 | import java.math.BigInteger; | |
| 24 | ||
| 25 | /** | |
| 26 | * This class represents a chunk within ASF streams. <br> | |
| 27 | * Each chunk starts with a 16byte {@linkplain GUID GUID} identifying the type. | |
| 28 | * After that a number (represented by 8 bytes) follows which shows the size in | |
| 29 | * bytes of the chunk. Finally there is the data of the chunk. | |
| 30 | * | |
| 31 | * @author Christian Laireiter | |
| 32 | */ | |
| 33 | public class Chunk { | |
| 34 | ||
| 35 | /** | |
| 36 | * The length of current chunk. <br> | |
| 37 | */ | |
| 38 | protected final BigInteger chunkLength; | |
| 39 | ||
| 40 | /** | |
| 41 | * The GUID of represented chunk header. | |
| 42 | */ | |
| 43 | protected final GUID guid; | |
| 44 | ||
| 45 | /** | |
| 46 | * The position of current header object within file or stream. | |
| 47 | */ | |
| 48 | protected long position; | |
| 49 | ||
| 50 | /** | |
| 51 | * Creates an instance | |
| 52 | * | |
| 53 | * @param headerGuid | |
| 54 | * The GUID of header object. | |
| 55 | * @param chunkLen | |
| 56 | * Length of current chunk. | |
| 57 | */ | |
| 58 | 724 | public Chunk(final GUID headerGuid, final BigInteger chunkLen) { |
| 59 | 724 | if (headerGuid == null) { |
| 60 | 0 | throw new IllegalArgumentException("GUID must not be null."); |
| 61 | } | |
| 62 | 724 | if (chunkLen == null || chunkLen.compareTo(BigInteger.ZERO) < 0) { |
| 63 | 0 | throw new IllegalArgumentException( |
| 64 | "chunkLen must not be null nor negative."); | |
| 65 | } | |
| 66 | 724 | this.guid = headerGuid; |
| 67 | 724 | this.chunkLength = chunkLen; |
| 68 | 724 | } |
| 69 | ||
| 70 | /** | |
| 71 | * Creates an instance | |
| 72 | * | |
| 73 | * @param headerGuid | |
| 74 | * The GUID of header object. | |
| 75 | * @param pos | |
| 76 | * Position of header object within stream or file. | |
| 77 | * @param chunkLen | |
| 78 | * Length of current chunk. | |
| 79 | */ | |
| 80 | public Chunk(final GUID headerGuid, final long pos, | |
| 81 | 5627 | final BigInteger chunkLen) { |
| 82 | 5627 | if (headerGuid == null) { |
| 83 | 4 | throw new IllegalArgumentException("GUID must not be null"); |
| 84 | } | |
| 85 | 5623 | if (pos < 0) { |
| 86 | 24 | throw new IllegalArgumentException( |
| 87 | "Position of header can't be negative."); | |
| 88 | } | |
| 89 | 5599 | if (chunkLen == null || chunkLen.compareTo(BigInteger.ZERO) < 0) { |
| 90 | 80 | throw new IllegalArgumentException( |
| 91 | "chunkLen must not be null nor negative."); | |
| 92 | } | |
| 93 | 5519 | this.guid = headerGuid; |
| 94 | 5519 | this.position = pos; |
| 95 | 5519 | this.chunkLength = chunkLen; |
| 96 | 5519 | } |
| 97 | ||
| 98 | /** | |
| 99 | * This method returns the End of the current chunk introduced by current | |
| 100 | * header object. | |
| 101 | * | |
| 102 | * @return Position after current chunk. | |
| 103 | * @deprecated typo, use {@link #getChunkEnd()} instead. | |
| 104 | */ | |
| 105 | @Deprecated | |
| 106 | public long getChunckEnd() { | |
| 107 | 0 | return this.position + this.chunkLength.longValue(); |
| 108 | } | |
| 109 | ||
| 110 | /** | |
| 111 | * This method returns the End of the current chunk introduced by current | |
| 112 | * header object. | |
| 113 | * | |
| 114 | * @return Position after current chunk. | |
| 115 | */ | |
| 116 | public long getChunkEnd() { | |
| 117 | 9832 | return this.position + this.chunkLength.longValue(); |
| 118 | } | |
| 119 | ||
| 120 | /** | |
| 121 | * @return Returns the chunkLength. | |
| 122 | */ | |
| 123 | public BigInteger getChunkLength() { | |
| 124 | 60 | return this.chunkLength; |
| 125 | } | |
| 126 | ||
| 127 | /** | |
| 128 | * @return Returns the guid. | |
| 129 | */ | |
| 130 | public GUID getGuid() { | |
| 131 | 5653 | return this.guid; |
| 132 | } | |
| 133 | ||
| 134 | /** | |
| 135 | * @return Returns the position. | |
| 136 | */ | |
| 137 | public long getPosition() { | |
| 138 | 7453 | return this.position; |
| 139 | } | |
| 140 | ||
| 141 | /** | |
| 142 | * This method creates a String containing useful information prepared to be | |
| 143 | * printed on STD-OUT. <br> | |
| 144 | * This method is intended to be overwritten by inheriting classes. | |
| 145 | * | |
| 146 | * @param prefix | |
| 147 | * each line gets this string prepended. | |
| 148 | * | |
| 149 | * @return Information of current Chunk Object. | |
| 150 | */ | |
| 151 | public String prettyPrint(final String prefix) { | |
| 152 | 4 | final StringBuilder result = new StringBuilder(); |
| 153 | 4 | result.append(prefix).append("-> GUID: ").append( |
| 154 | GUID.getGuidDescription(this.guid)) | |
| 155 | .append(Utils.LINE_SEPARATOR); | |
| 156 | 4 | result.append(prefix).append(" | : Starts at position: ").append( |
| 157 | getPosition()).append(Utils.LINE_SEPARATOR); | |
| 158 | 4 | result.append(prefix).append(" | : Last byte at: ").append( |
| 159 | getChunkEnd() - 1).append(Utils.LINE_SEPARATOR); | |
| 160 | 4 | return result.toString(); |
| 161 | } | |
| 162 | ||
| 163 | /** | |
| 164 | * Sets the position. | |
| 165 | * | |
| 166 | * @param pos | |
| 167 | * position to set. | |
| 168 | */ | |
| 169 | public void setPosition(final long pos) { | |
| 170 | 744 | this.position = pos; |
| 171 | 744 | } |
| 172 | ||
| 173 | /** | |
| 174 | * (overridden) | |
| 175 | * | |
| 176 | * @see java.lang.Object#toString() | |
| 177 | */ | |
| 178 | @Override | |
| 179 | public String toString() { | |
| 180 | 4 | return prettyPrint(""); |
| 181 | } | |
| 182 | ||
| 183 | } |