Seek doesn't work with http streams?

BlitzMax Forums/BlitzMax Programming/Seek doesn't work with http streams?

JoshK(Posted 2008) [#1]
It looks like the stream seek() method does not work with http strings. Is there any way to cache http streams?

--EDIT--

Well, I looked at the HTTPStream source, and I came up with this. Fortunately new stream factories get higher priority, so you can just import this and it will override BRL's httpstream behavior:
Strict

Import BRL.SocketStream
Import BRL.BankStream

Type TCachedHTTPStreamFactory Extends TStreamFactory

	Method CreateStream:TStream( url:Object,proto$,path$,readable,writeable )
		
		Notify 1
		
		If proto<>"http" Return
		
		Local i=path.Find( "/",0 ),server$,file$
		If i<>-1
			server=path[..i]
			file=path[i..]
		Else
			server=path
			file="/"
		EndIf

		Local stream:TStream=TSocketStream.CreateClient( server,80 )
		If Not stream Return

		stream.WriteLine "GET "+file+" HTTP/1.0"
		stream.WriteLine "Host: "+server
		stream.WriteLine ""

		While Not Eof( stream )
			If Not stream.ReadLine() Exit
		Wend
		
		Local bank:TBank=CreateBank()
		Local bs=0
		While Not stream.Eof()
			bs:+1
			bank.resize bs
			bank.PokeByte bs-1,Stream.ReadByte()
		Wend
		stream.close()
		stream=CreateBankStream(bank)
		
		Return stream
	EndMethod

End Type

New TCachedHTTPStreamFactory



skidracer(Posted 2008) [#2]
You should also be able to use OpenFile command instead of OpenStream for cached http: streams.


JoshK(Posted 2008) [#3]
Does ReadFile() still work with other custom stream factories? What is the difference between ReadFile() and ReadStream()?


skidracer(Posted 2008) [#4]
umm you could look in the docs but while i'm here
Rem
bbdoc: Open a file for input and/or output.
about:
This command is similar to the #OpenStream command but will attempt
to cache the contents of the file to ensure serial streams such as 
http: based url's are seekable. Use the #CloseStream command when
finished reading and or writing to a Stream returned by #OpenFile.
End Rem
Function OpenFile:TStream( url:Object,readable=True,writeable=True )
	Local stream:TStream=OpenStream( url,readable,writeable )
	If Not stream Return
	If stream.Pos()=-1 Return TBankStream.Create( TBank.Load(stream) )
	Return stream
End Function