~ubuntu-branches/ubuntu/trusty/libjaudiotagger-java/trusty

« back to all changes in this revision

Viewing changes to src/org/jaudiotagger/audio/real/RealChunk.java

  • Committer: Bazaar Package Importer
  • Author(s): Damien Raude-Morvan
  • Date: 2011-04-28 23:52:43 UTC
  • mfrom: (3.1.4 sid)
  • Revision ID: james.westby@ubuntu.com-20110428235243-pzalvw6lncis3ukf
Tags: 2.0.3-1
* d/control: Drop Depends on default-jre per Debian Java Policy as its
  a library package.
* d/watch: Fix to directly monitor SVN tags.
* Switch to 3.0 (quilt) format.
* Bump Standards-Version to 3.9.2 (no changes needed).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package org.jaudiotagger.audio.real;
2
 
 
3
 
import java.io.ByteArrayInputStream;
4
 
import java.io.DataInputStream;
5
 
import java.io.IOException;
6
 
import java.io.RandomAccessFile;
7
 
 
8
 
import org.jaudiotagger.audio.exceptions.CannotReadException;
9
 
import org.jaudiotagger.audio.generic.Utils;
10
 
 
11
 
public class RealChunk {
12
 
 
13
 
        protected static final String RMF = ".RMF";
14
 
        protected static final String PROP = "PROP";
15
 
        protected static final String MDPR = "MDPR";
16
 
        protected static final String CONT = "CONT";
17
 
        protected static final String DATA = "DATA";
18
 
        protected static final String INDX = "INDX";
19
 
 
20
 
        private final String id;
21
 
        private final int size;
22
 
        private final byte[] bytes;
23
 
 
24
 
        public static RealChunk readChunk(RandomAccessFile raf)
25
 
                        throws CannotReadException, IOException {
26
 
                final String id = Utils.readString(raf, 4);
27
 
                final int size = Utils.readUint32AsInt(raf);
28
 
                if (size < 8) {
29
 
                        throw new CannotReadException(
30
 
                                        "Corrupt file: RealAudio chunk length at position "
31
 
                                                        + (raf.getFilePointer() - 4)
32
 
                                                        + " cannot be less than 8");
33
 
                }
34
 
                if (size > (raf.length() - raf.getFilePointer() + 8)) {
35
 
                        throw new CannotReadException(
36
 
                                        "Corrupt file: RealAudio chunk length of " + size
37
 
                                                        + " at position " + (raf.getFilePointer() - 4)
38
 
                                                        + " extends beyond the end of the file");
39
 
                }
40
 
                final byte[] bytes = new byte[size - 8];
41
 
                raf.readFully(bytes);
42
 
                return new RealChunk(id, size, bytes);
43
 
        }
44
 
 
45
 
        public RealChunk(String id, int size, byte[] bytes) {
46
 
                super();
47
 
                this.id = id;
48
 
                this.size = size;
49
 
                this.bytes = bytes;
50
 
        }
51
 
 
52
 
        public DataInputStream getDataInputStream() {
53
 
                return new DataInputStream(new ByteArrayInputStream(getBytes()));
54
 
        }
55
 
 
56
 
        public boolean isCONT() {
57
 
                return CONT.equals(id);
58
 
        }
59
 
 
60
 
        public boolean isPROP() {
61
 
                return PROP.equals(id);
62
 
        }
63
 
 
64
 
        public byte[] getBytes() {
65
 
                return bytes;
66
 
        }
67
 
 
68
 
        public String getId() {
69
 
                return id;
70
 
        }
71
 
 
72
 
        public int getSize() {
73
 
                return size;
74
 
        }
75
 
 
76
 
        @Override
77
 
        public String toString() {
78
 
                return id + "\t" + size;
79
 
        }
80
 
}
 
1
package org.jaudiotagger.audio.real;
 
2
 
 
3
import java.io.ByteArrayInputStream;
 
4
import java.io.DataInputStream;
 
5
import java.io.IOException;
 
6
import java.io.RandomAccessFile;
 
7
 
 
8
import org.jaudiotagger.audio.exceptions.CannotReadException;
 
9
import org.jaudiotagger.audio.generic.Utils;
 
10
 
 
11
public class RealChunk {
 
12
 
 
13
        protected static final String RMF = ".RMF";
 
14
        protected static final String PROP = "PROP";
 
15
        protected static final String MDPR = "MDPR";
 
16
        protected static final String CONT = "CONT";
 
17
        protected static final String DATA = "DATA";
 
18
        protected static final String INDX = "INDX";
 
19
 
 
20
        private final String id;
 
21
        private final int size;
 
22
        private final byte[] bytes;
 
23
 
 
24
        public static RealChunk readChunk(RandomAccessFile raf)
 
25
                        throws CannotReadException, IOException {
 
26
                final String id = Utils.readString(raf, 4);
 
27
                final int size = Utils.readUint32AsInt(raf);
 
28
                if (size < 8) {
 
29
                        throw new CannotReadException(
 
30
                                        "Corrupt file: RealAudio chunk length at position "
 
31
                                                        + (raf.getFilePointer() - 4)
 
32
                                                        + " cannot be less than 8");
 
33
                }
 
34
                if (size > (raf.length() - raf.getFilePointer() + 8)) {
 
35
                        throw new CannotReadException(
 
36
                                        "Corrupt file: RealAudio chunk length of " + size
 
37
                                                        + " at position " + (raf.getFilePointer() - 4)
 
38
                                                        + " extends beyond the end of the file");
 
39
                }
 
40
                final byte[] bytes = new byte[size - 8];
 
41
                raf.readFully(bytes);
 
42
                return new RealChunk(id, size, bytes);
 
43
        }
 
44
 
 
45
        public RealChunk(String id, int size, byte[] bytes) {
 
46
                super();
 
47
                this.id = id;
 
48
                this.size = size;
 
49
                this.bytes = bytes;
 
50
        }
 
51
 
 
52
        public DataInputStream getDataInputStream() {
 
53
                return new DataInputStream(new ByteArrayInputStream(getBytes()));
 
54
        }
 
55
 
 
56
        public boolean isCONT() {
 
57
                return CONT.equals(id);
 
58
        }
 
59
 
 
60
        public boolean isPROP() {
 
61
                return PROP.equals(id);
 
62
        }
 
63
 
 
64
        public byte[] getBytes() {
 
65
                return bytes;
 
66
        }
 
67
 
 
68
        public String getId() {
 
69
                return id;
 
70
        }
 
71
 
 
72
        public int getSize() {
 
73
                return size;
 
74
        }
 
75
 
 
76
        @Override
 
77
        public String toString() {
 
78
                return id + "\t" + size;
 
79
        }
 
80
}