class Bzip2::Writer

Writer

A Bzip2::Writer represents a stream which compresses data written to it. It can be constructed with another IO object (a File) which data can be written to. Otherwise, data is all stored internally as a string and can be retrieved via the #flush method

It can both write to files:

writer = Bzip2::Writer.open('file')
writer << data
writer.close

Bzip2::Writer.open('file'){ |f| f << data }

writer = Bzip2::Writer.new File.open('file')

And output data as a string

writer = Bzip2::Writer.new
writer << data
writer.flush # => data compressed via bz2

@see Bzip2::Writer#initialize The initialize method for examples

Public Class Methods

allocate() click to toggle source

Internally allocates information about a new writer @private

static VALUE bz_writer_s_alloc(VALUE obj) {
    struct bz_file *bzf;
    VALUE res;
    res = Data_Make_Struct(obj, struct bz_file, bz_file_mark, bz_writer_free, bzf);
    bzf->bzs.bzalloc = bz_malloc;
    bzf->bzs.bzfree = bz_free;
    bzf->blocks = DEFAULT_BLOCKS;
    bzf->state = BZ_OK;
    return res;
}
new(*args) click to toggle source

Internally allocates data,

@see Bzip2::Writer#initialize @see Bzip2::Reader#initialize @private

static VALUE bz_s_new(int argc, VALUE *argv, VALUE obj) {
    VALUE res = rb_funcall2(obj, rb_intern("allocate"), 0, 0);
    rb_obj_call_init(res, argc, argv);
    return res;
}
open(filename, mode='wb', &block=nil) → Bzip2::Writer click to toggle source

@param [String] filename the name of the file to write to @param [String] mode a mode string passed to Kernel#open @yieldparam [Bzip2::Writer] writer the Bzip2::Writer instance

If a block is given, the created Bzip2::Writer instance is yielded to the block and will be closed when the block completes. It is guaranteed via ensure that the writer is closed

If a block is not given, a Bzip2::Writer instance will be returned

Bzip2::Writer.open('file') { |f| f << data }

writer = Bzip2::Writer.open('file')
writer << data
writer.close

@return [Bzip2::Writer, nil]

static VALUE bz_writer_s_open(int argc, VALUE *argv, VALUE obj) {
    VALUE res;
    struct bz_file *bzf;

    if (argc < 1) {
        rb_raise(rb_eArgError, "invalid number of arguments");
    }
    if (argc == 1) {
        argv[0] = rb_funcall(rb_mKernel, id_open, 2, argv[0],
            rb_str_new2("wb"));
    } else {
        argv[1] = rb_funcall2(rb_mKernel, id_open, 2, argv);
        argv += 1;
        argc -= 1;
    }
    res = rb_funcall2(obj, id_new, argc, argv);
    Data_Get_Struct(res, struct bz_file, bzf);
    bzf->flags |= BZ2_RB_CLOSE;
    if (rb_block_given_p()) {
        return rb_ensure(rb_yield, res, bz_writer_close, res);
    }
    return res;
}

Public Instance Methods

<<(data) click to toggle source

Append some data to this buffer, returning the buffer so this method can be chained

writer = Bzip2::Writer.new
writer << 'asdf' << 1 << obj << 'a'
writer.flush

@param [#to_s] data anything responding to to_s @see IO#<<

# File lib/bzip2/writer.rb, line 40
def << data
end
close() click to toggle source

Closes this writer for further use. The remaining data is compressed and flushed.

If the writer was constructed with an io object, that object is returned. Otherwise, the actual compressed data is returned

writer = Bzip2::Writer.new File.open('path', 'w')
writer << 'a'
writer.close # => #<File:path>

writer = Bzip2::Writer.new
writer << 'a'
writer.close # => "BZh91AY&SY...
static VALUE bz_writer_close(VALUE obj) {
    struct bz_file *bzf;
    VALUE res;

    Get_BZ2(obj, bzf);
    res = bz_writer_internal_close(bzf);
    return res;
}
close!() click to toggle source

Calls #close and then does some more stuff…

static VALUE bz_writer_close_bang(VALUE obj) {
    struct bz_file *bzf;
    int closed;

    Get_BZ2(obj, bzf);
    closed = bzf->flags & (BZ2_RB_INTERNAL|BZ2_RB_CLOSE);
    bz_writer_close(obj);
    if (!closed && rb_respond_to(bzf->io, id_close)) {
        if (rb_respond_to(bzf->io, id_closed)) {
            closed = RTEST(rb_funcall2(bzf->io, id_closed, 0, 0));
        }
        if (!closed) {
            rb_funcall2(bzf->io, id_close, 0, 0);
        }
    }
    return Qnil;
}
finish()
Alias for: flush
flush() click to toggle source

Flushes all of the data in this stream to the underlying IO.

If this writer was constructed with no underlying io object, the compressed data is returned as a string.

@return [String, nil] @raise [IOError] if the stream has been closed

static VALUE bz_writer_flush(VALUE obj) {
    struct bz_file *bzf;

    Get_BZ2(obj, bzf);
    if (bzf->flags & BZ2_RB_INTERNAL) {
        return bz_writer_close(obj);
    }
    bz_writer_internal_flush(bzf);
    return Qnil;
}
Also aliased as: finish, finish
print(*objs) click to toggle source

Similar to #puts except a newline is not appended after each object appended to this buffer

@see IO#print

printf(format, *ojbs) click to toggle source

Prints data to this buffer with the specified format.

@see Kernel#sprintf

# File lib/bzip2/writer.rb, line 59
def printf format, *ojbs
end
putc(num) click to toggle source

Write one byte into this stream. @param [Integer] num the number value of the character to write @return [Integer] always 1 @raise [IOError] if the stream has been closed

static VALUE bz_writer_putc(VALUE obj, VALUE a) {
    char c = NUM2CHR(a);
    return bz_writer_write(obj, rb_str_new(&c, 1));
}
puts(*objs) click to toggle source

Adds a number of strings to this buffer. A newline is also inserted into the buffer after each object @see IO#puts

# File lib/bzip2/writer.rb, line 46
def puts *objs
end
to_io() click to toggle source

Returns the io stream underlying this stream. If the strem was constructed with a file, that is returned. Otherwise, an empty string is returned.

@return [File, String] similar to whatever the stream was constructed with @raise [IOError] if the stream has been closed

static VALUE bz_to_io(VALUE obj) {
    struct bz_file *bzf;

    Get_BZ2(obj, bzf);
    return bzf->io;
}
write(data) click to toggle source
Actually writes some data into this stream.

@param [String] data the data to write @return [Integer] the length of the data which was written (uncompressed) @raise [IOError] if the stream has been closed

static VALUE bz_writer_write(VALUE obj, VALUE a) {
    struct bz_file *bzf;
    int n;

    a = rb_obj_as_string(a);
    Get_BZ2(obj, bzf);
    if (!bzf->buf) {
        if (bzf->state != BZ_OK) {
            bz_raise(bzf->state);
        }
        bzf->state = BZ2_bzCompressInit(&(bzf->bzs), bzf->blocks,
            0, bzf->work);
        if (bzf->state != BZ_OK) {
            bz_writer_internal_flush(bzf);
            bz_raise(bzf->state);
        }
        bzf->buf = ALLOC_N(char, BZ_RB_BLOCKSIZE + 1);
        bzf->buflen = BZ_RB_BLOCKSIZE;
        bzf->buf[0] = bzf->buf[bzf->buflen] = '\0';
    }
    bzf->bzs.next_in = RSTRING_PTR(a);
    bzf->bzs.avail_in = RSTRING_LEN(a);
    while (bzf->bzs.avail_in) {
        bzf->bzs.next_out = bzf->buf;
        bzf->bzs.avail_out = bzf->buflen;
        bzf->state = BZ2_bzCompress(&(bzf->bzs), BZ_RUN);
        if (bzf->state == BZ_SEQUENCE_ERROR || bzf->state == BZ_PARAM_ERROR) {
            bz_writer_internal_flush(bzf);
            bz_raise(bzf->state);
        }
        bzf->state = BZ_OK;
        if (bzf->bzs.avail_out < bzf->buflen) {
            n = bzf->buflen - bzf->bzs.avail_out;
            rb_funcall(bzf->io, id_write, 1, rb_str_new(bzf->buf, n));
        }
    }
    return INT2NUM(RSTRING_LEN(a));
}