在 Crystal 中将二进制数据写入标准输出

Write binary data to stdout in Crystal

我正在尝试将二进制数据输出到标准输出(以使用 Kemal 提供一些动态二进制数据)。

这是一个测试:

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

我试过没有成功:

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

你可以直接使用 IO#write(Slice):

STDOUT.write(slice)