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

com.javanut.pronghorn.util.EncodingConverter 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.util;

import com.javanut.pronghorn.pipe.DataOutputBlobWriter;
import com.javanut.pronghorn.pipe.Pipe;
import com.javanut.pronghorn.pipe.RawDataSchema;
import com.javanut.pronghorn.util.TrieParserReader;

public class EncodingConverter {

	private final int MAX_TEXT_LENGTH = 2048;
	private final Pipe workingPipeA = RawDataSchema.instance.newPipe(3,MAX_TEXT_LENGTH);
	private final Pipe workingPipeB = RawDataSchema.instance.newPipe(3,MAX_TEXT_LENGTH);
	private final TrieParserReader templateParserReader = new TrieParserReader(true);

	public interface EncodingStorage {

		void store(Pipe pipe);

	}

	public interface EncodingTransform {

		void transform(TrieParserReader templateParserReader, DataOutputBlobWriter outputStream);

	}
    
    public EncodingConverter() {
    	
    	workingPipeA.initBuffers();
    	workingPipeB.initBuffers();
    }

	private DataOutputBlobWriter openEncoder(Pipe workingPipeOut) {
		Pipe.addMsgIdx(workingPipeOut, RawDataSchema.MSG_CHUNKEDSTREAM_1);
		DataOutputBlobWriter outputStream = Pipe.outputStream(workingPipeOut);
		DataOutputBlobWriter.openField(outputStream);
		return outputStream;
	}

	private static void setupParser(TrieParserReader templateParserReader, Pipe workingPipeIn) {
		Pipe.takeMsgIdx(workingPipeIn);
		TrieParserReader.parseSetup(templateParserReader, workingPipeIn);
	}

	private static void convertUTF8ToBytes(Pipe workingPipeIn, CharSequence route) {
		Pipe.addMsgIdx(workingPipeIn, RawDataSchema.MSG_CHUNKEDSTREAM_1);
		Pipe.addUTF8(route, workingPipeIn);
		Pipe.confirmLowLevelWrite(workingPipeIn);
		Pipe.publishWrites(workingPipeIn);
	}

	public void convert(CharSequence textBlock, EncodingTransform transform, EncodingStorage storage) {
		
		cleanInternalState();
		DataOutputBlobWriter outputStream = openTransform(textBlock);
		transform.transform(templateParserReader, outputStream);
		closeTransform(outputStream);
		storage.store(workingPipeB);
		closeStore();
		
	}

	private void closeStore() {
		Pipe.confirmLowLevelWrite(workingPipeB);
		Pipe.releaseReadLock(workingPipeB);
	}

	private void closeTransform(DataOutputBlobWriter outputStream) {
		//finished writing the converted bytes
		closeEncoder(workingPipeB, outputStream);		
		//release the input template
		Pipe.releaseReadLock(workingPipeA);
		Pipe.takeMsgIdx(workingPipeB);
	}

	private DataOutputBlobWriter openTransform(CharSequence textBlock) {
		convertUTF8ToBytes(workingPipeA, textBlock);		
		setupParser(templateParserReader, workingPipeA);
		DataOutputBlobWriter outputStream = openEncoder(workingPipeB);
		return outputStream;
	}

	private void cleanInternalState() {
		workingPipeA.reset();
		workingPipeB.reset();
	} 

	private void closeEncoder(Pipe workingPipeOut, DataOutputBlobWriter outputStream) {
		DataOutputBlobWriter.closeLowLevelField(outputStream);
		Pipe.confirmLowLevelWrite(workingPipeOut);
		Pipe.publishWrites(workingPipeOut);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy