<commit_before># ByteBuffer

* Source: [http://github.com/netarc/byte_buffer](http://github.com/netarc/byte_buffer)

ByteBuffer is a tool for reading & writing data to a blob.  With the ability to easily extend it to write/read custom data types.

## Quick Start

Simply:

    gem install byte_buffer

Then just begin working with the Buffer with something like:

    bb = ByteBuffer.new
    bb.write_null_string "FOOBAR"
    bb.write_word 23

    file.write bb.buffer

Or maybe for reading data from our output above:

    bb = ByteBuffer.new(file.read)
    title = bb.read_null_string     # => FOOBAR
    count = bb.read_word            # => 23

## Extendable

Want to have your own custom types? No problem! Simple as including in your project at startup:

    class Bytebuffer
      define_type :dbl_null_string do |type|
        type.read = Proc.new do |byte_buffer, args|
          result = ""
          while true
            byte = byte_buffer.read(1).to_s
            break if byte.empty? || byte == "\x00"
            result <<= byte
          end
          result
        end
        type.write = Proc.new do |byte_buffer, data|
          byte_buffer.write data
          byte_buffer.write 0x00
          byte_buffer.write 0x00
        end
      end
    end
<commit_msg>updated example in readme<commit_after># ByteBuffer

* Source: [http://github.com/netarc/byte_buffer](http://github.com/netarc/byte_buffer)

ByteBuffer is a tool for reading & writing data to a blob.  With the ability to easily extend it to write/read custom data types.

## Quick Start

Simply:

    gem install byte_buffer

Then just begin working with the Buffer with something like:

    bb = ByteBuffer.new
    bb.write_null_string "FOOBAR"
    bb.write_word 23

    file.write bb.buffer

Or maybe for reading data from our output above:

    bb = ByteBuffer.new(file.read)
    title = bb.read_null_string     # => FOOBAR
    count = bb.read_word            # => 23

## Extendable

Want to have your own custom types? No problem! Simple as including in your project at startup:

    class Bytebuffer
      define_type :dbl_null_string do |type|
        type.read = Proc.new do |byte_buffer, args|
          result = ""
          cnt = 0
          while true
            byte = byte_buffer.read(1).to_s
            if byte == "\x00"
              cnt+=1
              break if cnt >= 2
              next
            end
            result <<= byte
          end
          result
        end
        type.write = Proc.new do |byte_buffer, data|
          byte_buffer.write data
          byte_buffer.write 0x00
          byte_buffer.write 0x00
        end
      end
    end

{'fraction_non_alphanumeric': 0.07333584054155698, 'fraction_numerical': 0.010906355772846935, 'mean_word_length': 2.0539609644087258, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2511882', 'n_tokens_mistral': 818, 'n_tokens_neox': 768, 'n_words': 262}