com.javanut.pronghorn.pipe.stream.RingOutputStream Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of pronghorn-pipes Show documentation
Show all versions of pronghorn-pipes Show documentation
Ring buffer based queuing utility for applications that require high performance and/or a small
footprint. Well suited for embedded and stream based processing.
package com.javanut.pronghorn.pipe.stream;
import static com.javanut.pronghorn.pipe.Pipe.headPosition;
import static com.javanut.pronghorn.pipe.Pipe.tailPosition;
import java.io.OutputStream;
import com.javanut.pronghorn.pipe.Pipe;
import com.javanut.pronghorn.pipe.RawDataSchema;
public class RingOutputStream extends OutputStream implements AutoCloseable {
private Pipe pipe;
private int blockSize;
private byte[] oneByte = new byte[1];
public RingOutputStream(Pipe pipe) {
this.pipe = pipe;
blockSize = pipe.maxVarLen;
if (Pipe.from(pipe) != RawDataSchema.FROM) {
throw new UnsupportedOperationException("This class can only be used with the very simple RAW_BYTES catalog of messages.");
}
}
@Override
public void write(int b) {
oneByte[0] = (byte)(0xFF&b);
RingStreams.writeBytesToRing(oneByte, 0, 1, pipe, blockSize);
}
@Override
public void write(byte[] b) {
RingStreams.writeBytesToRing(b, 0, b.length, pipe, blockSize);
}
@Override
public void write(byte[] b, int off, int len) {
RingStreams.writeBytesToRing(b, off, len, pipe, blockSize);
}
@Override
public void close() {
long lastCheckedValue = tailPosition(pipe);
while (null==Pipe.slab(pipe) || lastCheckedValue < headPosition(pipe)-(1 + pipe.slabMask - Pipe.EOF_SIZE)) {
Pipe.spinWork(pipe);
lastCheckedValue = Pipe.tailPosition(pipe);
}
Pipe.publishEOF(pipe);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy