<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package Plack::TempBuffer;
use strict;
use warnings;
use Plack::Util;
use FileHandle; # for seek etc.

our $MaxMemoryBufferSize = 1024 * 1024;

sub new {
    my($class, $length) = @_;

    # $MaxMemoryBufferSize = 0  -&gt; Always temp file
    # $MaxMemoryBufferSize = -1 -&gt; Always PerlIO
    my $backend;
    if ($MaxMemoryBufferSize &lt; 0) {
        $backend = "PerlIO";
    } elsif ($MaxMemoryBufferSize == 0) {
        $backend = "File";
    } elsif (!$length) {
        $backend = "Auto";
    } elsif ($length &gt; $MaxMemoryBufferSize) {
        $backend = "File";
    } else {
        $backend = "PerlIO";
    }

    $class-&gt;create($backend, $length, $MaxMemoryBufferSize);
}

sub create {
    my($class, $backend, $length, $max) = @_;
    Plack::Util::load_class($backend, $class)-&gt;new($length, $max);
}

sub print;
sub rewind;
sub size;

1;

__END__

=head1 NAME

Plack::TempBuffer - temporary buffer to save bytes

=head1 SYNOPSIS

  my $buf = Plack::TempBuffer-&gt;new($length);
  $buf-&gt;print($bytes);

  my $size = $buf-&gt;size;
  my $fh   = $buf-&gt;rewind;

=head1 DESCRIPTION

Plack::TempBuffer is a buffer class to store arbitrary length of byte
strings and then get a seekable filehandle once everything is
buffered. It uses PerlIO and/or temporary file to save the buffer
depending on the length of the size.

=head1 SEE ALSO

L&lt;Plack&gt; L&lt;Plack::Request&gt;

=cut

</pre></body></html>