diff --git a/src/crystal/system/win32/file_descriptor.cr b/src/crystal/system/win32/file_descriptor.cr index 3967e5e9f4a2..28501406df8d 100644 --- a/src/crystal/system/win32/file_descriptor.cr +++ b/src/crystal/system/win32/file_descriptor.cr @@ -14,13 +14,14 @@ module Crystal::System::FileDescriptor private def unbuffered_write(slice : Bytes) loop do + return if slice.empty? + bytes_written = LibC._write(@fd, slice, slice.size) if bytes_written == -1 raise Errno.new("Error writing file") end slice += bytes_written - return if slice.size == 0 end end diff --git a/src/flate/writer.cr b/src/flate/writer.cr index ab504842c38b..f16bd60e42af 100644 --- a/src/flate/writer.cr +++ b/src/flate/writer.cr @@ -48,6 +48,8 @@ class Flate::Writer < IO def write(slice : Bytes) check_open + return if slice.empty? + @stream.avail_in = slice.size @stream.next_in = slice consume_output LibZ::Flush::NO_FLUSH diff --git a/src/gzip/writer.cr b/src/gzip/writer.cr index eb40b0c43f4d..8a86e6eaf132 100644 --- a/src/gzip/writer.cr +++ b/src/gzip/writer.cr @@ -68,6 +68,8 @@ class Gzip::Writer < IO def write(slice : Bytes) : Nil check_open + return if slice.empty? + flate_io = write_header flate_io.write(slice) diff --git a/src/http/server/response.cr b/src/http/server/response.cr index fbff80976387..9a3c7fc6c8d2 100644 --- a/src/http/server/response.cr +++ b/src/http/server/response.cr @@ -63,6 +63,8 @@ class HTTP::Server # See `IO#write(slice)`. def write(slice : Bytes) + return if slice.empty? + @output.write(slice) end @@ -159,6 +161,8 @@ class HTTP::Server end private def unbuffered_write(slice : Bytes) + return if slice.empty? + unless response.wrote_headers? if response.version != "HTTP/1.0" && !response.headers.has_key?("Content-Length") response.headers["Transfer-Encoding"] = "chunked" diff --git a/src/http/web_socket/protocol.cr b/src/http/web_socket/protocol.cr index 9a09de6394fc..798bc24f95ec 100644 --- a/src/http/web_socket/protocol.cr +++ b/src/http/web_socket/protocol.cr @@ -50,6 +50,8 @@ class HTTP::WebSocket::Protocol end def write(slice : Bytes) + return if slice.empty? + count = Math.min(@buffer.size - @pos, slice.size) (@buffer + @pos).copy_from(slice.pointer(count), count) @pos += count diff --git a/src/io/buffered.cr b/src/io/buffered.cr index f2bd56df3fb9..ce218a8f53ab 100644 --- a/src/io/buffered.cr +++ b/src/io/buffered.cr @@ -113,6 +113,8 @@ module IO::Buffered def write(slice : Bytes) check_open + return if slice.empty? + count = slice.size if sync? diff --git a/src/io/hexdump.cr b/src/io/hexdump.cr index f383c0c694bd..7fdbc46014f8 100644 --- a/src/io/hexdump.cr +++ b/src/io/hexdump.cr @@ -33,6 +33,8 @@ class IO::Hexdump < IO end def write(buf : Bytes) + return if buf.empty? + @io.write(buf).tap do @output.puts buf.hexdump if @write end diff --git a/src/io/multi_writer.cr b/src/io/multi_writer.cr index 9bcfb8125e11..60bfd076178a 100644 --- a/src/io/multi_writer.cr +++ b/src/io/multi_writer.cr @@ -32,6 +32,8 @@ class IO::MultiWriter < IO def write(slice : Bytes) check_open + return if slice.empty? + @writers.each { |writer| writer.write(slice) } end diff --git a/src/io/stapled.cr b/src/io/stapled.cr index f45eb04f280a..101532192da0 100644 --- a/src/io/stapled.cr +++ b/src/io/stapled.cr @@ -68,6 +68,8 @@ class IO::Stapled < IO def write(slice : Bytes) : Nil check_open + return if slice.empty? + @writer.write(slice) end diff --git a/src/io/syscall.cr b/src/io/syscall.cr index 4bf93b9b638f..77665e5ba210 100644 --- a/src/io/syscall.cr +++ b/src/io/syscall.cr @@ -62,22 +62,26 @@ module IO::Syscall end def write_syscall_helper(slice : Bytes, errno_msg : String) : Nil - loop do - bytes_written = yield slice - if bytes_written != -1 - slice += bytes_written - return if slice.size == 0 - else - if Errno.value == Errno::EAGAIN - wait_writable + return if slice.empty? + + begin + loop do + bytes_written = yield slice + if bytes_written != -1 + slice += bytes_written + return if slice.size == 0 else - raise Errno.new(errno_msg) + if Errno.value == Errno::EAGAIN + wait_writable + else + raise Errno.new(errno_msg) + end end end - end - ensure - if (writers = @writers) && !writers.empty? - add_write_event + ensure + if (writers = @writers) && !writers.empty? + add_write_event + end end end diff --git a/src/openssl/digest/digest_io.cr b/src/openssl/digest/digest_io.cr index 4231c64182d0..74c30a03165b 100644 --- a/src/openssl/digest/digest_io.cr +++ b/src/openssl/digest/digest_io.cr @@ -43,6 +43,8 @@ module OpenSSL end def write(slice : Bytes) + return if slice.empty? + if @mode.write? digest_algorithm.update(slice) end diff --git a/src/openssl/ssl/socket.cr b/src/openssl/ssl/socket.cr index 10fc100966d4..2eac660612f2 100644 --- a/src/openssl/ssl/socket.cr +++ b/src/openssl/ssl/socket.cr @@ -107,6 +107,8 @@ abstract class OpenSSL::SSL::Socket < IO def unbuffered_write(slice : Bytes) check_open + return if slice.empty? + count = slice.size bytes = LibSSL.ssl_write(@ssl, slice.pointer(count), count) unless bytes > 0 diff --git a/src/string/builder.cr b/src/string/builder.cr index e3e7b580df71..f1fb1cfd8c0d 100644 --- a/src/string/builder.cr +++ b/src/string/builder.cr @@ -39,6 +39,8 @@ class String::Builder < IO end def write(slice : Bytes) + return if slice.empty? + count = slice.size new_bytesize = real_bytesize + count if new_bytesize > @capacity diff --git a/src/zip/checksum_writer.cr b/src/zip/checksum_writer.cr index 2158f2ff4952..8866bc559123 100644 --- a/src/zip/checksum_writer.cr +++ b/src/zip/checksum_writer.cr @@ -14,6 +14,8 @@ module Zip end def write(slice : Bytes) + return if slice.empty? + @count += slice.size @crc32 = CRC32.update(slice, @crc32) if @compute_crc32 io.write(slice) diff --git a/src/zlib/writer.cr b/src/zlib/writer.cr index d57025c72949..f742aa994075 100644 --- a/src/zlib/writer.cr +++ b/src/zlib/writer.cr @@ -47,6 +47,8 @@ class Zlib::Writer < IO def write(slice : Bytes) : Nil check_open + return if slice.empty? + write_header unless @wrote_header @flate_io.write(slice)