Rogue: Savage Rats, a retro-themed dungeon crawler
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
rogue-savage-rats/src/mightypork/util/files/ion/AbstractIonList.java

81 lines
1.6 KiB

package mightypork.util.files.ion;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
/**
* Ionizable Arraylist
*
* @author MightyPork
* @param <T>
*/
public abstract class AbstractIonList<T> extends ArrayList<T> implements Ionizable {
@Override
public void ionRead(InputStream in) throws IonException
{
try {
while (true) {
final byte b = BinaryUtils.readByte(in);
if (b == IonMarks.ENTRY) {
final T value = (T) Ion.readObject(in);
add(value);
} else if (b == IonMarks.END) {
break;
} else {
throw new IonException("Unexpected mark in IonList: " + Integer.toHexString(b));
}
}
ionReadCustomData(in);
} catch (final IOException e) {
throw new IonException("Error reading ion list", e);
}
}
@Override
public void ionWrite(OutputStream out) throws IonException
{
try {
for (final T entry : this) {
if (entry instanceof IonizableOptional && !((IonizableOptional) entry).ionShouldSave()) continue;
BinaryUtils.writeByte(out, IonMarks.ENTRY);
Ion.writeObject(out, entry);
}
BinaryUtils.writeByte(out, IonMarks.END);
ionWriteCustomData(out);
} catch (final IOException e) {
throw new IonException("Error reading ion map", e);
}
}
/**
* Read custom data of this AbstractIonList implementation
*
* @param in input stream
*/
public void ionReadCustomData(InputStream in)
{
}
/**
* Write custom data of this AbstractIonList implementation
*
* @param out output stream
*/
public void ionWriteCustomData(OutputStream out)
{
}
@Override
public abstract byte ionMark();
}