def read(amount = nil, buf = nil)
buffer = buf || ''
done = if amount; nil; else ''; end
partial_amount = amount
loop do
result = done
while !@ios.empty? && (result = @ios.first.read(partial_amount)) == done
@ios.shift
end
result.force_encoding("BINARY") if result.respond_to?(:force_encoding)
buffer << result if result
partial_amount -= result.length if partial_amount && result != done
break if partial_amount && partial_amount <= 0
break if result == done
end
if buffer.length > 0
buffer
else
done
end
end