core :: BytesWriter :: defaultinit
# Write to `bytes` in memory
#
# ~~~
# var writer = new BytesWriter
#
# writer.write "Strings "
# writer.write_char '&'
# writer.write_byte 0x20
# writer.write_bytes "bytes".to_bytes
#
# assert writer.to_s == "\\x53\\x74\\x72\\x69\\x6E\\x67\\x73\\x20\\x26\\x20\\x62\\x79\\x74\\x65\\x73"
# assert writer.bytes.to_s == "Strings & bytes"
# ~~~
#
# As with any binary data, UTF-8 code points encoded on two bytes or more
# can be constructed byte by byte.
#
# ~~~
# writer = new BytesWriter
#
# # Write just the character first half
# writer.write_byte 0xC2
# assert writer.to_s == "\\xC2"
# assert writer.bytes.to_s == "�"
#
# # Complete the character
# writer.write_byte 0xA2
# assert writer.to_s == "\\xC2\\xA2"
# assert writer.bytes.to_s == "¢"
# ~~~
class BytesWriter
super Writer
# Written memory
var bytes = new Bytes.empty
redef fun to_s do return bytes.chexdigest
redef fun write(str)
do
if closed then return
str.append_to_bytes bytes
end
redef fun write_char(c)
do
if closed then return
bytes.add_char c
end
redef fun write_byte(value)
do
if closed then return
bytes.add value
end
redef fun write_bytes_from_cstring(ns, len) do
if closed then return
bytes.append_ns(ns, len)
end
# Is the stream closed?
protected var closed = false
redef fun close do closed = true
redef fun is_writable do return not closed
end
lib/core/stream.nit:604,1--670,3