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 */ public abstract class AbstractIonList extends ArrayList 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(); }