Write binary data to stdout in Crystal

370 views Asked by At

I'm trying to output binary data to stdout (to serve some dynamic binary data using Kemal).

Here is a test:

size = File.size( "./img.png" )
slice = Slice( UInt8 ).new( size )
File.open( "./img.png" ) do |file|
  file.read_fully( slice )
end

I tried without success:

slice
slice.hexdump
slice.hexstring
slice.to_a
slice.to_s
slice.to_unsafe.value
1

There are 1 answers

2
Jonne Haß On BEST ANSWER

You can just use IO#write(Slice):

STDOUT.write(slice)