Best way to replace bytes in databuffer

Monkey Forums/Monkey Programming/Best way to replace bytes in databuffer

Hero(Posted 2015) [#1]
Hi,

Need to replace a series of bytes in a data buffer with a series of different bytes. I came up with the code below. It works, but I was wondering if someone knew a more elegant solution.

		Local newFile:FileStream = FileStream.Open(newFilePath, "r")
		Local newFileBuffer:DataBuffer = New DataBuffer(newFile.Length)

		Local oBytes:Int[] =[116, 101, 115, 116] 'test
		Local nBytes:Int[] =[84, 69, 83, 84] 'TEST
				
		newFile.Seek(0)
		newFile.ReadAll(newFileBuffer, 0, newFileBuffer.Length)
		
	    For Local i:Int = 0 To newFileBuffer.Length - 5
	
			Local fourBytes:Int[] = newFileBuffer.PeekBytes(i, 4)
						
			If (fourBytes[0] = oBytes[0] And fourBytes[1] = oBytes[1] And fourBytes[2] = oBytes[2] And fourBytes[3] = oBytes[3])
				newFileBuffer.PokeBytes(i, nBytes, 0, 4)				
			EndIf
		
		Next


Thanks


Hero(Posted 2015) [#2]
Also i am wondering if there is any way to combine the functionality of FileStream and DataBuffer, so that both Peak and ReadString, ReadInt etc. can be used.

My container format in this example is so that each file starts with FFFFFF followed by an identifier that in this example is assumed to be always 5 character, followed by 2 byte with the size of the file data, followed by the file data. Currently I need to use FileStream and DataBuffer together and it is already getting very slow for larger container files.

	Field FileStart:Int[] =[255, 255, 255, 255]

	Method FindFileIndexes:Int[] ()

		Local sourceFileBuffer:DataBuffer = New DataBuffer(SourceFile.Length)
		Local fileIndexes:Int[]
						
		SourceFile.Seek(0)
		SourceFile.ReadAll(sourceFileBuffer, 0, sourceFileBuffer.Length)
		
		For Local i:Int = 0 To sourceFileBuffer.Length - 5
			Local block:Int[] = sourceFileBuffer.PeekBytes(i, 4)
			
			block[0] = block[0] & $FF
			block[1] = block[1] & $FF
			block[2] = block[2] & $FF
			block[3] = block[3] & $FF
						
			If (block[0] = FileStart[0] And block[1] = FileStart[1] And block[2] = FileStart[2] And block[3] = FileStart[3])
			
				fileIndexes = fileIndexes.Resize(fileIndexes.Length + 1)
				fileIndexes[fileIndexes.Length - 1] = i
			
			EndIf
		Next

		sourceFileBuffer.Discard
		
		Return fileIndexes
	End

	Method ExtractFileMenu:Void()
		Print ""
		Local identifier:String = Input("Enter unique identifier: ")
		Local filename:String = Input("Enter filename/path: ")

		Local filePosition:Int = -1
		Local fileIndexes:Int[] = FindFileIndexes()
		
		For Local index:Int = EachIn fileIndexes
			
			SourceFile.Seek(index + 4)
			Local id:String = SourceFile.ReadString(5)
						
			If (id = identifier)
				filePosition = index
				Exit
			EndIf
		Next
		
		If (filePosition = -1)
			Print "File not found."
			Return
		End
		
		Local sourceFileBuffer:DataBuffer = New DataBuffer(SourceFile.Length)
		Local targetFile:FileStream = FileStream.Open(filename, "w")
					
		SourceFile.Seek(filePosition + 4 + 5)
		
		Local fileSize:Int = SourceFile.ReadInt()
		
		SourceFile.ReadAll(sourceFileBuffer, 0, fileSize)
		targetFile.WriteAll(sourceFileBuffer, 0, fileSize)

		targetFile.Close
		sourceFileBuffer.Discard
		
		Print "File has been extracted."
	End