| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| WriteableChunkModifer |
|
| 2.3333333333333335;2.333 |
| 1 | package org.jaudiotagger.audio.asf.io; | |
| 2 | ||
| 3 | import org.jaudiotagger.audio.asf.data.GUID; | |
| 4 | import org.jaudiotagger.audio.asf.util.Utils; | |
| 5 | ||
| 6 | import java.io.IOException; | |
| 7 | import java.io.InputStream; | |
| 8 | import java.io.OutputStream; | |
| 9 | ||
| 10 | /** | |
| 11 | * A chunk modifier which works with information provided by | |
| 12 | * {@link WriteableChunk} objects.<br> | |
| 13 | * | |
| 14 | * @author Christian Laireiter | |
| 15 | */ | |
| 16 | 4 | public class WriteableChunkModifer implements ChunkModifier { |
| 17 | ||
| 18 | /** | |
| 19 | * The chunk to write. | |
| 20 | */ | |
| 21 | private final WriteableChunk writableChunk; | |
| 22 | ||
| 23 | /** | |
| 24 | * Creates an instance.<br> | |
| 25 | * | |
| 26 | * @param chunk | |
| 27 | * chunk to write | |
| 28 | */ | |
| 29 | 697 | public WriteableChunkModifer(final WriteableChunk chunk) { |
| 30 | 697 | this.writableChunk = chunk; |
| 31 | 697 | } |
| 32 | ||
| 33 | /** | |
| 34 | * {@inheritDoc} | |
| 35 | */ | |
| 36 | public boolean isApplicable(final GUID guid) { | |
| 37 | 2978 | return guid.equals(this.writableChunk.getGuid()); |
| 38 | } | |
| 39 | ||
| 40 | /** | |
| 41 | * {@inheritDoc} | |
| 42 | */ | |
| 43 | public ModificationResult modify(final GUID guid, final InputStream chunk, | |
| 44 | OutputStream destination) throws IOException { // NOPMD by Christian Laireiter on 5/9/09 5:03 PM | |
| 45 | 697 | int chunkDiff = 0; |
| 46 | 697 | long newSize = 0; |
| 47 | 697 | long oldSize = 0; |
| 48 | /* | |
| 49 | * Replace the outputstream with the counting one, only if assert's are | |
| 50 | * evaluated. | |
| 51 | */ | |
| 52 | 697 | assert (destination = new CountingOutputstream(destination)) != null; |
| 53 | 697 | if (!this.writableChunk.isEmpty()) { |
| 54 | 216 | newSize = this.writableChunk.writeInto(destination); |
| 55 | 216 | assert newSize == this.writableChunk.getCurrentAsfChunkSize(); |
| 56 | /* | |
| 57 | * If assert's are evaluated, we have replaced destination by a | |
| 58 | * CountingOutpustream and can now verify if | |
| 59 | * getCurrentAsfChunkSize() really works correctly. | |
| 60 | */ | |
| 61 | 216 | assert ((CountingOutputstream) destination).getCount() == newSize; |
| 62 | 216 | if (guid == null) { |
| 63 | 76 | chunkDiff++; |
| 64 | } | |
| 65 | ||
| 66 | } | |
| 67 | 697 | if (guid != null) { |
| 68 | 224 | assert isApplicable(guid); |
| 69 | 224 | if (this.writableChunk.isEmpty()) { |
| 70 | 84 | chunkDiff--; |
| 71 | } | |
| 72 | 224 | oldSize = Utils.readUINT64(chunk); |
| 73 | 224 | chunk.skip(oldSize - 24); |
| 74 | } | |
| 75 | 697 | return new ModificationResult(chunkDiff, (newSize - oldSize), guid); |
| 76 | } | |
| 77 | ||
| 78 | } |