Byte per byte download is slow!

Blitz3D Forums/Blitz3D Programming/Byte per byte download is slow!

You can still use the method - in fact, at present you are writing byte by byte directly to the hard-disk which is horribly slow. Instead, write the data to a memory bank, and then save the bank directly to disk. Again, not tested (as I don't have BlitzPlus/Blitz3D installed), but here is the BlitzGet() function tweaked:

Function BlitzGet2(path$)
	If Left(path$, 7) = "http://" Then path$ = Right(path$, Len(path$) - 7)
	inst = Instr(path$, "/")
	If inst Then
		host$ = Left(path$, inst - 1)
		path$ = Right(path$, Len(path$) - inst + 1)
	Else
		host$ = path$
		path$ = "/"
	EndIf
	
	If out$ = "" Then
		If path = "/" Then
			out$ = "Unknown file.txt"
		Else
			For findSlash = Len(path$) To 1 Step - 1
				testForSlash$ = Mid(path$, findSlash, 1)
				If testForSlash$ = "/" Then
					out$ = Right(path$, Len(path$) - findSlash)
					Exit
				EndIf
			Next
			If out$ = "" Then out$ = "Unknown file.txt"
		EndIf
	EndIf
	
	tcp = OpenTCPStream(host$, 80)
	If tcp Then
		WriteLine tcp, "GET " + path$ + " HTTP/1.1"
		WriteLine tcp, "Host: " + host$
		WriteLine tcp, "User-Agent: unknown"
		WriteLine tcp, "Accept: */*"
		WriteLine tcp, ""
		Repeat
			header$ = ReadLine(tcp)
			reply$ = ""
			inst = Instr(header$, ": ")
			If inst Then reply$ = Left(header$, inst + 1)
			If Lower(reply$) = "content-length: " Then toread = Right(header$, Len(header$) - Len(reply$))
		Until header$ = "" Or Eof(tcp)
		If toread > 0 Then
			Local tmpBank = CreateBank(toread)
			Local i% = 0, tmpReadAvail%
			Repeat
				;==========================================================================================
				If Eof(tcp) Then
					ResizeBank(tmpBank, i)
				Else
					tmpReadAvail = Min(ReadAvail(tcp),toread-i)
					ReadBytes( tmpBank, tcp, i, tmpReadAvail)
					i = i + tmpReadAvail
				EndIf 
				;==========================================================================================
			Until i >= BankSize(tmpBank)
			file = WriteFile(out$)
			WriteBytes( tmpBank, file, 0, BankSize(tmpBank) )
			CloseFile file
			If BankSize(tmpBank) <> toread Then RuntimeError "Download failed!"
			FreeBank( tmpBank )
		EndIf
		CloseTCPStream tcp
	Else
		RuntimeError "Failed to connect"
	EndIf
End Function

Function Min( intOne, intTwo )
	If intOne < intTwo Then Return intOne Else Return intTwo
End Function


Edit: Updated! I've downloaded and installed Blitz3D for you, and have tested the above code. It seems to work far faster than the old code.