Class: Zlib::ZStream

Inherits:
Object
  • Object
show all
Defined in:
ext/zlib/zlib.c,
ext/zlib/zlib.c

Overview

Zlib::ZStream is the abstract class for the stream which handles the compressed data. The operations are defined in the subclasses: Zlib::Deflate for compression, and Zlib::Inflate for decompression.

An instance of Zlib::ZStream has one stream (struct zstream in the source) and two variable-length buffers which associated to the input (next_in) of the stream and the output (next_out) of the stream. In this document, "input buffer" means the buffer for input, and "output buffer" means the buffer for output.

Data input into an instance of Zlib::ZStream are temporally stored into the end of input buffer, and then data in input buffer are processed from the beginning of the buffer until no more output from the stream is produced (i.e. until avail_out > 0 after processing). During processing, output buffer is allocated and expanded automatically to hold all output data.

Some particular instance methods consume the data in output buffer and return them as a String.

Here is an ascii art for describing above:

+================ an instance of Zlib::ZStream ================+
||                                                            ||
||     +--------+          +-------+          +--------+      ||
||  +--| output |<---------|zstream|<---------| input  |<--+  ||
||  |  | buffer |  next_out+-------+next_in   | buffer |   |  ||
||  |  +--------+                             +--------+   |  ||
||  |                                                      |  ||
+===|======================================================|===+
   |                                                      |
   v                                                      |
"output data"                                         "input data"

If an error occurs during processing input buffer, an exception which is a subclass of Zlib::Error is raised. At that time, both input and output buffer keep their conditions at the time when the error occurs.

Method Catalogue

Many of the methods in this class are fairly low-level and unlikely to be of interest to users. In fact, users are unlikely to use this class directly; rather they will be interested in Zlib::Inflate and Zlib::Deflate.

The higher level methods are listed below.

  • #total_in
  • #total_out
  • #data_type
  • #adler
  • #reset
  • #finish
  • #finished?
  • #close
  • #closed?

Direct Known Subclasses

Deflate, Inflate

Instance Method Summary collapse

Instance Method Details

#adler ⇒ Object

Returns the adler-32 checksum.



1662
1663
1664
1665
1666
# File 'ext/zlib/zlib.c', line 1662

static VALUE
rb_zstream_adler(VALUE obj)
{
    return rb_uint2inum(get_zstream(obj)->stream.adler);
}

#avail_in ⇒ Object

Returns bytes of data in the input buffer. Normally, returns 0.



1622
1623
1624
1625
1626
1627
1628
# File 'ext/zlib/zlib.c', line 1622

static VALUE
rb_zstream_avail_in(VALUE obj)
{
    struct zstream *z;
    TypedData_Get_Struct(obj, struct zstream, &zstream_data_type, z);
    return NIL_P(z->input) ? INT2FIX(0) : LEN2NUM(RSTRING_LEN(z->input));
}

#avail_out ⇒ Object

Returns number of bytes of free spaces in output buffer. Because the free space is allocated automatically, this method returns 0 normally.



1593
1594
1595
1596
1597
1598
1599
# File 'ext/zlib/zlib.c', line 1593

static VALUE
rb_zstream_avail_out(VALUE obj)
{
    struct zstream *z;
    TypedData_Get_Struct(obj, struct zstream, &zstream_data_type, z);
    return rb_uint2inum(z->stream.avail_out);
}

#avail_out=(size) ⇒ Object

Allocates size bytes of free space in the output buffer. If there are more than size bytes already in the buffer, the buffer is truncated. Because free space is allocated automatically, you usually don't need to use this method.



1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
# File 'ext/zlib/zlib.c', line 1607

static VALUE
rb_zstream_set_avail_out(VALUE obj, VALUE size)
{
    struct zstream *z = get_zstream(obj);
    int len = FIX2INT(size);

    if (len < 0) rb_raise(rb_eArgError, "negative size");

    zstream_expand_buffer_into(z, len);
    return size;
}

#close ⇒ Object

Closes the stream. All operations on the closed stream will raise an exception.



1516
1517
1518
1519
1520
1521
# File 'ext/zlib/zlib.c', line 1516

static VALUE
rb_zstream_end(VALUE obj)
{
    zstream_end(get_zstream(obj));
    return Qnil;
}

#closed? ⇒ Boolean

Returns true if the stream is closed.

Returns:

  • (Boolean)


1680
1681
1682
1683
1684
1685
1686
# File 'ext/zlib/zlib.c', line 1680

static VALUE
rb_zstream_closed_p(VALUE obj)
{
    struct zstream *z;
    TypedData_Get_Struct(obj, struct zstream, &zstream_data_type, z);
    return ZSTREAM_IS_READY(z) ? Qfalse : Qtrue;
}

#data_type ⇒ Object

Guesses the type of the data which have been inputted into the stream. The returned value is either BINARY, ASCII, or UNKNOWN.



1653
1654
1655
1656
1657
# File 'ext/zlib/zlib.c', line 1653

static VALUE
rb_zstream_data_type(VALUE obj)
{
    return INT2FIX(get_zstream(obj)->stream.data_type);
}

#end ⇒ Object

Closes the stream. All operations on the closed stream will raise an exception.



1516
1517
1518
1519
1520
1521
# File 'ext/zlib/zlib.c', line 1516

static VALUE
rb_zstream_end(VALUE obj)
{
    zstream_end(get_zstream(obj));
    return Qnil;
}

#ended? ⇒ Boolean

Returns true if the stream is closed.

Returns:

  • (Boolean)


1680
1681
1682
1683
1684
1685
1686
# File 'ext/zlib/zlib.c', line 1680

static VALUE
rb_zstream_closed_p(VALUE obj)
{
    struct zstream *z;
    TypedData_Get_Struct(obj, struct zstream, &zstream_data_type, z);
    return ZSTREAM_IS_READY(z) ? Qfalse : Qtrue;
}

#finish ⇒ String #finish {|chunk| ... } ⇒ nil

Finishes the stream and flushes output buffer. If a block is given each chunk is yielded to the block until the input buffer has been flushed to the output buffer.

Overloads:

  • #finish ⇒ String

    Returns:

    • (String)
  • #finish {|chunk| ... } ⇒ nil

    Yields:

    • (chunk)

    Returns:

    • (nil)


1543
1544
1545
1546
1547
1548
1549
1550
1551
# File 'ext/zlib/zlib.c', line 1543

static VALUE
rb_zstream_finish(VALUE obj)
{
    struct zstream *z = get_zstream(obj);

    zstream_run(z, (Bytef*)"", 0, Z_FINISH);

    return zstream_detach_buffer(z);
}

#finished? ⇒ Boolean

Returns true if the stream is finished.

Returns:

  • (Boolean)


1671
1672
1673
1674
1675
# File 'ext/zlib/zlib.c', line 1671

static VALUE
rb_zstream_finished_p(VALUE obj)
{
    return ZSTREAM_IS_FINISHED(get_zstream(obj)) ? Qtrue : Qfalse;
}

#flush_next_in ⇒ Object

Flushes input buffer and returns all data in that buffer.



1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
# File 'ext/zlib/zlib.c', line 1559

static VALUE
rb_zstream_flush_next_in(VALUE obj)
{
    struct zstream *z;
    VALUE dst;

    TypedData_Get_Struct(obj, struct zstream, &zstream_data_type, z);
    dst = zstream_detach_input(z);
    return dst;
}

#flush_next_out ⇒ String #flush_next_out {|chunk| ... } ⇒ nil

Flushes output buffer and returns all data in that buffer. If a block is given each chunk is yielded to the block until the current output buffer has been flushed.

Overloads:

  • #flush_next_out ⇒ String

    Returns:

    • (String)
  • #flush_next_out {|chunk| ... } ⇒ nil

    Yields:

    • (chunk)

    Returns:

    • (nil)


1579
1580
1581
1582
1583
1584
1585
1586
1587
# File 'ext/zlib/zlib.c', line 1579

static VALUE
rb_zstream_flush_next_out(VALUE obj)
{
    struct zstream *z;

    TypedData_Get_Struct(obj, struct zstream, &zstream_data_type, z);

    return zstream_detach_buffer(z);
}

#reset ⇒ Object

Resets and initializes the stream. All data in both input and output buffer are discarded.



1527
1528
1529
1530
1531
1532
# File 'ext/zlib/zlib.c', line 1527

static VALUE
rb_zstream_reset(VALUE obj)
{
    zstream_reset(get_zstream(obj));
    return Qnil;
}

#stream_end? ⇒ Boolean

Returns true if the stream is finished.

Returns:

  • (Boolean)


1671
1672
1673
1674
1675
# File 'ext/zlib/zlib.c', line 1671

static VALUE
rb_zstream_finished_p(VALUE obj)
{
    return ZSTREAM_IS_FINISHED(get_zstream(obj)) ? Qtrue : Qfalse;
}

#total_in ⇒ Object

Returns the total bytes of the input data to the stream. FIXME



1633
1634
1635
1636
1637
# File 'ext/zlib/zlib.c', line 1633

static VALUE
rb_zstream_total_in(VALUE obj)
{
    return rb_uint2inum(get_zstream(obj)->stream.total_in);
}

#total_out ⇒ Object

Returns the total bytes of the output data from the stream. FIXME



1642
1643
1644
1645
1646
# File 'ext/zlib/zlib.c', line 1642

static VALUE
rb_zstream_total_out(VALUE obj)
{
    return rb_uint2inum(get_zstream(obj)->stream.total_out);
}