Skip to content

Commit

Permalink
Avro reader and writer
Browse files Browse the repository at this point in the history
  • Loading branch information
fvilla committed Dec 8, 2024
1 parent 44a062c commit c1e669b
Showing 1 changed file with 22 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package org.integratedmodelling.common.data;

import org.apache.avro.io.DecoderFactory;
import org.apache.avro.io.EncoderFactory;
import org.apache.avro.specific.SpecificDatumReader;
import org.apache.avro.specific.SpecificDatumWriter;
import org.integratedmodelling.klab.api.data.Data;
import org.integratedmodelling.klab.api.data.Metadata;
import org.integratedmodelling.klab.api.exceptions.KlabIOException;
import org.integratedmodelling.klab.api.knowledge.Artifact;
import org.integratedmodelling.klab.api.knowledge.Concept;
import org.integratedmodelling.klab.api.knowledge.observation.scale.Scale;
import org.integratedmodelling.klab.api.services.runtime.Notification;
import org.integratedmodelling.klab.common.data.Instance;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
Expand All @@ -23,10 +28,16 @@
*/
public class DataImpl implements Data {

private Instance instance;
private final Instance instance;

public DataImpl(InputStream inputStream) {
// TODO
try {
var decoder = DecoderFactory.get().binaryDecoder(inputStream, null);
var reader = new SpecificDatumReader<>(Instance.class);
this.instance = reader.read(null, decoder);
} catch (IOException e) {
throw new KlabIOException(e);
}
}

public DataImpl(Instance instance) {
Expand All @@ -39,7 +50,7 @@ public DataImpl(Instance instance) {

@Override
public boolean isEmpty() {
return false;
return instance == null; /* TODO need to check the instance */
}

@Override
Expand Down Expand Up @@ -88,7 +99,14 @@ public Metadata getMetadata() {
}

public void copyTo(OutputStream dataStream) {
var encoder = EncoderFactory.get().binaryEncoder(dataStream, null);
try {
var encoder = EncoderFactory.get().binaryEncoder(dataStream, null);
var writer = new SpecificDatumWriter<>(Instance.class);
writer.write(this.instance, encoder);
encoder.flush();
} catch (IOException e) {
throw new KlabIOException(e);
}
}

}

0 comments on commit c1e669b

Please sign in to comment.