Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/marcel/magic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -317,9 +317,10 @@ def self.io_read(io, length, buffer, mode)
end

def self.read_mode(io)
return :native if io.is_a?(IO) || io.is_a?(StringIO)
reader = io.method(:read)
return :native if reader.owner == IO || reader.owner == StringIO

parameters = io.method(:read).parameters
parameters = reader.parameters
supports_buffer = parameters.any? { |kind,| kind == :rest } ||
parameters.count { |kind,| kind == :req || kind == :opt } >= 2
supports_buffer ? :buffer : :single
Expand Down
14 changes: 14 additions & 0 deletions test/magic_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,20 @@ def read(*)
end
end

test "StringIO subclasses can override read without accepting a buffer" do
io_class = Class.new(StringIO) do
def read(length)
super(length)
end
end
io = io_class.new("abcdef")
buffer = String.new(encoding: Encoding::BINARY)
mode = Marcel::Magic.send(:read_mode, io)

assert_equal :single, mode
assert_equal "abc", Marcel::Magic.send(:io_read, io, 3, buffer, mode)
end

# Before Tika 4.0.0, the timestamped-data magic matched any 11-byte OID under the
# 1.2.840.113549 arc, so sibling CMS content types (compressedData, authData, ...) in
# 1.2.840.113549.1.9.16.1.* were misdetected as application/timestamped-data. The 4.0.0
Expand Down