View Javadoc

1   /*
2    * Entagged Audio Tag library
3    * Copyright (c) 2003-2005 Raphaël Slinckx <raphael@slinckx.net>
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 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  	 * Returns the extension of the given file.
59  	 * The extension is empty if there is no extension
60  	 * The extension is the string after the last "."
61  	 * 
62  	 * @param f The file whose extension is requested
63  	 * @return The extension of the given file
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  	 * Returns the extension of the given file. The extension is empty if there
76  	 * is no extension The extension is the string after the last "."
77  	 * 
78  	 * @param f The file whose extension is requested @return The extension of
79  	 * the given file&&
80  	 * 
81  	 * public static String getExtension(File f) { String name =
82  	 * f.getName().toLowerCase(); int i = name.lastIndexOf("."); if (i == -1)
83  	 * return "";
84  	 * 
85  	 * return name.substring(i + 1); }
86  	 *  /* Computes a number composed of (end-start) bytes in the b array.
87  	 * 
88  	 * @param b The byte array @param start The starting offset in b
89  	 * (b[offset]). The less significant byte @param end The end index
90  	 * (included) in b (b[end]). The most significant byte @return a long number
91  	 * represented by the byte sequence.
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 	 * same as above, but returns an int instead of a long @param b The byte
113 	 * array @param start The starting offset in b (b[offset]). The less
114 	 * significant byte @param end The end index (included) in b (b[end]). The
115 	 * most significant byte @return a int number represented by the byte
116 	 * sequence.
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 	 * Tries to convert a string into an UTF8 array of bytes If the conversion
146 	 * fails, return the string converted with the default encoding.
147 	 * 
148 	 * @param s The string to convert @return The byte array representation of
149 	 * this string in UTF8 encoding
150 	 */
151 	public static byte[] getUTF8Bytes(String s)
152 			throws UnsupportedEncodingException {
153 		return s.getBytes("UTF-8");
154 	}
155 }