Commit 0b806600 authored by Sven Robertz's avatar Sven Robertz
Browse files

started refactoring Java EncoderChannel to use LabCommWriter + minor fixes

parent 245a4f7c
......@@ -15,7 +15,7 @@
<property name="tools" value="tools"/>
<!-- "test" is the directory where tests are located. -->
<property name="test" value="test"/>
<property name="test" value="../test"/>
<!-- "jflex" is an ant task class for the scanner generator in JFlex.jar -->
<taskdef name="jflex" classname="JFlex.anttask.JFlexTask" classpath="tools/JFlex.jar"/>
......@@ -66,7 +66,7 @@ classpath="tools/jastadd2.jar"/>
<!-- compile sources -->
<target name="test" depends="jar">
<echo message = "Running tests"/>
<exec executable="./run" dir="test">
<exec executable="./run" dir="../test">
<env key="PYTHONPATH" value="../lib/python"/>
<!--arg value="hej"/-->
</exec>
......
......@@ -6,4 +6,5 @@ extern int labcomm_fd_reader(
extern int labcomm_fd_writer(
labcomm_writer_t *r,
labcomm_writer_action_t action);
labcomm_writer_action_t action,
...);
......@@ -7,18 +7,22 @@ import java.io.OutputStream;
public class LabCommEncoderChannel implements LabCommEncoder {
private OutputStream writer;
private LabCommWriter writer;
private ByteArrayOutputStream bytes;
private DataOutputStream data;
private LabCommEncoderRegistry registry;
public LabCommEncoderChannel(OutputStream writer) {
public LabCommEncoderChannel(LabCommWriter writer) {
this.writer = writer;
bytes = new ByteArrayOutputStream();
data = new DataOutputStream(bytes);
registry = new LabCommEncoderRegistry();
}
public LabCommEncoderChannel(OutputStream writer) {
this(new WriterWrapper(writer));
}
public void register(LabCommDispatcher dispatcher) throws IOException {
int index = registry.add(dispatcher);
encodePacked32(LabComm.SAMPLE);
......@@ -37,7 +41,9 @@ public class LabCommEncoderChannel implements LabCommEncoder {
public void end(Class<? extends LabCommSample> c) throws IOException {
data.flush();
bytes.writeTo(writer);
//XXX when writer was a stream, it was probably a bit more GC efficient:
//bytes.writeTo(writer);
writer.write(bytes.toByteArray());
bytes.reset();
}
......
package se.lth.control.labcomm;
import java.io.IOException;
public interface LabCommWriter {
public void write(byte[] data);
public void write(byte[] data) throws IOException;
}
package se.lth.control.labcomm;
import java.io.OutputStream;
import java.io.IOException;
class WriterWrapper implements LabCommWriter{
private OutputStream os;
public WriterWrapper(OutputStream os) {
this.os = os;
}
public void write(byte[] data) throws IOException {
os.write(data);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment