use of de.lmu.ifi.dbs.elki.utilities.io.ByteBufferSerializer in project elki by elki-project.
the class BundleWriter method writeBundleStream.
/**
* Write a bundle stream to a file output channel.
*
* @param source Data source
* @param output Output channel
* @throws IOException on IO errors
*/
public void writeBundleStream(BundleStreamSource source, WritableByteChannel output) throws IOException {
ByteBuffer buffer = ByteBuffer.allocateDirect(INITIAL_BUFFER);
DBIDVar var = DBIDUtil.newVar();
ByteBufferSerializer<?>[] serializers = null;
loop: while (true) {
BundleStreamSource.Event ev = source.nextEvent();
switch(ev) {
case NEXT_OBJECT:
if (serializers == null) {
serializers = writeHeader(source, buffer, output);
}
if (serializers[0] != null) {
if (!source.assignDBID(var)) {
throw new AbortException("An object did not have an DBID assigned.");
}
DBID id = DBIDUtil.deref(var);
@SuppressWarnings("unchecked") ByteBufferSerializer<DBID> ser = (ByteBufferSerializer<DBID>) serializers[0];
int size = ser.getByteSize(id);
buffer = ensureBuffer(size, buffer, output);
ser.toByteBuffer(buffer, id);
}
for (int i = 1, j = 0; i < serializers.length; ++i, ++j) {
@SuppressWarnings("unchecked") ByteBufferSerializer<Object> ser = (ByteBufferSerializer<Object>) serializers[i];
int size = ser.getByteSize(source.data(j));
buffer = ensureBuffer(size, buffer, output);
ser.toByteBuffer(buffer, source.data(j));
}
// switch
break;
case META_CHANGED:
if (serializers != null) {
throw new AbortException("Meta changes are not supported, once the block header has been written.");
}
// switch
break;
case END_OF_STREAM:
break loop;
default:
LOG.warning("Unknown bundle stream event. API inconsistent? " + ev);
// switch
break;
}
}
if (buffer.position() > 0) {
flushBuffer(buffer, output);
}
}
Aggregations