[View] [Edit] [Lock] [References] [Attachments] [History] [Home] [Changes] [Search] [Help]
test[nodeJS] 044 File Read (sync)
"044-1 Reading files with #readFileSync:"
| content |
[content := NodeJS fs readFileSync: (TestPath, 'Sample.txt')]
on: Error do: [:ex| ^self error: '044-1 ' ,ex ].
"self print: 'OK-> 044-1 Reading ', (content toString: #utf8) size, ' characters'."
"044-2 Reading filess with #readFileSync:options"
| content |
[content := NodeJS fs readFileSync: (TestPath, 'Sample.txt') options: #utf8]
on: Error do: [:ex| ^self error: '044-2 ' ,ex ].
"self print: 'OK-> 044-2 Reading ', (content toString: #utf8) size, ' characters'."
"044-3 Reading files with #readSync:buffer:offset:length:position: and #fstat:with:"
| fs content |
fs := NodeJS fs.
fs open: (TestPath, 'Sample.txt') flags:#r with: [:error :fd|
error isNil ifFalse: [ ^self error: '044-3 ' ,error].
fs fstat: fd with: [:err :stats| | jsObject size bytesRead|
size := (NodeStats @ stats) size.
jsObject := {'new Buffer(size);'}.
[fs readSync: fd
buffer: (NodeBuffer @ jsObject)
offset: 0
length: jsObject#length
position:0.
content := (NodeBuffer @ jsObject) toString:#utf8.
fs closeSync:fd ]
on: Error
do: [:ex| ^self error: '044-3 ' ,ex]
]
].
"self print: 'OK-> 044-3 Reading ', content size, ' characters'."