File: TempBuffer.pm

package info (click to toggle)
libplack-perl 0.9989-1%2Bdeb7u1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,556 kB
  • sloc: perl: 6,890; python: 6; makefile: 2
file content (67 lines) | stat: -rw-r--r-- 1,363 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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  -> Always temp file
    # $MaxMemoryBufferSize = -1 -> Always PerlIO
    my $backend;
    if ($MaxMemoryBufferSize < 0) {
        $backend = "PerlIO";
    } elsif ($MaxMemoryBufferSize == 0) {
        $backend = "File";
    } elsif (!$length) {
        $backend = "Auto";
    } elsif ($length > $MaxMemoryBufferSize) {
        $backend = "File";
    } else {
        $backend = "PerlIO";
    }

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

sub create {
    my($class, $backend, $length, $max) = @_;
    Plack::Util::load_class($backend, $class)->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->new($length);
  $buf->print($bytes);

  my $size = $buf->size;
  my $fh   = $buf->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<Plack> L<Plack::Request>

=cut