Characters versus bytes
All file stream operations accept both character (Character, DBString, String) and byte (Integer, ByteArray) arguments interchangeably as appropriate for the platform. For example, nextPut: accepts either a character or an integer between 0 and 255 on platforms that use single-byte characters, and a character or an integer between 0 to 65535 on platforms that use double-byte characters. The nextPutAll: message accepts an instance of String, DBString, or ByteArray as appropriate for the platform.
Additionally, you can change the type of objects answered by file stream operations such as contents, ext, nextLine, upTo:, and upToAll: by using the isBytes: and isCharacters: messages to specify the type of data that is being streamed over, as shown in the following example:
"Open a text file for reading"
| file contents |
file := CfsReadFileStream open: 'readme.txt'.
file isCharacters: true.
"We are streaming over characters, answer file contents as characters"
contents := file contents.
file close.
^contents
"Open a bitmap image file for reading"
| file contents |
file := CfsReadFileStream open: 'looknice.bmp'.
file isBytes: true.
"We are streaming over bytes, answer file contents as bytes"
contents := file contents.
file close.
^contents
The data type being used by a stream can be determined using the Boolean messages isBytes and isCharacters. These messages are opposites; exactly one returns true at any given time.
Tip:
Care must be taken when using Stream methods position, position:, size, and copyFrom:to: with CfsFileStreams on a double-byte locale, because these are answered in bytes and not in characters.
Last modified date: 01/29/2015