52 lines
1.6 KiB
Java
52 lines
1.6 KiB
Java
package org.apache.commons.beanutils.converters;
|
|
|
|
import java.util.List;
|
|
import org.apache.commons.beanutils.ConversionException;
|
|
|
|
public final class ByteArrayConverter extends AbstractArrayConverter {
|
|
public ByteArrayConverter() {
|
|
this.defaultValue = null;
|
|
this.useDefault = false;
|
|
}
|
|
|
|
public ByteArrayConverter(Object defaultValue) {
|
|
this.defaultValue = defaultValue;
|
|
this.useDefault = true;
|
|
}
|
|
|
|
private static byte[] model = new byte[0];
|
|
|
|
public Object convert(Class type, Object value) {
|
|
if (value == null) {
|
|
if (this.useDefault)
|
|
return this.defaultValue;
|
|
throw new ConversionException("No value specified");
|
|
}
|
|
if (model.getClass() == value.getClass())
|
|
return value;
|
|
if (AbstractArrayConverter.strings.getClass() == value.getClass())
|
|
try {
|
|
String[] values = (String[])value;
|
|
byte[] results = new byte[values.length];
|
|
for (int i = 0; i < values.length; i++)
|
|
results[i] = Byte.parseByte(values[i]);
|
|
return results;
|
|
} catch (Exception e) {
|
|
if (this.useDefault)
|
|
return this.defaultValue;
|
|
throw new ConversionException(value.toString(), e);
|
|
}
|
|
try {
|
|
List list = parseElements(value.toString());
|
|
byte[] results = new byte[list.size()];
|
|
for (int i = 0; i < results.length; i++)
|
|
results[i] = Byte.parseByte((String)list.get(i));
|
|
return results;
|
|
} catch (Exception e) {
|
|
if (this.useDefault)
|
|
return this.defaultValue;
|
|
throw new ConversionException(value.toString(), e);
|
|
}
|
|
}
|
|
}
|