Sha256: 25ee81c1759f98b93a33b645f5f4def9e1b522b24d999838918a44418bfa1ca3

Contents?: true

Size: 1.52 KB

Versions: 38

Compression:

Stored size: 1.52 KB

Contents

require 'stringio'

class ChunkWriterIO < StringIO
  # Writer for chunked data.

  MAX_CHUNK_SIZE = 0xFFFF

  def initialize
    @output_buffer = []
    @output_size = 0
    super
  end

  # Write some bytes, splitting into chunks if necessary.
  def write_with_chunking(string)
    # Kernel.puts "Write!"
    until string.empty?
      future_size = @output_size + string.size
      if future_size >= MAX_CHUNK_SIZE
        last = MAX_CHUNK_SIZE - @output_size
        write_buffer!(string[0, last], MAX_CHUNK_SIZE)
        string = string[last..-1]

        write_without_chunking(buffer_result)
        clear_buffer!
      else
        write_buffer!(string, future_size)

        string = ''
      end
    end
  end

  alias write_without_chunking write
  alias write write_with_chunking

  def flush(zero_chunk = false)
    write_without_chunking(buffer_result(zero_chunk))
    clear_buffer!

    super()
  end

  # Close the stream.
  def close(zero_chunk = false)
    flush(zero_chunk)
    super
  end

  # private
  def write_buffer!(string, size)
    @output_buffer << string
    @output_size = size
  end

  def buffer_result(zero_chunk = false)
    result = ''

    # Kernel.puts 'result1', result.inspect
    if !@output_buffer.empty?
      result << [@output_size].pack('s>*')
      result.concat(@output_buffer.join)
    end

    # Kernel.puts 'result2', result.inspect
    result << "\x00\x00" if zero_chunk
    # Kernel.puts 'result3', result.inspect

    result
  end

  def clear_buffer!
    @output_buffer.clear
    @output_size = 0
  end
end

Version data entries

38 entries across 38 versions & 1 rubygems

Version Path
neo4j-core-7.0.7 lib/neo4j/core/cypher_session/adaptors/bolt/chunk_writer_io.rb
neo4j-core-7.0.6 lib/neo4j/core/cypher_session/adaptors/bolt/chunk_writer_io.rb
neo4j-core-7.0.5 lib/neo4j/core/cypher_session/adaptors/bolt/chunk_writer_io.rb
neo4j-core-7.0.4 lib/neo4j/core/cypher_session/adaptors/bolt/chunk_writer_io.rb
neo4j-core-7.0.3 lib/neo4j/core/cypher_session/adaptors/bolt/chunk_writer_io.rb
neo4j-core-7.0.2 lib/neo4j/core/cypher_session/adaptors/bolt/chunk_writer_io.rb
neo4j-core-7.0.1 lib/neo4j/core/cypher_session/adaptors/bolt/chunk_writer_io.rb
neo4j-core-7.0.0 lib/neo4j/core/cypher_session/adaptors/bolt/chunk_writer_io.rb
neo4j-core-7.0.0.rc.2 lib/neo4j/core/cypher_session/adaptors/bolt/chunk_writer_io.rb
neo4j-core-7.0.0.rc.1 lib/neo4j/core/cypher_session/adaptors/bolt/chunk_writer_io.rb
neo4j-core-7.0.0.alpha.8 lib/neo4j/core/cypher_session/adaptors/bolt/chunk_writer_io.rb
neo4j-core-7.0.0.alpha.7 lib/neo4j/core/cypher_session/adaptors/bolt/chunk_writer_io.rb
neo4j-core-7.0.0.alpha.6 lib/neo4j/core/cypher_session/adaptors/bolt/chunk_writer_io.rb
neo4j-core-7.0.0.alpha.5 lib/neo4j/core/cypher_session/adaptors/bolt/chunk_writer_io.rb
neo4j-core-7.0.0.alpha.4 lib/neo4j/core/cypher_session/adaptors/bolt/chunk_writer_io.rb
neo4j-core-7.0.0.alpha.3 lib/neo4j/core/cypher_session/adaptors/bolt/chunk_writer_io.rb
neo4j-core-7.0.0.alpha.2 lib/neo4j/core/cypher_session/adaptors/bolt/chunk_writer_io.rb
neo4j-core-7.0.0.alpha.1 lib/neo4j/core/cypher_session/adaptors/bolt/chunk_writer_io.rb