NAME

Imager::IO - Imager's io_layer object.

SYNOPSIS

# Imager supplies Imager::IO objects to various callbacks
my $IO = ...;

my $count = $IO->write($data);
my $count = $IO->read($buffer, $max_count);
my $position = $IO->seek($offset, $whence);
my $status = $IO->close;

DESCRIPTION

Imager uses an abstraction when dealing with image files to allow the same code to work with disk files, in memory data and callbacks.

If you're writing an Imager file handler your code will be passed an Imager::IO object to write to or read from.

Note that Imager::IO can only work with collections of bytes - if you need to read UTF-8 data you will need to read the bytes and decode them. If you want to write UTF-8 data you will need to encode your characters to bytes and write the bytes.

CONSTRUCTORS

new_fd($fd)

Create a new I/O layer based on a file descriptor.

my $io = Imager::IO->new(fileno($fh));
new_buffer($data)

Create a new I/O layer based on a memory buffer.

Buffer I/O layers are read only.

$data can either a simple octet string, or a reference to an octet string. If $data contains characters with a code point above 0xFF an exception will be thrown.

new_cb($writecb, $readcb, $seekcb, $closecb)

Create a new I/O layer based on callbacks. See "I/O Callbacks" in Imager::Files for details on the behavior of the callbacks.

new_fh($fh)

Create a new I/O layer based on a perl file handle.

new_bufchain()

Create a new bufchain based I/O layer. This accumulates the file data as a chain of buffers starting from an empty stream.

Use the "slurp()" method to retrieve the accumulated content into a perl string.

BUFFERED I/O METHODS

These methods use buffered I/O to improve performance unless you call set_buffered() to disable buffering.

Prior to Imager 0.86 the write and read methods performed raw I/O.

write($data)

Call to write to the file. Returns the number of bytes written. The data provided may contain only characters \x00 to \xFF - characters outside this range will cause this method to croak().

If you supply a UTF-8 flagged string it will be converted to a byte string, which may have a performance impact.

Returns -1 on error, though in most cases if the result of the write isn't the number of bytes supplied you'll want to treat it as an error anyway.

read($buffer, $size)
my $buffer;
my $count = $io->read($buffer, $max_bytes);

Reads up to $max_bytes bytes from the current position in the file and stores them in $buffer. Returns the number of bytes read on success or an empty list on failure. Note that a read of zero bytes is not a failure, this indicates end of file.

read2($size)
my $buffer = $io->read2($max_bytes);

An alternative interface to read, that might be simpler to use in some cases.

Returns the data read or an empty list. At end of file the data read will be an empty string.

seek($offset, $whence)
my $new_position = $io->seek($offset, $whence);

Seek to a new position in the file. Possible values for $whence are:

  • SEEK_SET - $offset is the new position in the file.

  • SEEK_CUR - $offset is the offset from the current position in the file.

  • SEEK_END - $offset is the offset relative to the end of the file.

Note that seeking past the end of the file may or may not result in an error.

Any buffered output will be flushed, if flushing fails, seek() will return -1.

Returns the new position in the file, or -1 on error.

getc()

Return the next byte from the stream.

Returns the ordinal of the byte or -1 on error or end of file.

while ((my $c = $io->getc) != -1) {
  print chr($c);
}
gets()
gets($max_size)
gets($max_size, $end_of_line)

Returns the next line of input from the stream, as terminated by end_of_line.

The default max_size is 8192.

The default end_of_line is ord "\n".

Returns nothing if the stream is in error or at end of file.

Returns the line as a string, including the line terminator (if one was found) on success.

while (defined(my $line = $io->gets)) {
  # do something with $line
}
peekc()

Return the buffered next character from the stream, loading the buffer if necessary.

For an unbuffered stream a buffer will be setup and loaded with a single character.

Returns the ordinal of the byte or -1 on error or end of file.

my $c = $io->peekc;
peekn($size)

Returns up to the next size bytes from the file as a string.

Only up to the stream buffer size bytes (currently 8192) can be peeked.

This method ignores the buffering state of the stream.

Returns nothing on EOF.

my $s = $io->peekn(4);
if ($s =~ /^(II|MM)\*\0/) {
  print "TIFF image";
}
putc($code)

Write a single character to the stream.

Returns code on success, or -1 on failure.

close()
my $result = $io->close;

Call when you're done with the file. If the IO object is connected to a file this won't close the file handle, but buffers may be flushed (if any).

Returns 0 on success, -1 on failure.

eof()
$io->eof

Test if the stream is at end of file. No further read requests will be passed to your read callback until you seek().

error()

Test if the stream has encountered a read or write error.

my $data = $io->read2(100);
$io->error
   and die "Failed";

When the stream has the error flag set no further read or write requests will be passed to your callbacks until you seek.

flush()
$io->flush
  or die "Flush error";

Flush any buffered output. This will not call lower write layers when the stream has it's error flag set.

Returns a true value on success.

is_buffered()

Test if buffering is enabled for this stream.

Returns a true value if the stream is buffered.

set_buffered($enabled)

If $enabled is a non-zero integer, enable buffering, other disable it.

Disabling buffering will flush any buffered output, but any buffered input will be retained and consumed by input methods.

Returns true if any buffered output was flushed successfully, false if there was an error flushing output.

RAW I/O METHODS

These call the underlying I/O abstraction directly.

raw_write()

Call to write to the file. Returns the number of bytes written. The data provided may contain only characters \x00 to \xFF - characters outside this range will cause this method to croak().

If you supply a UTF-8 flagged string it will be converted to a byte string, which may have a performance impact.

Returns -1 on error, though in most cases if the result of the write isn't the number of bytes supplied you'll want to treat it as an error anyway.

raw_read()
my $buffer;
my $count = $io->raw_read($buffer, $max_bytes);

Reads up to $max_bytes bytes from the current position in the file and stores them in $buffer. Returns the number of bytes read on success or an empty list on failure. Note that a read of zero bytes is not a failure, this indicates end of file.

raw_read2()
my $buffer = $io->raw_read2($max_bytes);

An alternative interface to raw_read, that might be simpler to use in some cases.

Returns the data read or an empty list.

raw_seek()
my $new_position = $io->raw_seek($offset, $whence);

Seek to a new position in the file. Possible values for $whence are:

  • SEEK_SET - $offset is the new position in the file.

  • SEEK_CUR - $offset is the offset from the current position in the file.

  • SEEK_END - $offset is the offset relative to the end of the file.

Note that seeking past the end of the file may or may not result in an error.

Returns the new position in the file, or -1 on error.

raw_close()
my $result = $io->raw_close;

Call when you're done with the file. If the IO object is connected to a file this won't close the file handle.

Returns 0 on success, -1 on failure.

UTILITY METHODS

slurp()

Retrieve the data accumulated from an I/O layer object created with the new_bufchain() method.

my $data = $io->slurp;
dump()

Dump the internal buffering state of the I/O object to stderr.

$io->dump();

AUTHOR

Tony Cook <tonyc@cpan.org>

SEE ALSO

Imager, Imager::Files