Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write binary data to stdout in Crystal

Tags:

crystal-lang

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
like image 838
Mat Avatar asked Jun 15 '26 16:06

Mat


1 Answers

You can just use IO#write(Slice):

STDOUT.write(slice)
like image 193
Jonne Haß Avatar answered Jun 21 '26 07:06

Jonne Haß