1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package entagged.audioformats.generic;
20
21 import java.io.File;
22 import java.io.UnsupportedEncodingException;
23
24 /***
25 * Contains various frequently used static functions in the different tag
26 * formats
27 *
28 * @author Raphael Slinckx
29 */
30 public class Utils {
31
32 /***
33 * Copies the bytes of <code>srd</code> to <code>dst</code> at the
34 * specified offset.
35 *
36 * @param src
37 * The byte do be copied.
38 * @param dst
39 * The array to copy to
40 * @param dstOffset
41 * The start offset for the bytes to be copied.
42 */
43 public static void copy(byte[] src, byte[] dst, int dstOffset) {
44 System.arraycopy(src, 0, dst, dstOffset, src.length);
45 }
46
47 /***
48 * Returns {@link String#getBytes()}.<br>
49 *
50 * @param s
51 * The String to call.
52 * @return The bytes.
53 */
54 public static byte[] getDefaultBytes(String s) {
55 return s.getBytes();
56 }
57
58
59
60
61
62
63
64
65 public static String getExtension(File f) {
66 String name = f.getName().toLowerCase();
67 int i = name.lastIndexOf( "." );
68 if(i == -1)
69 return "";
70
71 return name.substring( i + 1 );
72 }
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93 public static long getLongNumber(byte[] b, int start, int end) {
94 long number = 0;
95 for (int i = 0; i < (end - start + 1); i++) {
96 number += ((b[start + i] & 0xFF) << i * 8);
97 }
98
99 return number;
100 }
101
102 public static long getLongNumberBigEndian(byte[] b, int start, int end) {
103 int number = 0;
104 for (int i = 0; i < (end - start + 1); i++) {
105 number += ((b[end - i] & 0xFF) << i * 8);
106 }
107
108 return number;
109 }
110
111
112
113
114
115
116
117
118 public static int getNumber(byte[] b, int start, int end) {
119 return (int) getLongNumber(b, start, end);
120 }
121
122 public static int getNumberBigEndian(byte[] b, int start, int end) {
123 return (int) getLongNumberBigEndian(b, start, end);
124 }
125
126 public static byte[] getSizeBigEndian(int size) {
127 byte[] b = new byte[4];
128 b[0] = (byte) ((size >> 24) & 0xFF);
129 b[1] = (byte) ((size >> 16) & 0xFF);
130 b[2] = (byte) ((size >> 8) & 0xFF);
131 b[3] = (byte) (size & 0xFF);
132 return b;
133 }
134
135 public static String getString(byte[] b, int offset, int length) {
136 return new String(b, offset, length);
137 }
138
139 public static String getString(byte[] b, int offset, int length,
140 String encoding) throws UnsupportedEncodingException {
141 return new String(b, offset, length, encoding);
142 }
143
144
145
146
147
148
149
150
151 public static byte[] getUTF8Bytes(String s)
152 throws UnsupportedEncodingException {
153 return s.getBytes("UTF-8");
154 }
155 }