slcom/slangc/sdk/SimpleBytecodeOutput.sauce

55 lines
1.2 KiB
Plaintext

package slangc.sdk;
import slang.streams.SyncOutput;
import slangc.streams.ArrayOutput;
import slangc.streams.FileOutput;
import slangc.api.BytecodeOutput;
public class SimpleBytecodeOutput extends BytecodeOutput {
private SyncOutput<byte> out;
public SimpleBytecodeOutput() {
this.out = new ArrayOutput();
}
public SimpleBytecodeOutput(FileOutput out) {
//try {
this.out = out; //new FileOutput(outname);
//} catch (Error e) {
// throw new Error("Couldn't open output stream", e);
//}
}
public SimpleBytecodeOutput(SyncOutput<byte> out) {
this.out = out;
}
@Override
public void processByte(byte b) {
//byte[] buf = new byte[1];
//int tmp = b + 0;
//buf[0] = (byte) tmp; // TODO: More thorough type-checking??
try {
if (out.writeBuffer(new byte[]{b}, 0, 1) != 1) {
throw new Error("Failed to write byte to output stream");
}
} catch (Error e) {
throw new Error("Couldn't write to output stream", e);
}
}
@Override
public void endOfFile() {
try {
out.close();
} catch (Error e) {
throw new Error("Couldn't close output stream", e);
}
out = null;
}
public SyncOutput<byte> getOut() {
return out;
}
}