lib/protocol/http/body/stream.rb in protocol-http-0.22.6 vs lib/protocol/http/body/stream.rb in protocol-http-0.22.7

- old
+ new

@@ -74,10 +74,15 @@ def read_nonblock(length, buffer = nil) @buffer ||= read_next chunk = nil + unless @buffer + buffer&.clear + return + end + if @buffer.bytesize > length chunk = @buffer.byteslice(0, length) @buffer = @buffer.byteslice(length, @buffer.bytesize) else chunk = @buffer @@ -92,31 +97,40 @@ return buffer end def write(buffer) - @output.write(buffer) + if @output + @output.write(buffer) + return buffer.bytesize + else + raise IOError, "Stream is not writable, output has been closed!" + end end alias write_nonblock write def flush end def close_read @input&.close + @input = nil end # close must never be called on the input stream. huh? def close_write @output&.close + @output = nil end # Close the input and output bodies. def close self.close_read self.close_write + + return nil ensure @closed = true end # Whether the stream has been closed. @@ -130,14 +144,14 @@ end private def read_next - if chunk = @input&.read - return chunk + if @input + return @input.read else @input = nil - return nil + raise IOError, "Stream is not readable, input has been closed!" end end end end end