* refactoring

This commit is contained in:
Reinhard Pointner 2009-05-12 08:57:37 +00:00
parent 63f5f4ba26
commit a6c82fc8e5
2 changed files with 40 additions and 57 deletions

View File

@ -40,9 +40,7 @@ import java.util.Map;
*
* @author TdC_VgA
*/
public class BDecoder {
private boolean recovery_mode;
class BDecoder {
private static final Charset BINARY_CHARSET = Charset.forName("ISO-8859-1");
@ -80,38 +78,27 @@ public class BDecoder {
// create a new dictionary object
Map<String, Object> tempMap = new HashMap<String, Object>();
try {
// get the key
byte[] tempByteArray = null;
// get the key
byte[] tempByteArray = null;
while ((tempByteArray = (byte[]) decodeInputStream(bais, nesting + 1)) != null) {
while ((tempByteArray = (byte[]) decodeInputStream(bais, nesting + 1)) != null) {
// decode some more
Object value = decodeInputStream(bais, nesting + 1);
// add the value to the map
CharBuffer cb = BINARY_CHARSET.decode(ByteBuffer.wrap(tempByteArray));
String key = new String(cb.array(), 0, cb.limit());
tempMap.put(key, value);
}
// decode some more
if (bais.available() < nesting)
throw (new IOException("BDecoder: invalid input data, 'e' missing from end of dictionary"));
} catch (Throwable e) {
Object value = decodeInputStream(bais, nesting + 1);
if (!recovery_mode) {
if (e instanceof IOException)
throw ((IOException) e);
throw (new IOException(e));
}
// add the value to the map
CharBuffer cb = BINARY_CHARSET.decode(ByteBuffer.wrap(tempByteArray));
String key = new String(cb.array(), 0, cb.limit());
tempMap.put(key, value);
}
if (bais.available() < nesting)
throw (new IOException("BDecoder: invalid input data, 'e' missing from end of dictionary"));
// return the map
return tempMap;
@ -119,25 +106,15 @@ public class BDecoder {
// create the list
List<Object> tempList = new ArrayList<Object>();
try {
// create the key
Object tempElement = null;
while ((tempElement = decodeInputStream(bais, nesting + 1)) != null)
// add the element
tempList.add(tempElement);
if (bais.available() < nesting)
throw (new IOException("BDecoder: invalid input data, 'e' missing from end of list"));
} catch (Throwable e) {
if (!recovery_mode) {
if (e instanceof IOException)
throw ((IOException) e);
throw (new IOException(e));
}
}
// create the key
Object tempElement = null;
while ((tempElement = decodeInputStream(bais, nesting + 1)) != null)
// add the element
tempList.add(tempElement);
if (bais.available() < nesting)
throw (new IOException("BDecoder: invalid input data, 'e' missing from end of list"));
// return the list
return tempList;
@ -246,8 +223,4 @@ public class BDecoder {
return tempArray;
}
public void setRecoveryMode(boolean r) {
recovery_mode = r;
}
}

View File

@ -42,12 +42,22 @@ public final class ExceptionUtilities {
}
public static RuntimeException asRuntimeException(Throwable t) {
if (t instanceof RuntimeException) {
return (RuntimeException) t;
@SuppressWarnings("unchecked")
public static <T extends Throwable> T wrap(Throwable t, Class<T> type) {
if (type.isInstance(t)) {
return (T) t;
}
return new RuntimeException(t);
try {
return type.getConstructor(Throwable.class).newInstance(t);
} catch (Exception e) {
throw new IllegalArgumentException(e);
}
}
public static RuntimeException asRuntimeException(Throwable t) {
return wrap(t, RuntimeException.class);
}