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 @@ ...@@ -15,7 +15,7 @@
<property name="tools" value="tools"/> <property name="tools" value="tools"/>
<!-- "test" is the directory where tests are located. --> <!-- "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 --> <!-- "jflex" is an ant task class for the scanner generator in JFlex.jar -->
<taskdef name="jflex" classname="JFlex.anttask.JFlexTask" classpath="tools/JFlex.jar"/> <taskdef name="jflex" classname="JFlex.anttask.JFlexTask" classpath="tools/JFlex.jar"/>
...@@ -66,7 +66,7 @@ classpath="tools/jastadd2.jar"/> ...@@ -66,7 +66,7 @@ classpath="tools/jastadd2.jar"/>
<!-- compile sources --> <!-- compile sources -->
<target name="test" depends="jar"> <target name="test" depends="jar">
<echo message = "Running tests"/> <echo message = "Running tests"/>
<exec executable="./run" dir="test"> <exec executable="./run" dir="../test">
<env key="PYTHONPATH" value="../lib/python"/> <env key="PYTHONPATH" value="../lib/python"/>
<!--arg value="hej"/--> <!--arg value="hej"/-->
</exec> </exec>
......
...@@ -6,4 +6,5 @@ extern int labcomm_fd_reader( ...@@ -6,4 +6,5 @@ extern int labcomm_fd_reader(
extern int labcomm_fd_writer( extern int labcomm_fd_writer(
labcomm_writer_t *r, labcomm_writer_t *r,
labcomm_writer_action_t action); labcomm_writer_action_t action,
...);
...@@ -7,18 +7,22 @@ import java.io.OutputStream; ...@@ -7,18 +7,22 @@ import java.io.OutputStream;
public class LabCommEncoderChannel implements LabCommEncoder { public class LabCommEncoderChannel implements LabCommEncoder {
private OutputStream writer; private LabCommWriter writer;
private ByteArrayOutputStream bytes; private ByteArrayOutputStream bytes;
private DataOutputStream data; private DataOutputStream data;
private LabCommEncoderRegistry registry; private LabCommEncoderRegistry registry;
public LabCommEncoderChannel(OutputStream writer) { public LabCommEncoderChannel(LabCommWriter writer) {
this.writer = writer; this.writer = writer;
bytes = new ByteArrayOutputStream(); bytes = new ByteArrayOutputStream();
data = new DataOutputStream(bytes); data = new DataOutputStream(bytes);
registry = new LabCommEncoderRegistry(); registry = new LabCommEncoderRegistry();
} }
public LabCommEncoderChannel(OutputStream writer) {
this(new WriterWrapper(writer));
}
public void register(LabCommDispatcher dispatcher) throws IOException { public void register(LabCommDispatcher dispatcher) throws IOException {
int index = registry.add(dispatcher); int index = registry.add(dispatcher);
encodePacked32(LabComm.SAMPLE); encodePacked32(LabComm.SAMPLE);
...@@ -37,7 +41,9 @@ public class LabCommEncoderChannel implements LabCommEncoder { ...@@ -37,7 +41,9 @@ public class LabCommEncoderChannel implements LabCommEncoder {
public void end(Class<? extends LabCommSample> c) throws IOException { public void end(Class<? extends LabCommSample> c) throws IOException {
data.flush(); 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(); bytes.reset();
} }
......
package se.lth.control.labcomm; package se.lth.control.labcomm;
import java.io.IOException;
public interface LabCommWriter { 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);
}
}
Supports Markdown
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