HomeForumSourceResearchGuide
Sign in to contribute to source. how it works
Component data.StreamBuffer by barry
expand copy to clipboardexpand
data Chunk {
	byte content[]
	Chunk next
	}

component provides StreamBuffer(AdaptEvents) {
	
	int totalLen
	int chunkStart
	
	Chunk chunks
	Chunk lastChunk
	
	void copyBytes(byte dest[], int destOffset, byte src[], int srcOffset, int len)
		{
		for (int i = 0; i < len; i++)
			{
			dest[destOffset+i] 
			= src[srcOffset+i]
			}
		}
	
	void StreamBuffer:write(byte ar[], opt int len)
		{
		Chunk newChunk = new Chunk()
		
		if (isset len)
			{
			newChunk.content = new byte[len]
			copyBytes(newChunk.content, 0, ar, 0, len)
			}
			else
			{
			newChunk.content = ar
			}
		
		if (lastChunk == null)
			chunks = newChunk
			else
			chunks.next = newChunk
		
		lastChunk = newChunk
		
		totalLen += newChunk.content.arrayLength
		}
	
	byte[] StreamBuffer:read(int len)
		{
		byte result[]
		if (len > totalLen)
			result = new byte[totalLen]
			else
			result = new byte[len]
		
		int sf = 0
		int rem = result.arrayLength
		
		while (sf < result.arrayLength)
			{
			int rdAmt = rem
			
			if (rdAmt > (chunks.content.arrayLength - chunkStart))
				rdAmt = chunks.content.arrayLength - chunkStart
			
			copyBytes(result, sf, chunks.content, chunkStart, rdAmt)
			
			sf += rdAmt
			rem -= rdAmt
			chunkStart += rdAmt
			
			if (chunkStart == chunks.content.arrayLength)
				{
				chunks = chunks.next
				chunkStart = 0
				
				if (chunks == null)
					lastChunk = null
				}
			
			if (sf == result.arrayLength) break
			}
		
		totalLen -= result.arrayLength
		
		return result
		}
	
	int StreamBuffer:getSize()
		{
		return totalLen
		}
	
	void AdaptEvents:active()
		{
		chunks = null
		lastChunk = null
		
		write(bufferContent)
		totalLen = bufferContent.arrayLength
		
		bufferContent = null
		}
	
	void AdaptEvents:inactive()
		{
		byte ar[] = new byte[totalLen]
		int offset = 0
		
		Chunk cw = chunks
		while (cw != null)
			{
			copyBytes(ar, offset, cw.content, 0, cw.content.arrayLength)
			offset += cw.content.arrayLength
			
			cw = cw.next
			}
		
		bufferContent = ar
		}
	
	}
Revision history
To propose a new revision to this entity, use dana source put -uc your/new/version.dn -n data.StreamBuffer -m "reason for update" -u yourUsername
Version 2 by barry
Version 1 (this version) by barry
Notes for this version: Standard Library Initialisation