All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.javanut.pronghorn.pipe.stream.RingOutputStream Maven / Gradle / Ivy

Go to download

Ring buffer based queuing utility for applications that require high performance and/or a small footprint. Well suited for embedded and stream based processing.

There is a newer version: 1.1.27
Show newest version
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