📄 directory.java
字号:
* @return the tag's value as an array of Strings
* @throws MetadataException if the tag has not been set or cannot be represented
* as a String[]
*/
public String[] getStringArray(int tagType) throws MetadataException
{
Object o = getObject(tagType);
if (o==null) {
throw new MetadataException("Tag " + getTagName(tagType) + " has not been set -- check using containsTag() first");
} else if (o instanceof String[]) {
return (String[])o;
} else if (o instanceof String) {
String[] strings = {(String)o};
return strings;
} else if (o instanceof int[]) {
int[] ints = (int[])o;
String[] strings = new String[ints.length];
for (int i = 0; i<strings.length; i++) {
strings[i] = Integer.toString(ints[i]);
}
return strings;
} else if (o instanceof byte[]) {
byte[] bytes = (byte[])o;
String[] strings = new String[bytes.length];
for (int i = 0; i<strings.length; i++) {
strings[i] = Byte.toString(bytes[i]);
}
return strings;
} else if (o instanceof Rational[]) {
Rational[] rationals = (Rational[])o;
String[] strings = new String[rationals.length];
for (int i = 0; i<strings.length; i++) {
strings[i] = rationals[i].toSimpleString(false);
}
return strings;
}
throw new MetadataException("Tag '" + tagType + "' cannot be cast to an String array. It is of type '" + o.getClass() + "'.");
}
/**
* Gets the specified tag's value as an int array, if possible. Only supported
* where the tag is set as String, int[], byte[] or Rational[].
* @param tagType the tag identifier
* @return the tag's value as an int array
* @throws MetadataException if the tag has not been set, or cannot be converted to
* an int array
*/
public int[] getIntArray(int tagType) throws MetadataException
{
Object o = getObject(tagType);
if (o==null) {
throw new MetadataException("Tag " + getTagName(tagType) + " has not been set -- check using containsTag() first");
} else if (o instanceof Rational[]) {
Rational[] rationals = (Rational[])o;
int[] ints = new int[rationals.length];
for (int i = 0; i<ints.length; i++) {
ints[i] = rationals[i].intValue();
}
return ints;
} else if (o instanceof int[]) {
return (int[])o;
} else if (o instanceof byte[]) {
byte[] bytes = (byte[])o;
int[] ints = new int[bytes.length];
for (int i = 0; i<bytes.length; i++) {
byte b = bytes[i];
ints[i] = b;
}
return ints;
} else if (o instanceof String) {
String str = (String)o;
int[] ints = new int[str.length()];
for (int i = 0; i<str.length(); i++) {
ints[i] = str.charAt(i);
}
return ints;
}
throw new MetadataException("Tag '" + tagType + "' cannot be cast to an int array. It is of type '" + o.getClass() + "'.");
}
/**
* Gets the specified tag's value as an byte array, if possible. Only supported
* where the tag is set as String, int[], byte[] or Rational[].
* @param tagType the tag identifier
* @return the tag's value as a byte array
* @throws MetadataException if the tag has not been set, or cannot be converted to
* a byte array
*/
public byte[] getByteArray(int tagType) throws MetadataException
{
Object o = getObject(tagType);
if (o==null) {
throw new MetadataException("Tag " + getTagName(tagType) + " has not been set -- check using containsTag() first");
} else if (o instanceof Rational[]) {
Rational[] rationals = (Rational[])o;
byte[] bytes = new byte[rationals.length];
for (int i = 0; i<bytes.length; i++) {
bytes[i] = rationals[i].byteValue();
}
return bytes;
} else if (o instanceof byte[]) {
return (byte[])o;
} else if (o instanceof int[]) {
int[] ints = (int[])o;
byte[] bytes = new byte[ints.length];
for (int i = 0; i<ints.length; i++) {
bytes[i] = (byte)ints[i];
}
return bytes;
} else if (o instanceof String) {
String str = (String)o;
byte[] bytes = new byte[str.length()];
for (int i = 0; i<str.length(); i++) {
bytes[i] = (byte)str.charAt(i);
}
return bytes;
}
throw new MetadataException("Tag '" + tagType + "' cannot be cast to a byte array. It is of type '" + o.getClass() + "'.");
}
/**
* Returns the specified tag's value as a double, if possible.
*/
public double getDouble(int tagType) throws MetadataException
{
Object o = getObject(tagType);
if (o==null) {
throw new MetadataException("Tag " + getTagName(tagType) + " has not been set -- check using containsTag() first");
} else if (o instanceof String) {
try {
return Double.parseDouble((String)o);
} catch (NumberFormatException nfe) {
throw new MetadataException("unable to parse string " + o + " as a double", nfe);
}
} else if (o instanceof Number) {
return ((Number)o).doubleValue();
}
throw new MetadataException("Tag '" + tagType + "' cannot be cast to a double. It is of type '" + o.getClass() + "'.");
}
/**
* Returns the specified tag's value as a float, if possible.
*/
public float getFloat(int tagType) throws MetadataException
{
Object o = getObject(tagType);
if (o==null) {
throw new MetadataException("Tag " + getTagName(tagType) + " has not been set -- check using containsTag() first");
} else if (o instanceof String) {
try {
return Float.parseFloat((String)o);
} catch (NumberFormatException nfe) {
throw new MetadataException("unable to parse string " + o + " as a float", nfe);
}
} else if (o instanceof Number) {
return ((Number)o).floatValue();
}
throw new MetadataException("Tag '" + tagType + "' cannot be cast to a float. It is of type '" + o.getClass() + "'.");
}
/**
* Returns the specified tag's value as a long, if possible.
*/
public long getLong(int tagType) throws MetadataException
{
Object o = getObject(tagType);
if (o==null) {
throw new MetadataException("Tag " + getTagName(tagType) + " has not been set -- check using containsTag() first");
} else if (o instanceof String) {
try {
return Long.parseLong((String)o);
} catch (NumberFormatException nfe) {
throw new MetadataException("unable to parse string " + o + " as a long", nfe);
}
} else if (o instanceof Number) {
return ((Number)o).longValue();
}
throw new MetadataException("Tag '" + tagType + "' cannot be cast to a long. It is of type '" + o.getClass() + "'.");
}
/**
* Returns the specified tag's value as a boolean, if possible.
*/
public boolean getBoolean(int tagType) throws MetadataException
{
Object o = getObject(tagType);
if (o==null) {
throw new MetadataException("Tag " + getTagName(tagType) + " has not been set -- check using containsTag() first");
} else if (o instanceof Boolean) {
return ((Boolean)o).booleanValue();
} else if (o instanceof String) {
try {
return Boolean.getBoolean((String)o);
} catch (NumberFormatException nfe) {
throw new MetadataException("unable to parse string " + o + " as a boolean", nfe);
}
} else if (o instanceof Number) {
return (((Number)o).doubleValue()!=0);
}
throw new MetadataException("Tag '" + tagType + "' cannot be cast to a boolean. It is of type '" + o.getClass() + "'.");
}
/**
* Returns the specified tag's value as a java.util.Date, if possible.
*/
public java.util.Date getDate(int tagType) throws MetadataException
{
Object o = getObject(tagType);
if (o==null) {
throw new MetadataException("Tag " + getTagName(tagType) + " has not been set -- check using containsTag() first");
} else if (o instanceof java.util.Date) {
return (java.util.Date)o;
} else if (o instanceof String) {
// add new dateformat strings to make this method even smarter
// so far, this seems to cover all known date strings
// (for example, AM and PM strings are not supported...)
String datePatterns[] = {
"yyyy:MM:dd HH:mm:ss",
"yyyy:MM:dd HH:mm",
"yyyy-MM-dd HH:mm:ss",
"yyyy-MM-dd HH:mm"};
String dateString = (String)o;
for (int i = 0; i<datePatterns.length; i++) {
try {
DateFormat parser = new java.text.SimpleDateFormat(datePatterns[i]);
return parser.parse(dateString);
} catch (java.text.ParseException ex) {
// simply try the next pattern
}
}
}
throw new MetadataException("Tag '" + tagType + "' cannot be cast to a java.util.Date. It is of type '" + o.getClass() + "'.");
}
/**
* Returns the specified tag's value as a Rational, if possible.
*/
public Rational getRational(int tagType) throws MetadataException
{
Object o = getObject(tagType);
if (o==null) {
throw new MetadataException("Tag " + getTagName(tagType) + " has not been set -- check using containsTag() first");
} else if (o instanceof Rational) {
return (Rational)o;
}
throw new MetadataException("Tag '" + tagType + "' cannot be cast to a Rational. It is of type '" + o.getClass() + "'.");
}
public Rational[] getRationalArray(int tagType) throws MetadataException
{
Object o = getObject(tagType);
if (o==null) {
throw new MetadataException("Tag " + getTagName(tagType) + " has not been set -- check using containsTag() first");
} else if (o instanceof Rational[]) {
return (Rational[])o;
}
throw new MetadataException("Tag '" + tagType + "' cannot be cast to a Rational array. It is of type '" + o.getClass() + "'.");
}
/**
* Returns the specified tag's value as a String. This value is the 'raw' value. A more presentable decoding
* of this value may be obtained from the corresponding Descriptor.
* @return the String reprensentation of the tag's value, or
* <code>null</code> if the tag hasn't been defined.
*/
public String getString(int tagType)
{
Object o = getObject(tagType);
if (o==null)
return null;
if (o instanceof Rational)
return ((Rational)o).toSimpleString(true);
if (o.getClass().isArray())
{
// handle arrays of objects and primitives
int arrayLength = Array.getLength(o);
// determine if this is an array of objects i.e. [Lcom.drew.blah
boolean isObjectArray = o.getClass().toString().startsWith("class [L");
StringBuffer sbuffer = new StringBuffer();
for (int i = 0; i<arrayLength; i++)
{
if (i!=0)
sbuffer.append(' ');
if (isObjectArray)
sbuffer.append(Array.get(o, i).toString());
else
sbuffer.append(Array.getInt(o, i));
}
return sbuffer.toString();
}
return o.toString();
}
/**
* Returns the object hashed for the particular tag type specified, if available.
* @param tagType the tag type identifier
* @return the tag's value as an Object if available, else null
*/
public Object getObject(int tagType)
{
return _tagMap.get(new Integer(tagType));
}
// OTHER METHODS
/**
* Returns the name of a specified tag as a String.
* @param tagType the tag type identifier
* @return the tag's name as a String
*/
public String getTagName(int tagType)
{
Integer key = new Integer(tagType);
HashMap nameMap = getTagNameMap();
if (!nameMap.containsKey(key)) {
String hex = Integer.toHexString(tagType);
while (hex.length()<4) {
hex = "0" + hex;
}
return "Unknown tag (0x" + hex + ")";
}
return (String)nameMap.get(key);
}
/**
* Provides a description of a tag's value using the descriptor set by
* <code>setDescriptor(Descriptor)</code>.
* @param tagType the tag type identifier
* @return the tag value's description as a String
* @throws MetadataException if a descriptor hasn't been set, or if an error
* occurs during calculation of the description within the Descriptor
*/
public String getDescription(int tagType) throws MetadataException
{
if (_descriptor==null) {
throw new MetadataException("a descriptor must be set using setDescriptor(...) before descriptions can be provided");
}
return _descriptor.getDescription(tagType);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -