links to this page:    
View this PageEdit this PageUploads to this PageHistory of this PageTop of the SwikiRecent ChangesSearch the SwikiHelp Guide
Last updated at 12:26 pm UTC on 30 October 2019
 StandardFileStream subclasses  
 {CrLfFileStream . MultiByteFileStream}

Consider to use MultiByteFileStream


Question: I want to read from data files created by my stock software, and I know it's in binary mode. Exactly, it's written by some C function as Long or Int32. I can't find a suitable filestream class to deal with it. For example I need a nextLong/nextInt32, nextLongPut:, etc.

Question: Vanessa Freudenberg October 10, 2004 Just use the standard file stream. Binary reading is implemented in PositionableStream, which is a superclass of all file streams. You need to be careful about endianness, of course.

In the class FileStreamTest method testReadIntoStartingAtCount you find an example how to deal with a binary stream:

	| filename file |
	filename := 'filestream.tst'.
	[ | writeBuffer readBuffer bytesRead |
	writeBuffer := (ByteArray new: 2500)
		 atAllPut: 1 ;
	(StandardFileStream forceNewFileNamed: filename)
		 binary ;
		 nextPutAll: writeBuffer ;
	file := StandardFileStream readOnlyFileNamed: filename.
	readBuffer := ByteArray new: 400.
	bytesRead := file
		readInto: readBuffer
		startingAt: 10
		count: 100.
	self assert: bytesRead = 100.
	"quick test"
	self assert: (readBuffer occurrencesOf: 1) = 100.
	"compare test"
	1 to: readBuffer size do:
		[ : n | self assert:
			(readBuffer at: n) = ((n between: 10 	and: 10 + 100 - 1)
				ifTrue: [ writeBuffer at: n ]
				ifFalse: [ 0 ]) ] ]
		[ file ifNotNil: [ file close ].
		FileDirectory default
			deleteFileNamed: filename
			ifAbsent: [ "ok" ] ]

To read a binary file in one go:
 ba := (StandardFileStream readOnlyFileNamed: '../../../myPicture.png') binary contentsOfEntireFile
This will give you a ByteArray so that the following is possible:
 (ba at: 1) = 137 

See http://www.libpng.org/pub/png/spec/1.2/PNG-Structure.html

The first eight bytes of a PNG file always contain the following (decimal) values:

   137 80 78 71 13 10 26 10

To get back a stream evaluate
 ReadStream on: ba


For text files use MultiByteFileStream