Squeak
  links to this page:    
View this PageEdit this PageUploads to this PageHistory of this PageTop of the SwikiRecent ChangesSearch the SwikiHelp Guide
Form fromBinaryStream:
Last updated at 11:44 am UTC on 22 October 2019
Form
 fromBinaryStream: aBinaryStream
 	"Read a Form or ColorForm from given file, using the first byte of the file to guess its format. 
        Currently handles: GIF, uncompressed BMP, and both old = and new DisplayObject writeOn: formats, JPEG, and PCX. 
        Return nil if the file could not be read or was of an unrecognized format."
 
	| firstByte |
	aBinaryStream binary.
	firstByte := aBinaryStream next.
	firstByte = 1 ifTrue: [
		"old Squeakform format"
		^ self new readFromOldFormat: aBinaryStream].
	firstByte = 2 ifTrue: [
		"new Squeak form format"
		^ self new readFrom: aBinaryStream].

	"Try for JPG, GIF, or PCX..."
	"Note: The following call closes the stream."
	^ ImageReadWriter formFromStream: aBinaryStream

Example


 theUrl := 'https://upload.wikimedia.org/wikipedia/commons/thumb/1/11/Squeak-x11.png/617px-Squeak-x11.png'.
 Form fromBinaryStream: (WebClient httpGet: theUrl) content asByteArray readStream.

Note

If it is known that the stream is over a jpg file you may directly call

 JPEGReadWriter2 formFromStream: aStream

It is not necessary to look for a particular reader.