http file stream help

BlitzMax Forums/BlitzMax Programming/http file stream help

Kev(Posted 2005) [#1]
hi

When using url stream's they open ok, but getting the stream size of the open stream returns 0. ive never used url streams in blitz so any pointers to where im going wrong would be usefull. Or is it not possable? have i mis-understood the use of StreamSize().


Local infile:TStream = OpenStream("http::myweb.tiscali.co.uk/blitzbasic/WinBlitz3DTest.zip",True,False)
If infile <> Null Then
	Print "stream open"
	Local size = StreamSize(infile)  
	Print size
	CloseStream infile
EndIf

End



thanks for your help
kev


Yan(Posted 2005) [#2]
You can't use StreamSize() on a serial (non seekable) stream.


Kev(Posted 2005) [#3]
cheers TwoEyedPete,

forgive the stupid question but why is the file above not seekable? by what means would i make it seekable. or is local file's(on my hd) only seekable?

kev


skidracer(Posted 2005) [#4]
The following buffers a stream into a bank which because it reads until the end of the stream has a known size and because it is in RAM and not on a computer on the other side of the world is also seekable:
Function OpenBufferedStream:TStream(url:Object)
	Local	bank:TBank
	bank=LoadBank(url)
	If bank Return CreateBankStream(bank)
End Function



Kev(Posted 2005) [#5]
thanks skidracer, does what i need.

kev


Yan(Posted 2005) [#6]
If you just want the size of the file without having to download it then you can use this...

Print HTTPFileSize("http://myweb.tiscali.co.uk/blitzbasic/WinBlitz3DTest.zip") + " Bytes"

End


Function HTTPFileSize(url$)
	url$ = url$.Replace("http://", "")
	Local slashPos = url$.Find("/"), host$, file$
	
	If slashPos <> -1
		host$ = url$[..slashPos]
		file$ = url$[slashPos..]
	Else
		Return -1
	EndIf
	
	Local stream:TStream=OpenStream("tcp::" + host$, 80)
	If Not stream Then Return -1	
	
	stream.WriteLine "HEAD " + file$ + " HTTP/1.0"
	stream.WriteLine "Host: " + host$
	stream.WriteLine ""
	
	While Not Eof(stream)
		Local in$ = stream.ReadLine()

		If in$.Find("Content-Length:") <> -1
			stream.Close()
			Return Int(in$[in$.Find(":") + 1..].Trim())
		EndIf	
	Wend
	
	stream.Close()
	Return -1		
End Function



Kev(Posted 2005) [#7]
thats useful also pete thanks again.

kev


gman(Posted 2005) [#8]
i havent tried it but you can also give OpenFile() a whirl. it returns a stream as well but the manual has this to say about it:


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.