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

com.javanut.pronghorn.pipe.ChannelReaderController 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;

public class ChannelReaderController {

	protected final Pipe pipe;
	protected boolean isReading = false;
			
	public ChannelReaderController(Pipe pipe) {
		this.pipe = pipe;
	}
		
	/**
	 * checks if there is data on the channel
	 * @return true if there is a full message to read
	 */
	public boolean hasContentToRead() {
		return Pipe.hasContentToRead(pipe);
	}
	
	/**
	 * Opens the channel reader for reading from beginning of message
	 * @return ChannelReader opened for reading or null if channel has no data
	 */
	public ChannelReader beginRead() {
		if (Pipe.hasContentToRead(pipe)) {
			Pipe.markTail(pipe);
			int msg = Pipe.takeMsgIdx(pipe);
			if (msg >= 0) {
				isReading = true;
				return Pipe.openInputStream(pipe);
			} 
		}
		return null;
	}
	
	/**
	 * Restore position to the beginning, the ChannelReader is invalidated
	 * beginRead() must be called again for another read.
	 */
	public void rollback() {
		if (isReading) {
			Pipe.resetTail(pipe);
		}
		isReading = false;
	}
	
	/**
	 * Move position forward.  ChanelReader is invalid and beginRead() must be called again.
	 */
	public void commitRead() {
		if (isReading) {
			Pipe.confirmLowLevelRead(pipe, Pipe.sizeOf(RawDataSchema.instance,RawDataSchema.MSG_CHUNKEDSTREAM_1));
			Pipe.releaseReadLock(pipe);
		}
		isReading = false;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy